Mysql master-slave synchronization data construction (one master and one slave)

1. [Main Library] Configuration
1. [Main Library] Configuration Modification

######################################
# Binary Logging.
# log-bin 二进制日志
log-bin=mysql-bin
#只保留7天的二进制日志,以防磁盘被日志占满
expire-logs-days=7
#不备份的数据库
binlog-ignore-db  = mysql       
binlog-ignore-db  = information_schema
binlog-ignore-db  = performation_schema
binlog-ignore-db  = sys
#需要做复制的数据库名,如果有多个,复制binlog-do-db即可
binlog-do-db      = test 
#######################################

2. Create and authorize users
. Create slave users for synchronous replication from the database and grant permissions for replication and synchronous access.

mysql> CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
Query OK, 0 rows affected (0.00 sec)

3. View information
3.1 User information

SELECT * FROM mysql.user;

Insert image description here
3. 2 log_bin status
Insert image description here
3. 3 View master status
Insert image description here
2. [Slave library] configuration
1. Slave library configuration
Insert image description here
2. Configure master library information from the slave library

Insert image description here

mysql>stop slave 
mysql>CHANGE MASTER TO MASTER_HOST='localhost',MASTER_PORT=3306,MASTER_USER='slave',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000100',MASTER_LOG_POS=617;

3. Check the synchronization status
Insert image description here
slave_io_status status is empty, forget to turn on start slave; check the status after turning it on.

mysql>start slave

Insert image description here
4. Notes
Insert image description here
4.1 The function of I/O is to fetch its binlog from the master 3306 port (the content modified by the master will be written to its own binlog waiting for the slave to update), and then written to the local relay-log, and the SQL thread It is to read the local relay-log, and then convert it into a statement that Mysql can understand, so the synchronization is completed step by step. When encountering one of the reasons for data synchronization failure, the troubleshooting idea is to see whether these two values ​​​​are Yes. .
After starting slave, the SLAVE_IO_RUNNING value is Yes and SLAVE_SQL_RUNNING is Yes.
4.2 After creating a new database test in the main database, create a new t_user table and check the slave database. No new database or new table was found. Check show slave status; SLAVE_SQL_RUNNING is NO. Reason: The master never synchronizes the database, so before synchronization, ensure that the slave database manually creates the test database. If SLAVE_SQL_RUNNING is NO, after the slave database creates the synchronization database test, and after adding data in the master database t_user, the slave database does not Synchronous Data. Therefore, before synchronization, change SLAVE_SQL_RUNNING to Yes. You can execute the following three commands, which can be executed repeatedly. The value is changed to Yes after repeated execution twice. The slave database manually creates the table t_user, and the main database adds data in t_user. , data synchronization from the database t_user table was successful.

mysql>stop slave;
mysql>set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
mysql>start slave;

Insert image description here
5. Synchronous test:
Create a new test library in the main library, and the slave library will automatically synchronize.
Insert image description here
You can test by creating a new table, adding, deleting, modifying and checking data. If the data is not synchronized during the test, use the command show slave status; check whether SLAVE_SQL_RUNNING is NO and the reason for NO:
Insert image description here

Guess you like

Origin blog.csdn.net/FORLOVEHUAN/article/details/128879556