使用参数SQL_SLAVE_SKIP_COUNTER处理mysql slave同步错误讨论
本文链接地址:http://blog.chinaunix.net/uid-31396856-id-5753206.html
本文讨论使用sql_slave_skip_counter 来使sql 线程跳过遇到错误的event记录
案例如下:
数据库数据无法同步,检查同步状态
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.21.28
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000010
Read_Master_Log_Pos: 137752106
Relay_Log_File: relay-bin.000004
Relay_Log_Pos: 670
Relay_Master_Log_File: master-bin.000009
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 1008
Last_Error: Error ‘Can‘t drop database ‘emodb‘; database doesn‘t exist‘ on query. Default database: ‘emodb‘. Query: ‘drop database emodb‘
Skip_Counter: 0
Exec_Master_Log_Pos: 1379
Relay_Log_Space: 320095465
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 1008
Last_SQL_Error: Error ‘Can‘t drop database ‘emodb‘; database doesn‘t exist‘ on query. Default database: ‘emodb‘. Query: ‘drop database emodb‘
Replicate_Ignore_Server_Ids:
Master_Server_Id: 28
1 row in set (0.00 sec)
发现在执行删除emodb的操作时候,发现备库不存在这个库。至于为什么备库已经没有了emo数据库的原因按下不表,继续处理往下:
首选关闭同步:
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.00 sec)
跳过这个错误的操作
MariaDB [(none)]> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;
Query OK, 0 rows affected (0.00 sec)
然后开启同步,检查同步是否能够正常进行。
MariaDB [(none)]>
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.21.28
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000010
Read_Master_Log_Pos: 137752106
Relay_Log_File: relay-bin.000004
Relay_Log_Pos: 761
Relay_Master_Log_File: master-bin.000009
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 1007
Last_Error: Error ‘Can‘t create database ‘confluence‘; database exists‘ on query. Default database: ‘confluence‘. Query: ‘create database confluence DEFAULT CHARACTER SET utf8‘
Skip_Counter: 0
Exec_Master_Log_Pos: 1470
Relay_Log_Space: 320096038
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 1007
Last_SQL_Error: Error ‘Can‘t create database ‘confluence‘; database exists‘ on query. Default database: ‘confluence‘. Query: ‘create database confluence DEFAULT CHARACTER SET utf8‘
Replicate_Ignore_Server_Ids:
Master_Server_Id: 28
1 row in set (0.00 sec)
按照上面的处理办法,继续处理
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]>
MariaDB [(none)]>
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.21.28
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000010
Read_Master_Log_Pos: 137752106
Relay_Log_File: relay-bin.000004
Relay_Log_Pos: 57581283
Relay_Master_Log_File: master-bin.000009
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 57581992
Relay_Log_Space: 320096611
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 83248
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 28
1 row in set (0.00 sec)
okay了,继续观察
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.21.28
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000010
Read_Master_Log_Pos: 137752193
Relay_Log_File: relay-bin.000008
Relay_Log_Pos: 617
Relay_Master_Log_File: master-bin.000010
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 137752193
Relay_Log_Space: 1190
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 28
1 row in set (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| confluence |
| dbyang |
| mysql |
| performance_schema |
| temp |
+--------------------+
6 rows in set (0.00 sec)
在测试下
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| confluence |
| mysql |
| performance_schema |
| temp |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.21.28
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000010
Read_Master_Log_Pos: 137752278
Relay_Log_File: relay-bin.000008
Relay_Log_Pos: 702
Relay_Master_Log_File: master-bin.000010
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 137752278
Relay_Log_Space: 1275
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 28
1 row in set (0.00 sec)
主从虽然开始同步,但是还需要检查主从数据是否一致,但这不在本文讨论范围内。
说明:
1、参数SQL_SLAVE_SKIP_COUNTER=n 是跳过n个事务继续进入后面的事务同步。这里是主库在进行drop database的操作的同步到slave,slave已经没有drop的database,这种情况下进行跳过,不影响主从的不一致。
2、如果轻易的使用容易造成主从不一致,需要谨慎的使用,使用前应该具体问题具体分析。
3、如果需要使用SQL_SLAVE_SKIP_COUNTER,需要明确跳过的event是什么操作;
4、如果使用了参数SQL_SLAVE_SKIP_COUNTER,需要检查主从数据的一致性。
---the end