Two solutions to mysql master slave database unsynchronization

  • 2020-05-14 05:08:21
  • OfStack

Today we found that Mysql's master and slave databases are not synchronized
Go to the Master library first:
mysql > show processlist; See if the process Sleep too much. That's normal.
show master status; Also normal.
mysql > show master status;
+-------------------+----------+--------------+-------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+-------------------------------+
| mysqld-bin.000001 | 3260 | | mysql,test,information_schema |
+-------------------+----------+--------------+-------------------------------+
1 row in set (0.00 sec)
Check again on Slave
mysql > show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: No
So Slave is out of sync

Here are two solutions:
Method 1: ignore the error and continue synchronization
This method is suitable for the cases where the difference between master and slave database data is not big, or where the data may not be completely unified, or where the data requirements are not strict
Solution:
stop slave;
# is an error to skip step 1, the number after which is variable
set global sql_slave_skip_counter =1;
start slave;
And then mysql > show slave status\G view:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
ok, master slave synchronization is now normal...

Approach 2: redo and follow, fully synchronized
This method is suitable for the cases where the master and slave database data differ greatly, or the data is required to be completely unified
The solution steps are as follows:
1. Advanced access to the main library to lock the table to prevent data writing
Command to use:
mysql > flush tables with read lock;
Note: the lock is read-only and the statement is case insensitive
2. Back up your data
Backup the data to mysql.bak.sql
[root@server01 mysql]#mysqldump -uroot -p -hlocalhost > mysql.bak.sql
Here note 1 point: database backup 1 must be carried out regularly, you can use shell script or python script, it is more convenient, to ensure that no data is lost
3. Check the master status
mysql > show master status;
+-------------------+----------+--------------+-------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+-------------------------------+
| mysqld-bin.000001 | 3260 | | mysql,test,information_schema |
+-------------------+----------+--------------+-------------------------------+
1 row in set (0.00 sec)
4. Transfer the mysql backup file to the slave machine for data recovery
Use the scp command
[root@server01 mysql]# scp mysql.bak.sql root@192.168.128.101:/tmp/
5. Stop the slave status
mysql > stop slave;
6. Then go to the slave library and execute the mysql command to import the data backup
mysql > source /tmp/mysql.bak.sql
7. Set synchronization from the slave library, and note the synchronization point there, which is | File| Position in the master library show master status information
change master to master_host = '192.168.128.100', master_user = 'rsync', master_port=3306, master_password='', master_log_file = 'mysqld-bin.000001', master_log_pos=3260;
8. Restart slave synchronization
mysql > stop slave;
9. Check the sync status
mysql > show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Ok, so we're done synchronizing.

Related articles: