MySQL 中繼日誌已損壞,如何修復?試過但失敗了
當機器突然關閉時,MySQL v5.1.61 中繼損壞。我試圖修復它,但它沒有用。
- 我如何解決它?我做錯什麼了嗎?
據我所知,損壞的 MySQL 中繼日誌很容易修復:
change master to master_log_file='<Relay_Master_Log_File>', master_log_pos=<Exec_Master_Log_Pos>;
其中
Relay_Master_Log_File
和Exec_Master_Log_Pos
由以下人員列出:
mysql> show slave status;
但是,當我這樣做時
change master status ...
,我收到了主鍵違規錯誤。這怎麼可能?上述程序是不正確的,還是缺少一些+1?(現在我只是簡單地將 –master-data mysqldump 從主伺服器重新導入到從伺服器,這解決了問題。但是,將來這樣做可能不合適。)
以下是有關我的特定問題的詳細資訊:
mysql> show slave status \G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: the-master-host Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000021 Read_Master_Log_Pos: 33639968 Relay_Log_File: mysql-relay-bin.000271 Relay_Log_Pos: 2031587 Relay_Master_Log_File: mysql-bin.000020 Slave_IO_Running: Yes Slave_SQL_Running: No Replicate_Do_DB: the_database Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 1594 Last_Error: Relay log read failure: Could not parse relay log event entry. The possible reasons are: the master's binary log is corrupted (you can check this by running 'mysqlbinlog' on the binary log), the slave's relay log is corrupted (you can check this by running 'mysqlbinlog' on the relay log), a network problem, or a bug in the master's or slave's MySQL code. If you want to check the master's binary log or slave's relay log, you will be able to know their names by issuing 'SHOW SLAVE STATUS' on this slave. Skip_Counter: 0 Exec_Master_Log_Pos: 66395191 Relay_Log_Space: 36559177 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: 1594 Last_SQL_Error: Relay log read failure: Could not parse relay log event entry. The possible reasons are: the master's binary log is corrupted (you can check this by running 'mysqlbinlog' on the binary log), the slave's relay log is corrupted (you can check this by running 'mysqlbinlog' on the relay log), a network problem, or a bug in the master's or slave's MySQL code. If you want to check the master's binary log or slave's relay log, you will be able to know their names by issuing 'SHOW SLAVE STATUS' on this slave.
這就是我所做的:
mysql> stop slave; mysql> reset slave; mysql> change master to master_host='the-master-host', master_user='replication', master_password='the-password', master_log_file='mysql-bin.000020', master_log_pos=66395191; mysql> start slave;
這就是發生的事情,一個PK錯誤:
131122 15:17:29 [Note] Slave I/O thread: connected to master 'replication@the-master-host:3306',replication started in log 'mysql-bin.000020' at position 66395191 131122 15:17:29 [ERROR] Slave SQL: Error 'Duplicate entry '71373' for key 'PRIMARY'' on query. Default database: 'the_database'. Query: 'insert into ... values ...', Error_code: 1062 131122 15:17:29 [Warning] Slave: Data truncated for column 'date' at row 1 Error_code: 1265 131122 15:17:29 [Warning] Slave: Duplicate entry '71373' for key 'PRIMARY' Error_code: 1062
我想我遵循了推薦的程序(請參見下面的連結),仍然存在 PK 錯誤:-( ? http://bugs.mysql.com/bug.php?id=26489,搜尋“解決方法” 。http: //mhbarr.wordpress.com/2013/07/26/mysql-slave-corrupted-relay-log/ https://stackoverflow.com/a/14438408
錯誤:Last_SQL_Errno:1594 Last_SQL_Error:中繼日誌讀取失敗:無法解析中繼日誌事件條目。
此錯誤表示主日誌文件已損壞或中繼日誌文件已損壞。
- 在做任何事情之前備份你所有的數據庫、日誌、圖像伺服器,重複幾次,並且只有你自己承擔風險。
首先在從站上執行“顯示從站狀態\G”並註意:
Master_Log_File: mysql-bin.000026 Read_Master_Log_Pos: 2377104 Relay_Log_File: mysqld-relay-bin.000056 Relay_Log_Pos: 1097303 Relay_Master_Log_File: mysql-bin.000026 Exec_Master_Log_Pos: 1097157
首先我們要確保主日誌文件完好無損,所以跳轉到主伺服器並找到 Relay_Master_Log_File(檢查 /var/log/mysql)並執行以下命令:
mysqlbinlog mysql-bin.000026
將顯示日誌,但希望您不會看到任何錯誤消息。如果您看到錯誤消息,則說明主日誌已損壞,您可能需要重新映像。
接下來在從屬中繼日誌上執行相同的命令(通常在 /var/lib/mysql 中)
mysqlbinlog mysqld-relay-bin.000056
您可能會看到一些錯誤,顯示已停止複制的損壞,如下所示:
ERROR: Error in Log_event::read_log_event(): 'read error', data_len: 336, event_type: 2 ERROR: Could not read entry at offset 1097414: Error in log format or read error. DELIMITER ; # End of log file ROLLBACK /* added by mysqlbinlog */; /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/; root@db:/var/lib/mysql#
如果您看到任何錯誤,則主伺服器上的日誌很好,只有從伺服器的中繼日誌損壞。這是個好消息,我們可以重置從站並告訴它主站的詳細資訊以及從哪裡繼續。如果你沒有看到任何錯誤,那麼現在停止閱讀,你有一個不同的問題。
如果slave中繼日誌有錯誤,執行以下命令重置slave和損壞的日誌重新連接到master,獲取ok日誌並重新開始slaving。請注意, MASTER_LOG_POS 是
Exec_Master_Log_Pos
,並且 MASTER_LOG_FILE 是Relay_Master_Log_File
(不是第一個,它匹配已獲取並需要丟棄的中繼日誌)都來自第一個命令。mysql> stop slave; Query OK, 0 rows affected (0.14 sec) mysql> reset slave all; Query OK, 0 rows affected (0.43 sec) mysql> CHANGE MASTER TO MASTER_HOST='master.host.com', MASTER_USER='masteruser', MASTER_PASSWORD='masterpass', MASTER_LOG_FILE='mysql-bin.000026', MASTER_LOG_POS=1097157; Query OK, 0 rows affected (0.93 sec) mysql> start slave; Query OK, 0 rows affected (0.00 sec)
$$ Fixing MySQL replication after slaves’ relay log was corrupted $$
從屬伺服器(5.XX 版)上的 MySQL 複製已停止。Slave_IO_Running 標記為 Yes,但 Slave_SQL_Running 標記為 No。簡單的停止/啟動從站沒有幫助,因此需要進一步分析問題。似乎目前從站的中繼日誌已損壞,因為使用“mysqlbinlog”進行測試已列印出錯誤。因此,解決方案是丟棄目前的中繼 binlog,並將 slave 指向最後一個 master binlog 位置。
要修復此錯誤,應丟棄從站上的目前 binlog 文件並設置新位置。在設置新的 binlog 位置之前,請務必使用命令SHOW SLAVE STATUS \G記住損壞的從伺服器中的Relay_Master_Log_File和Exec_Master_Log_Pos值:
Relay_Master_Log_File: mysql-bin.002045 Exec_Master_Log_Pos: 103641119
好的,有了這個值,可以設置新的 binlog 位置:
# stop slave mysql> stop slave; # make slave forget its replication position in the master's binary log mysql> reset slave; # change slave to start reading from stopped position mysql> change master to master_log_file='mysql-bin.002045', master_log_pos=103641119; # start slave mysql> start slave;
請注意,這
reset slave
將刪除master.info
和relay-log.info
所有中繼日誌文件,因此不需要清理目錄中的剩余/var/lib/mysql
文件。