版本:MySQL5.7.22
一、報錯現象
dba:(none)> start slave;
ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository
這個時候檢視error.log:
2018-08-07T09:28:12.605775Z 0 [ERROR] Failed to open the relay log './localhost-relay-bin.000001' (relay_log_pos 4).
2018-08-07T09:28:12.605792Z 0 [ERROR] Could not find target log file mentioned in relay log info in the index file '/application/mysql/relay-log/mysql-relay-bin.index' during relay log initialization.
2018-08-07T09:28:12.606062Z 0 [ERROR] Slave: Failed to initialize the master info structure for channel ''; its record may still be present in 'mysql.slave_master_info' table, consider deleting it.
2018-08-07T09:28:12.606298Z 0 [ERROR] Failed to create or recover replication info repositories.
2018-08-07T09:28:12.606316Z 0 [Note] Some of the channels are not created/initialized properly. Check for additional messages above. You will not be able to start replication on those channels until the issue is resolved and the server restarted.
那如何解決呢?先來簡單的了解MySQL Relay log的基礎知識:從報錯上看,意思是啟動slave時,使用repository中資訊初始化relay log結構失敗了。為什麼失敗了?原來是從mysql-relay-bin.index檔案中找不到localhost-relay-bin.000001檔案。到這裡,答案就很清楚了,由于我使用的是冷備份檔案恢複的執行個體,在mysql庫中的slave_relay_log_info表中依然保留之前relay_log的資訊,是以導緻啟動slave報錯。
二、MySQL Relay log介紹
在MySQL複制結構下,Slave伺服器會産生三種日志檔案,用來儲存主庫的二進制日志事件以及relay log已執行到的位置和狀态。
1、relay log 檔案:由IO thread線程從主庫讀取的二進制日志事件組成,該日志被Slave上的SQL thread線程執行,進而實作資料的複制。
2、master info log:該檔案儲存slave連接配接master的狀态以及配置資訊,如使用者名,密碼,日志執行的位置等。在5.6版本之前,都是使用master.info檔案,從5.6開始,通過在my.cnf 中配置 --master-info-repository=TABLE。這些資訊會被寫入mysql.slave_master_info 表中,代替原來的master.info檔案了。
3、relay log info log:該檔案儲存slave上relay log的執行位置。在5.6版本之前,都是使用relay-log.info檔案,從5.6開始,通過在my.cnf中配置 --relay-log-info-repository=TABLE,使用mysql.slave_relay_log_info表代替原來的檔案。每次當slave上執行start slave時,就會讀取該表中的位置資訊。
新版本使用表來代替原來的檔案,主要為了crash-safe replication,進而大大提高從庫的可靠性。為了保證意外情況下從庫的可靠性,mysql.slave_master_info和mysql.slave_relay_log_info表必須為事務性的表,從5.6.6起,這些表預設使用InnoDB存儲引擎。在5.6.5及之前的版本預設使用MyISAM引擎,可用下面語句進行轉換:
ALTER TABLE mysql.slave_master_info ENGINE=InnoDB;
ALTER TABLE mysql.slave_relay_log_info ENGINE=InnoDB;
【注意】不要試圖手工的更新、插入、删除以上兩個表的内容,以免出現意料不到的問題。
三、問題解決
通過上面的報錯以及relay log介紹,很容易知道由于mysql.slave_relay_log_info表中保留了以前的複制資訊,導緻新從庫啟動時無法找到對應檔案,那麼我們清理掉該表中的記錄不就可以了。再次提醒,不要手動删該表資料,MySQL已經提供工具給我們了:reset slave:
reset slave幹的那些事:
1、删除slave_master_info ,slave_relay_log_info兩個表中資料;2、删除所有relay log檔案,并重新建立新的relay log檔案;3、不會改變gtid_executed 或者 gtid_purged的值
下面解決問題:
1,
dba:(none)> reset slave;
Query OK, 0 rows affected (0.00 sec)
2,
dba:(none)> change master to ......
3,
dba:(none)> start slave;
Query OK, 0 rows affected (0.00 sec)
到這裡問題解決了。