mysql 主從復制如何跳過報錯
一、傳統binlog主從復制,跳過報錯方法
mysql> stop slave;mysql> set global sql_slave_skip_counter = 1;mysql> start slave;mysql> show slave status G
二、GTID主從復制,跳過報錯方法
mysql> stop slave; #先關閉slave復制;mysql> change master to ...省略... #配置主從復制;mysql> show slave statusG #查看主從狀態;
發現報錯:
mysql> show slave statusG*************************** 1. row ***************************Slave_IO_State: Waiting for master to send event Master_Host: 172.19.195.212 Master_User: master-slave Master_Port: 3306Connect_Retry: 60 Master_Log_File: mysql-bin.000021 Read_Master_Log_Pos: 194Relay_Log_File: nginx-003-relay-bin.000048Relay_Log_Pos: 454 Relay_Master_Log_File: mysql-bin.000016 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 ’code’; database exists’ on query. Default database: ’code’. Query: ’create database code’ Skip_Counter: 0 Exec_Master_Log_Pos: 8769118 Relay_Log_Space: 3500 Until_Condition: NoneUntil_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: NULLMaster_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 1007Last_SQL_Error: Error ’Can’t create database ’code’; database exists’ on query. Default database: ’code’. Query: ’create database code’ Replicate_Ignore_Server_Ids:Master_Server_Id: 100 Master_UUID: fea89052-11ef-11eb-b241-00163e00a190 Master_Info_File: /usr/local/mysql/data/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: 201022 09:31:29Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: fea89052-11ef-11eb-b241-00163e00a190:8-5617 Executed_Gtid_Set: a56c9b04-11f1-11eb-a855-00163e128853:1-11224,fea89052-11ef-11eb-b241-00163e00a190:1-5614Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.01 sec)
可以看到 Slave_SQL_Running 為 NO,表示運行取回的二進制日志出了問題;在 Last_Error 中也可以看到大概的報錯;(因為我之前的操作,大概可以判斷出 是因為主庫的二進制日志中有創建code庫的sql,而從庫上我已經創建了這個庫,應該是產生了沖突;)
解決方法:
1、如果清楚自己之前的操作,可以將從庫中產生沖突的庫刪除;2、或者通過跳過GTID報錯的事務的方法
--- 通過 Last_SQL_Errno 報錯編號查詢具體的報錯事務mysql> select * from performance_schema.replication_applier_status_by_worker where LAST_ERROR_NUMBER=1007G*************************** 1. row *************************** CHANNEL_NAME: WORKER_ID: 0 THREAD_ID: NULL SERVICE_STATE: OFFLAST_SEEN_TRANSACTION: fea89052-11ef-11eb-b241-00163e00a190:5615 LAST_ERROR_NUMBER: 1007 LAST_ERROR_MESSAGE: Error ’Can’t create database ’code’; database exists’ on query. Default database: ’code’. Query: ’create database code’ LAST_ERROR_TIMESTAMP: 2020-10-22 09:31:291 row in set (0.00 sec)mysql> stop slave;Query OK, 0 rows affected (0.00 sec)--- 跳過查找到報錯的事務(LAST_SEEN_TRANSACTION 的值)mysql> set @@session.gtid_next=’fea89052-11ef-11eb-b241-00163e00a190:5615’;Query OK, 0 rows affected (0.00 sec)mysql> begin;Query OK, 0 rows affected (0.00 sec)--- 提交一個空的事務,因為設置gtid_next后,gtid的生命周期開始了,必須通過顯性的提交一個事務來結束;mysql> commit;Query OK, 0 rows affected (0.00 sec)--- 設置回自動模式;mysql> set @@session.gtid_next=automatic;Query OK, 0 rows affected (0.00 sec)mysql> start slave;Query OK, 0 rows affected (0.00 sec)
通過以上步驟,就跳過了這次的GTID報錯的事務,如果 start slave 之后還是有報錯,那么就按照此步驟繼續跳過;
經驗豐富的話,基本不用查詢事務,通過 Executed_Gtid_Set 就可以判斷出報錯的事務是 fea89052-11ef-11eb-b241-00163e00a190:5615 了;因為執行事務,到 fea89052-11ef-11eb-b241-00163e00a190:1-5614 的時候報錯了,應該可以判斷是 5615事務出現的錯誤;
以上就是mysql 主從復制如何跳過報錯的詳細內容,更多關于MySQL 跳過報錯的資料請關注好吧啦網其它相關文章!
相關文章:
