Home Database Mysql Tutorial mysql主从数据库,分库分表等笔记_MySQL

mysql主从数据库,分库分表等笔记_MySQL

Jun 01, 2016 pm 01:32 PM
mysql database

bitsCN.com

mysql主从数据库,分库分表等笔记

 

1、mysql的目录:在rpm或者yum安装时:/var/lib/mysql  在编译安装时默认目录:/usr/local/mysql

 

2、用rpm包安装的MySQL是不会安装/etc/my.cnf文件的,

至于为什么没有这个文件而MySQL却也能正常启动和作用,在点有两个说法,

第一种说法,my.cnf只是MySQL启动时的一个参数文件,可以没有它,这时MySQL会用内置的默认参数启动,

第二种说法,MySQL在启动时自动使用/usr/share/mysql目录下的my-medium.cnf文件,这种说法仅限于rpm包安装的MySQL,

解决方法,只需要复制一个/usr/share/mysql目录下的.cnf文件到/etc目录,并改名为my.cnf即可。

 

在/usr/share/mysql目录下有很多语言目录和mysql多个适用于不同类型项目的对应配置文件

 

3、配置mysql的字符集设置:

 

      在配置文件中

 

      在socket          = /var/lib/mysql/mysql.sock后添加default-character-set = utf8 (决定了客服端字符集和连接字符集)

 

      在myisam_sort_buffer_size = 8M后添加character-set-server = utf8 (决定了服务器字符集和数据库[数据库,表,字段]字符集,由于继承关系),添加collaction-server=usf8_general_ci (这是设置校验字符集,作用于排序有关order by)

 

4、开启mysql的log-bin:

 

    配置文件中开启 log-bin=mysql-bin (等于后面的内容可以随便写,mysql-bin只是说明log-bin的内容写入mysql-bin里面而已),重启mysql后,在/var/lib/mysql下能发现 mysql-bin.000001和mysql-bin.index,说明mysql-bin已经开启

 

5、关于mysqlbin的一些命令

 

     mysql提示符下查看bin-log日志

    show master status;

    #显示最后一个bin-log日志和它的最后一个位置

    show master logs;

    #查看所有的bin-log日志,同时标记最后一个pos位置

 

6、使用mysqlbinlog命令来查看binlog日志

 

     如果提示字符集错误---mysqlbinlog: unknown variable 'default-character-set=utf8' ,可以mysqlbinlog --no-defaults /var/lib/mysql/mysql-bin.000001来忽略字符集错误

 

7、什么时候会产生一个新bin-log日志

     1)重启mysql服务

     2)flush logs;

      #刷新bin-log日志,会产生一个新的bin-log日志 如:mysql-bin.00008

     3) reset master;

   #清空所有在bin-log日志,最后只剩下mysql-bin.00001 且位置为初始位置

 

   需求:公司内真正数据备份与恢复,9:30的时候备份数据,同时刷新bin-log日志,但是10:00数据损坏,如何去恢复9:30以来所有的数据?(在编译安装mysql环境下)

 

  1.9:30备份数据:

      /usr/local/mysql/bin/mysqldump -uroot -p123 test -l -F >/tmp/test.sql

     -l 将表锁了(防止在导表的过程中,又有用户修改或者添加数据),只读  -F (flush) 刷新bin-log日志(产生新的日志文件,继续保存后面的日志记录)

   2.9:30-10:00数据都会写到bin-log

     /usr/loca/mysql/bin/mysqlbinlog --no-defaults mysql-bin.000003

    #只保存增、删、改的语句

  3.10:00数据损坏,如何恢复数据:

    1)恢复9:30备份的数据

        /usr/local/mysql/bin/mysql -uroot -p123 test /test.sql

    2)恢复9:30-10:00之间的bin-log日志中的增、删、改语句

       /usr/local/mysql/bin/mysqlbinlog --no-defaults --stop-position=367 mysql-bin.000003|/usr/local/mysql/bin/mysql -uroot -p123 test

 

      --stop-position 为要恢复数据在logbin的最后日志,可以通过查看日志文件查找位置,还有--start-position表示恢复从该位置以后的数据

  4.这样,数据全部恢复完毕

 

8、主从数据库

 

   从mysql服务器:

  1.vi /etc/my.cnf

  server-id = 2

  log-bin=mysql-bin

  master-host=192.168.255.1

  master-user=user1

  master-password=123

  master-port=3306

  2.重启mysql

  3.show slave status/G

  Slave_IO_Running: Yes

  #从主服务器复制bin-log成功

  Slave_SQL_Running: Yes

  #把复制过来的sql语句执行成功

 

4.出错

#原因,与主服务器的最后一个pos位置对接不成功

slave stop;

change master to

master_host="192.168.10.1",

master_user="user1",

master_password='123',

master_port=3306,

#在主服务器上show master status;

master_log_file="mysql-bin.000001",

master_log_pos= 279;

slave start;

bitsCN.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1252
29
C# Tutorial
1226
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL for Beginners: Getting Started with Database Management MySQL for Beginners: Getting Started with Database Management Apr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles