The solution to the inconsistent database master and slave data

Master: 

Check if there are too many sleep processes

show processlist;

View master status:

show master status;



slave

View status:

show slave status\G

Slave_IO_Running: Yes 
Slave_SQL_Running: No 

It can be seen that the slave is not synchronized.
Solution


Method 1: After ignoring the error, continue to synchronize. 
This method is suitable for situations where there is little difference between the master and slave database data, or the data may not be completely unified, and the
synchronization is stopped if the data requirements are not strict  :

stop slave; 

Indicates skipping a step error, the following number is variable:

set global sql_slave_skip_counter =1;

Start synchronization:

start slave;

Look at the status again:

Slave_IO_Running: Yes 
Slave_SQL_Running: Yes

Ok, now the master-slave synchronization status is normal. .

Method 2: Re-master and slave, complete synchronization. 
This method is suitable for situations where the data in the master and slave libraries are quite different, or the data is required to be completely unified. The 
solution steps are as follows: 
1. Enter the master library first, lock the table, and prevent data from being written:
lock table : 

mysql> flush tables with read lock; 

Note: This place is locked as read-only, and the statement is not case-sensitive 
. 2. Data backup:
#Back up the data to the mysql.bak.sql file 

mysqldump -uroot -p -hlocalhost > mysql.bak.sql

One thing to note here: database backups must be performed regularly. Shell scripts or python scripts can be used, which is more convenient to ensure that the data is foolproof 
. 3. View the master status:

show master status;

4. Transfer the mysql backup file to the slave machine for data recovery, using the scp command:

scp mysql.bak.sql [email protected]:/tmp/

5. Stop the state from the library 

stop slave;

6. Then execute the mysql command from the library to import the data backup 

mysql> source /tmp/mysql.bak.sql

7. Set the synchronization of the slave library, pay attention to the synchronization point there, which is the | File| Position in the show master status information of the master library 

change master to master_host = '192.168.128.100', master_user = 'root', master_port=3306, master_password='', master_log_file = 'mysqld-bin.000001', master_log_pos=3260; 

8. Restart slave synchronization 

stop slave;

9. View synchronization status 

show slave status\G

Slave_IO_Running: Yes 
Slave_SQL_Running: Yes 
OK, synchronization is complete.

Guess you like

Origin blog.csdn.net/qq_36850813/article/details/102746932