故障描述
自己搭建了一套.7.26的主從復制環境,有2個多月沒怎么管它,今天上去想要做一個因為主鍵沖突,導致失敗的測試,發現mysql主從復制已經斷開了,上去一看報了1236錯誤,詳細錯誤信息如下所示:
[root@localhost] 16:54:01 [testdb1]>show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.112.131
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000019
Read_Master_Log_Pos: 194
Relay_Log_File: mysql-relay-bin.000037
Relay_Log_Pos: 4
Relay_Master_Log_File: binlog.000019
Slave_IO_Running: No
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: 194
Relay_Log_Space: 944
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: 1236
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'The slave is connecting using CHANGE MASTER TO MASTER_AUTO_POSITION = 1, but the master has purged binary logs containing GTIDs that the slave requires.'
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 111213106
Master_UUID: 3ada166e-c4db-11ea-b21d-000c29cc2388
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp: 200902 16:54:01
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: 3ada166e-c4db-11ea-b21d-000c29cc2388:1-84762,
3ada166e-c4db-11ea-b21d-000c29cc2389:1-7
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
: Got fatal error 1236 from when data from log: 'The slave is using TO = 1, but the has logs GTIDs that the slave .',
看這個報錯信息,是由于復制需要的 logs被刪除了,所以導致主從復制失敗,去主庫上查看,日志已經沒有了,這也就無法正常啟動復制進程了。
解決方案
到這里,有2個解決方案
第一:重新搭建主從復制,在主庫上做個全備,在從上庫上恢復,然后配置主從復制
第二:由于是測試環境數據庫一致性錯誤修復,數據的一致性不太重要,不用主從的復制的數據保持一致了,可以清除已經刪除的 logs日志的gtid信息,重啟主從復制。
方法如下所示:
1.在主庫上獲取信息
[root@localhost] 17:24:12 [(none)]> show global variables like '%gtid%';
+----------------------------------+----------------------------------------------+
| Variable_name | Value |

+----------------------------------+----------------------------------------------+
| binlog_gtid_simple_recovery | ON |
| enforce_gtid_consistency | ON |
| gtid_executed | 3ada166e-c4db-11ea-b21d-000c39cc2398:1-74621 |
| gtid_executed_compression_period | 1000 |
| gtid_mode | ON |
| gtid_owned | |
| gtid_purged | 3ada166e-c4db-11ea-b21d-000c29cc2388:1-84829 |
| session_track_gtids | OFF |
+----------------------------------+----------------------------------------------+
8 rows in set (0.00 sec)
2.登錄從庫數據庫一致性錯誤修復,停止slave服務,重置slave
[root@localhost] 17:02:31 [testdb1]>stop slave;
Query OK, 0 rows affected (0.00 sec)
[root@localhost] 17:03:40 [testdb1]>reset slave;
Query OK, 0 rows affected (0.01 sec)
[root@localhost] 17:03:44 [testdb1]>set @@global.gtid_purged='3ada166e-c4db-11ea-b21d-000c29cc2388:1-84829';
Query OK, 0 rows affected (0.00 sec)
[root@localhost] 17:03:48 [testdb1]>start slave;
Query OK, 0 rows affected (0.02 sec)
[root@localhost] 17:03:56 [testdb1]>show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.112.131
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000029
Read_Master_Log_Pos: 1881
Relay_Log_File: mysql-relay-bin.000003
Relay_Log_Pos: 405
Relay_Master_Log_File: binlog.000029
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: 1881
Relay_Log_Space: 612
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: 111213106
Master_UUID: 3ada166e-c4db-11ea-b21d-000c29cc2388
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
至此,主從復制已經恢復正常,當然在生產上不建議使用此方法,因為此方法,會導致主從數據不一致,在開發,測試環境是可以用的。
這個Mysql數據庫技能你get了吧。