MySQL運維-主從復制心跳(MASTER_HEARTBEAT_PERIOD)


1 心跳參數簡介
    設置復制心跳的周期,取值范圍為0 到 4294967秒。精確度可以達到毫秒,最小的非0值是0.001秒。心跳信息由master在主機binlog日志文件在設定的間隔時間內沒有收到新的事件時發出,以便slave知道master是否正常。
    slave連接到master后,該參數可通過mysql.slave_master_info表查看。
mysql> select * from mysql.slave_master_info \G

    默認值為slave_net_timeout的值除以2,設置為0表示完全的禁用心跳。
2 詳細使用介紹
    在 MySQL 主從復制時,有時候會碰到這樣的故障:在 Slave 上 Slave_IO_Running 和 Slave_SQL_Running 都是 Yes,Slave_SQL_Running_State 顯示 Slave has read all relay log; waiting for the slave I/O thread to update it ,看起來狀態都正常,但實際卻滯后於主,Master_Log_File 和 Read_Master_Log_Pos 也不是實際主上最新的位置。一種可能是 Master 上的 binlog dump 線程掛了。但有時候,在 Master 上檢查也是完全正常的。那 Slave 的延誤又是怎么造成的呢?
    在 MySQL 的復制協議里,由 Slave 發送一個 COM_BINLOG_DUMP 命令后,就完全由 Master 來推送數據,Master、Slave 之間不再需要交互。如果 Master 沒有更新,也就不會有數據流,Slave 就不會收到任何數據包。但是如果由於某種原因造成 Master 無法把數據發送到 Slave ,比如發生過網絡故障或其他原因導致 Master 上的 TCP 連接丟失,由於 TCP 協議的特性,Slave 沒有機會得到通知,所以也沒法知道收不到數據是因為 Master 本來就沒有更新呢還是由於出了故障。
    MySQL5.5提供的新的配置master_heartbeat_period。即復制心跳,能夠在復制停止工作和出現網絡中斷的時候幫助我們迅速發現問題。
    啟用方法:
mysql> stop slave;
mysql> change master to master_heartbeat_period = 10;
mysql> start slave;
    slave_net_timeout 的默認是 3600,也就是一小時。也就是說,在之前的情況下,Slave 要延誤 1 小時后才會嘗試重連。而在沒有設置 master_heartbeat_period 時,將 slave_net_timeout 設得很短會造成 Master 沒有數據更新時頻繁重連。
    所以推薦設置為:
mysql> stop slave;
mysql> change master to master_heartbeat_period = 10;
mysql> set global slave_net_timeout = 25;
mysql> start slave;
   Master 在沒有數據的時候,每 10 秒發送一個心跳包。這樣 Slave 就能知道 Master 是不是還正常。slave_net_timeout 是設置在多久沒收到數據后認為網絡超時,之后 Slave 的 IO 線程會重新連接 Master 。結合這兩個設置就可以避免由於網絡問題導致的復制延誤。master_heartbeat_period 單位是秒,可以是個帶上小數,如 10.5。最高精度為 1 毫秒。
    當前的 master_heartbeat_period 值無法通過 show slave status 查看,而要使用 show status like ‘Slave_heartbeat_period' 查看。此外,狀態變量 Slave_last_heartbeat 表示最后一次收到心跳的時間,Slave_received_heartbeats 表示總共收到的心跳次數。
mysql> show status like 'slave%';
+----------------------------+---------------------+
| Variable_name | Value |
+----------------------------+---------------------+
| Slave_heartbeat_period | 10.000 |
| Slave_last_heartbeat | 2015-01-08 10:28:16 |
| Slave_open_temp_tables | 0 |
| Slave_received_heartbeats | 1645 |
| Slave_retried_transactions | 0 |
| Slave_running | ON |
+----------------------------+---------------------+

    原文地址:http://blog.csdn.net/jesseyoung/article/details/42914577


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM