sync_master_info的一些說明

  • 2019 年 10 月 4 日
  • 筆記

官網:https://dev.mysql.com/doc/refman/5.7/en/replication-options-slave.html

默認我們MySQL5.6線上環境都是

master_info_repository = TABLE  relay_log_info_repository = TABLE

官方文檔上對 sync_master_info 描述如下:

The effects of this variable on a replication slave depend on whether the slaves master_info_repository is set to FILE or TABLE, as explained in the following paragraphs.

默認為10000。設置為1表示每個EVENT都要執行刷盤操作(注意不是每個事務!),為0表示有作業系統來決定何時刷盤。 對於 master_info_repository = TABLE 情況下,If the value of sync_master_info is greater than 0, the slave updates its master info repository table after every sync_master_info events. If it is 0, the table is never updated.

我們可以做個小實驗 搭建一套傳統複製的主從環境。

在從庫執行

set global sync_master_info=1;

然後,在主庫插入一條記錄。再到從庫去查看 select * from mysql.slave_master_info G 結果中的Master_log_pos欄位值。並執行show slave status G 查看Exec_Master_Log_Pos值。 可以重複操作幾次數據插入,可以發現和mysql.slave_master_info裡面是一致的。

然後,在從庫設置

set global sync_master_info=10000;

然後,在主庫插入一條記錄。再到從庫去查看 select from mysql.slave_master_info G 結果中的Master_log_pos欄位值。並執行show slave status G 查看Exec_Master_Log_Pos值。 可以重複操作幾次數據插入,可以發現show slave status G 和mysql.slave_master_info裡面對不上了。 show slave status G 展示的是基本實時的exec_master_log_pos數據。但是 select from mysql.slave_master_info G 裡面卻遲遲不更新。 這就是sync_master_info參數的功效。

說明:

如果set global sync_master_info=1的話,理論上複製會更安全,但是這樣的話執行每個event都要去update一次 mysql.slave_master_info,就增大了磁碟IO,因此我們一般在從庫也啟用binlog,這樣即便複製出問題了,根據從庫記錄下的最後的binlog資訊可以重新change master到主庫上。