在生產環境中,主從復制常常會有復制延遲的現象,主要是master是並發的寫,而slave是單線程的應用relay log,所以會出現復制延時,在MySQL 5.6版本中有了基於庫的多線程復制。還有MariaDB的並行復制。但是我們使用MySQL 5.5的版本也比較多。如何判斷復制是否延時呢?工具現在可以使用的有pt-heartbeat,但是如果我們自己明白怎么樣判斷復制是否延時的話,自己寫簡單的shell腳本或者python腳本也可以完成。
復制是否延時的判斷標准如下:
不要通過Seconds_Behind_Master去判斷,該值表示slave上SQL線程和IO線程之間的延遲
1、首先看 Relay_Master_Log_File 和 Master_Log_File 是否有差異
2、如果Relay_Master_Log_File 和 Master_Log_File 有差異的話,那說明延遲很大
3、如果Relay_Master_Log_File 和 Master_Log_File 沒有差異,再來看Exec_Master_Log_Pos 和 Read_Master_Log_Pos 的差異,那么更加嚴謹的做法是同時在主庫執行show master status和在從庫上面執行show slave status 的輸出進行比較。MHA就是這樣保證數據一致性的。MMM都沒有做到。這也算MHA比MMM更加優秀的地方。
so,根據上面的規則,我寫了簡單的shell腳本,如下:
#!/bin/bash # 判斷主從復制是否延遲 # write by yayun 2014-07-23 # http://www.cnblogs.com/gomysql/ # slave s_psswd=123456 s_user=root s_port=3306 s_host=localhost # master m_psswd=123456 m_user=root m_port=3306 m_host=192.168.0.102 slave_wan_ip=`ifconfig | sed -n '/inet /{s/.*addr://;s/ .*//;p}' | head -n1` while true do sleep 1 echo -e "\e[1;33m###################################\e[0m" Master_Log_File=$(mysql -u$s_user -p$s_psswd -h$s_host -P$s_port -e "show slave status\G" | grep -w Master_Log_File | awk -F": " '{print $2}') Relay_Master_Log_File=$(mysql -u$s_user -p$s_psswd -h$s_host -P$s_port -e "show slave status\G" | grep -w Relay_Master_Log_File | awk -F": " '{print $2}') Read_Master_Log_Pos=$(mysql -u$s_user -p$s_psswd -h$s_host -P$s_port -e "show slave status\G" | grep -w Read_Master_Log_Pos | awk -F": " '{print $2}') Exec_Master_Log_Pos=$(mysql -u$s_user -p$s_psswd -h$s_host -P$s_port -e "show slave status\G" | grep -w Exec_Master_Log_Pos | awk -F": " '{print $2}'|sed 's/[ \t]*$//g') Master_Log_File_Num=`echo $Master_Log_File | awk -F '.' '{print $2}' | sed 's/^0\+//'` Master_File=$(mysql -u$m_user -p$m_psswd -h$m_host -P$m_port -Nse "show master status" | awk '{print $1}') Master_Pos=$(mysql -u$m_user -p$m_psswd -h$m_host -P$m_port -Nse "show master status" | awk '{print $2}'|sed 's/[ \t]*$//g') Master_File_Num=`echo $Master_File | awk -F '.' '{print $2}' | sed 's/^0\+//'` if [ -z $Master_Log_File ] && [ -z $Relay_Master_Log_File ] && [ -z $Read_Master_Log_Pos ] && [ -z $Exec_Master_Log_Pos ] then echo -e "\e[1;31mSLAVE 沒有取到值,請檢查參數設置!\e[0m" exit 1 fi if [ $Master_Log_File = $Relay_Master_Log_File ] && [ $Read_Master_Log_Pos = $Exec_Master_Log_Pos ] then if [ $Master_Log_File = $Master_File ] && [ $Exec_Master_Log_Pos = $Master_Pos ] then echo -e "\e[1;32mMaster-slave 復制無延遲 ^_^\e[0m" else if [ $Master_Log_File_Num -gt $Master_File_Num ] || [ $Master_Pos -gt $Exec_Master_Log_Pos ] then log_count=$(expr $Master_Log_File_Num - $Master_File_Num) pos_count=$(expr $Master_Pos - $Exec_Master_Log_Pos) echo -e "\e[1;31mMaster-slave 復制延遲 !!!\e[0m" echo -e "\e[1;31mMaster:$m_host Slave:$slave_wan_ip\e[0m" echo -e "\e[1;31mMaster當前binlog: $Master_File" echo -e "\e[1;31mSlave當前binlog: $Master_Log_File" echo -e "\e[1;31mbinlog相差文件數: $log_count\e[0m" echo -e "\e[1;31mPos點相差: $pos_count\e[0m" fi fi fi done
如果你覺得判斷的標准或者腳本還不夠完善,可以相互交流一下。^_^