3 ways to resolve MySQL master-slave database out of sync

Source: Internet
Author: User
Tags mysql backup

3 ways to resolve MySQL master-slave database out of sync

Today, MySQL's master and slave databases are not synchronized.
first on the master library
Mysql>show processlist; See if the next process is too much sleep. Found to be 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)
Go to the slave and check it out.
Mysql> Show Slave Status\g
Slave_io_running:yes
Slave_sql_running:no
Visible is slave different steps

two workarounds are described below
method One: After ignoring the error, continue syncing
This method is applicable to the case that the data of master-slave database is not very different, or the data can not be completely unified, the data requirements are not strict.
Solve:
Stop slave;
#表示跳过一步错误, the following numbers are variable
Set global sql_slave_skip_counter = 1;
Start slave;
Then use mysql> Show slave status\g to view:
Slave_io_running:yes
Slave_sql_running:yes
OK, now the master-slave synchronization state is normal ...
mode two: Specify to skip the error code and continue syncing

Primary key conflicts, table already exist and other error codes, such as 1062,1032,1060, can be specified in the MySQL master configuration file to skip such exceptions and continue the next SQL synchronization, this can also avoid many master-slave synchronization of the abnormal interrupt

[Mysqld]

Slave-skip-errors = 1062,1032,1060

Restart MySQL

Service mysqld Restart

Then use mysql> Show slave status\g to view:
Slave_io_running:yes
Slave_sql_running:yes

Way Three: Re-master from, fully synchronized
This method is suitable for the case that the master-slave database data is large or the data is completely unified.
The steps to resolve are as follows:
1. Advanced Master Library, lock table, prevent data write
Use the command:
Mysql> flush tables with read lock;
Note: This is locked to a read-only state, and the statement is case insensitive
2. Perform data backup
#把数据备份到mysql. bak.sql file
[[email protected] MySQL] #mysqldump-uroot-p-hlocalhost > Mysql.bak.sql
Note here: Database backups must be done on a regular basis, with shell scripts or Python scripts, to make sure the data is foolproof
3. View 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. Upload the MySQL backup file to the slave library for data recovery
#使用scp命令
[Email protected] mysql]# SCP mysql.bak.sql [email protected]:/tmp/
5. Stop the state from the library
mysql> stop Slave;
6. Then run the MySQL command from the library, import the data backup
Mysql> Source/tmp/mysql.bak.sql
7. Set synchronization from the library, note that the synchronization point is in the main library show Master status information | file| Position two items
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. Re-open from sync
mysql> stop Slave;
9. View sync Status
Mysql> Show slave status\g View:
Slave_io_running:yes
Slave_sql_running:yes
OK, the sync is done.


This article is from the "struggling People" blog, please be sure to keep this source http://wodemeng.blog.51cto.com/1384120/1660027

3 ways to resolve MySQL master-slave database out of sync

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.