mysql5.7之后的GTID复制模式一旦数据同步出错和之前的解决方案略有差异,这里就详细的记录一下。
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Master_Host: 172.31.7.252 Master_User: repl Master_Port: 20101 Connect_Retry: 60 Master_Log_File: mysqlbin.000003 Read_Master_Log_Pos: 2502950 Relay_Log_File: fe43c62cc3e0-relay-bin.000003 Relay_Log_Pos: 2482454 Relay_Master_Log_File: mysqlbin.000003 Slave_IO_Running: No 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: 1051 Last_Error: Error 'Unknown table 'test.test_table'' on query. Default database: 'test'. Query: 'DROP TABLE `test_table` /* generated by server */' Skip_Counter: 0 Exec_Master_Log_Pos: 2482243 Relay_Log_Space: 5543513 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: 1051 Last_SQL_Error: Error 'Unknown table 'test.test_table'' on query. Default database: 'test'. Query: 'DROP TABLE `test_table` /* generated by server */' Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: 14268a44-4eab-11e8-95fb-0242ac120002 Master_Info_File: /var/lib/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: 181102 08:08:52 Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: 14268a44-4eab-11e8-95fb-0242ac120002:1-5338 Executed_Gtid_Set: 14268a44-4eab-11e8-95fb-0242ac120002:1-5290:5292 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
上面是从库出错的状态,首先就是要拿【Exec_Master_Log_Pos】中的pos去主服务器中通过showlogs方式查询出错的语句分析原因。
mysql> show binary logs; +-----------------+-----------+ | Log_name | File_size | +-----------------+-----------+ | mysqlbin.000001 | 177 | | mysqlbin.000002 | 3039593 | | mysqlbin.000003 | 2510198 | +-----------------+-----------+ 3 rows in set (0.00 sec) mysql> show binlog events in 'mysqlbin.000003' from 2482243 limit 10; +-----------------+---------+------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +-----------------+---------+------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | mysqlbin.000003 | 2482243 | Gtid | 1 | 2482308 | SET @@SESSION.GTID_NEXT= '14268a44-4eab-11e8-95fb-0242ac120002:5291' | | mysqlbin.000003 | 2482308 | Query | 1 | 2482430 | use `test`; DROP TABLE `test_table` /* generated by server */ | | mysqlbin.000003 | 2482430 | Gtid | 1 | 2482495 | SET @@SESSION.GTID_NEXT= '14268a44-4eab-11e8-95fb-0242ac120002:5292' | | mysqlbin.000003 | 2482495 | Query | 1 | 2482979 | use `test`; CREATE TABLE `test_table` (`id` int AUTO_INCREMENT,`group_id` bigint,`user_id` bigint,`card` varchar(255),`nickname` varchar(255),`sex` varchar(255),`area` varchar(255),`level` varchar(255),`role` varchar(255),`age` int,`join_time` int,`last_sent_time` int,`unfriendly` boolean,`card_changeable` boolean,`created_at` timestamp NULL,`updated_at` timestamp NULL,`deleted_at` timestamp NULL , PRIMARY KEY (`id`)) | | mysqlbin.000003 | 2482979 | Gtid | 1 | 2483044 | SET @@SESSION.GTID_NEXT= '14268a44-4eab-11e8-95fb-0242ac120002:5293' | | mysqlbin.000003 | 2483044 | Query | 1 | 2483168 | use `test`; CREATE INDEX idx ON `test_table`(group_id, user_id) | | mysqlbin.000003 | 2483168 | Gtid | 1 | 2483233 | SET @@SESSION.GTID_NEXT= '14268a44-4eab-11e8-95fb-0242ac120002:5294' | | mysqlbin.000003 | 2483233 | Query | 1 | 2483312 | BEGIN | | mysqlbin.000003 | 2483312 | Table_map | 1 | 2483391 | table_id: 149 (test.group_messages) | | mysqlbin.000003 | 2483391 | Write_rows | 1 | 2483612 | table_id: 149 flags: STMT_END_F | +-----------------+---------+------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 10 rows in set (0.00 sec) mysql>
从上面可以看到是在14268a44-4eab-11e8-95fb-0242ac120002:5291中出现的错误,然后就得到了下一行的全局同步点【14268a44-4eab-11e8-95fb-0242ac120002:5292】。
拿到上面的同步点接下来的工作就是去slave进行跳过了。
mysql>stop slave; mysql>set GTID_NEXT='14268a44-4eab-11e8-95fb-0242ac120002:5292'; mysql>begin; mysql>commit; mysql>set GTID_NEXT='AUTOCOMMIT'; mysql>start slave;
依次执行上面命令就可以了。
不要通过slave status中的[Retrieved_Gtid_Set]字段自己自增猜测下一个同步时间点。两个pos略有不同,直接使用会报错。