Home > Database > Mysql Tutorial > Detailed explanation of the sample code sharing of master-slave replication of mysql5.6 under centos7

Detailed explanation of the sample code sharing of master-slave replication of mysql5.6 under centos7

黄舟
Release: 2017-03-29 13:41:39
Original
1079 people have browsed it

This article mainly introduces the detailed explanation of master-slave replication in mysql5.6 under centos7. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.

1. Introduction to mysql master-slave replication

Mysql’s master-slave replication is not on the database disk The file is copied directly, but copied to the local server to be synchronized through the logical binlog log, and then the local thread reads the sql statement in the log and re-applies it to the mysql database.

Mysql database supports replication in different business scenarios such as one-way, two-way, chain cascade, and ring. One server acts as the main server master and receives updates from users, while one or more other servers act as The slave server receives the log content from the master server's binlog file, parses the sql, and updates it to the slave server.

One master and one slave (A -> B, A is the master, B is the slave)

One master and multiple slaves (A -> B, A -> C, A is the slave Master, B and C are slaves)

Double-master bidirectional synchronization (A -> B, B -> A, A and B are both masters and backup each other)

Linear cascade (A -> B -> C , A and B are masters and slaves, and C is slave)

Ring cascade (A -> B -> C -> A, A, B and C are both masters, and each node can write data)

2. Solution to realize mysql master-slave read and write separation

1. Realize reading and writing separation through programs (judgment statementskeywords to connect the master-slave database)

2. Realize reading and writing separation through open source software (mysql-proxy, amoeba, stable The performance and function are average, not recommended for production use)

3. Independent development of DAL layer software

3. Introduction to the principle of mysql master-slave replication

Mysql master-slave replication is an asynchronous replication process, which copies a master library to a slave library. The entire process between master and slave is completed by three threads. The sql thread and I/O thread are on the slave side, and the other I/O thread is on the master side.

Copying Principle and Process

1. Execute the start slave command on the slave, turn on the master-slave replication switch, and start master-slave replication.

2. The I/O thread of the slave requests the master through the authorized replication user on the master and requests the specified location of the specified binlog log.

3. After the master receives the request from the slave's I/O thread, its own I/O thread responsible for copying will read the log information after the specified position of the specified binlog log in batches based on the slave's request information. Then it is returned to the slave's I/O thread. In addition to the binlog log, the returned information also includes the master's new binlog file name and the next specified update position in the new binlog.

4. The slave obtains the binlog log content sent from the I/O thread on the master. After the log file and the location point, the binlog content will be written to the end of the slave's own relay log (relay log) file in turn. And record the new binlog file name and location into the master-info file, so that the next time the new binlog log is read from the master, the master can be told to read from the new location of the new binlog.

5. The slave's sql thread will detect the newly added log content of the I/O thread in the local relay log in real time, parse the content in the relay log file into sql statements in a timely manner, and parse the sql statements in the order of their positions. Execute these sql statements. Relay-log.info records the file name and location of the current application relay log.

4. Mysql master-slave replication operation

I have mysql single-machine multiple instances, 3306, 3308, 3309

The master library is 3306, the slave library is 3308,3309

(1), on the master library

1, set the server-id value and open it Binlog function

> vi /etc/my.cnf
Copy after login
 [mysqld]
 #用于同步的每台机器server-id都不能相同

server-id = 10

log-bin = /data/mysql56/data/mysql-bin
Copy after login

2. Restart the main library

> service mysqld restart
Copy after login

3. Log in to the main library and check the server-id

> mysql -uroot -p

> show variables like 'server_id';
Copy after login

4. Create a database on the main library for copying from the library Account

> grant replication slave on *.* to "rep"@"%" identified by "123456";

> flush privileges;

> select user,host from mysql.user;

> show grants for rep@"%";
Copy after login

5. Read-only lock table of main database database (do not close the current window)

> flush table with read lock;
Copy after login

View main database status

> show master status;
Copy after login

6 , Back up all data files in the main library

> mysqldump -uroot -p -A -B | gzip > /data/mysql_bak.$(date +%F).sql.gz
Copy after login

7. After backing up the main library data, unlock

> unlock tables;
Copy after login

8. Migrate the data exported from the main library to the slave library

