標籤:style blog http color 使用 os
在生產環境中,主從複製常常會有複寫延遲的現象,主要是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/# slaves_psswd=123456s_user=roots_port=3306s_host=localhost# masterm_psswd=123456m_user=rootm_port=3306m_host=192.168.0.102slave_wan_ip=`ifconfig | sed -n ‘/inet /{s/.*addr://;s/ .*//;p}‘ | head -n1`while truedo 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 fidone
如果你覺得判斷的標準或者指令碼還不夠完善,可以相互交流一下。^_^