( 2) On the slave library

1, set the server-id value and turn off the binlog function

①There are two situations where binlog needs to be turned on

② To synchronize B in the middle of A->B->C in cascade, you need to enable binlog

③When doing database backup in the slave database, you must have full backup and binlog log to be a complete backup.

> vi /mysql-instance/3308/my.cnf 

[mysqld]

server-id = 11

relay-log = /mysql-instance/3308/relay-bin

relay-log-info-file = /mysql-instance/3308/relay-log.info
Copy after login

2. Restart the slave library

> /mysql-instance/3308/mysql restart
Copy after login

3. Log in to the slave library to check the parameters

> mysql -uroot -p -S /mysql-instance/3308/mysql.sock

> show variables like 'log_bin';

> show variables like 'server_id';
Copy after login

4. Restore the data exported by mysqldump from the main library to the slave library

> gzip -d /data/mysql_bak.2017-01-15.sql.gz
Copy after login

Restore the master database data to the slave database

> mysql -uroot -p -S /mysql-instance/3308/mysql.sock < /data/mysql_bak.2017-01-15.sql
Copy after login

5. Log in to the slave database and configure the replication parameters

CHANGE MASTER TO

MASTER_HOST=&#39;127.0.0.1&#39;,

MASTER_PORT=3306,

MASTER_USER=&#39;rep&#39;,

MASTER_PASSWORD=&#39;123456&#39;,

MASTER_LOG_FILE=&#39;mysql-bin.000001&#39;,

MASTER_LOG_POS=396;
Copy after login

Note that the above MASTER_LOG_FILE and MASTER_LOG_POS are used in the master database using show master status; View information.

View the master.info file

> cat /mysql-instance/3308/data/master.info
Copy after login

6. Start the slave library synchronization switch and test the master-slave replication situation

> mysql -uroot -p -S /mysql-instance/3308/mysql.sock -e "start slave;"

> mysql -uroot -p -S /mysql-instance/3308/mysql.sock -e "show slave status\G;"

> mysql -uroot -p -S /mysql-instance/3308/mysql.sock -e "show slave status\G" | egrep "IO_Running|SQL_Running|_Behind_Master"
Copy after login

7. Test the master-slave replication

> mysql -uroot -p -e "create database wohehe;"

> mysql -uroot -p -S /mysql-instance/3308/mysql.sock -e "show databases;"
Copy after login

五、mysql主从复制线程状态说明及用途

1、主库线程的同步状态

> show processlist\G; 


*************************** 1. row ***************************

   Id: 5

  User: rep

  Host: localhost:47605

   db: NULL

Command: Binlog Dump

  Time: 4728

 State: Master has sent all binlog to slave; waiting for binlog to be updated

  Info: NULL
Copy after login

说明主库线程已从binlog读取更新,发送到了从库,线程处理空闲状态,等待binlog的事件更新。

2、从库线程的同频状态

> show processlist\G; 

*************************** 2. row ***************************

   Id: 6

  User: system user

  Host:

   db: NULL

Command: Connect

  Time: 5305

 State: Slave has read all relay log; waiting for the slave I/O thread to update it

  Info: NULL
Copy after login

说明从库已读取所有中继日志,等待从库I/O线程的更新。

六、主从复制故障

如果我在从库上创建了一个库,然后去主库创建同名的库,那么这就会冲突了。

> show slave status; 

Slave_IO_Running: Yes

Slave_SQL_Running: No

Seconds_Behind_Master: NULL

Last_Error: Error &#39;Can&#39;t create database &#39;xxxxx&#39;; database exists&#39; on query. Default database: &#39;xxxxx&#39;. Query: &#39;create database xxxxx&#39;
Copy after login

对于该冲突解决方法

方法一

> stop slave;

#将同步指针移动下一个,如果多次不同步,可重复操作

> set global sql_slave_skip_counter = 1;

> start slave;
Copy after login

方法二

> vi /mysql-instance/3308/my.cnf 

#把可以忽略的错误号事先在配置文件中配置

slave-skip-errors = 1002,1007,1032
Copy after login

The above is the detailed content of Detailed explanation of the sample code sharing of master-slave replication of mysql5.6 under centos7. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template