在生產環境中,主從複製經常會有複製延遲的現象,主要是master是併發的寫,而slave是單線程的應用relay log,因此會出現複製延時,在MySQL 5.6版本中有了基於庫的多線程複製。還有MariaDB的並行複製。可是咱們使用MySQL 5.5的版本也比較多。如何判斷複製是否延時呢?工具如今能夠使用的有pt-heartbeat,可是若是咱們本身明白怎麼樣判斷複製是否延時的話,本身寫簡單的shell腳本或者python腳本也能夠完成。python
複製是否延時的判斷標準以下:mysql
不要經過Seconds_Behind_Master去判斷,該值表示slave上SQL線程和IO線程之間的延遲sql
#!/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
若是你以爲判斷的標準或者腳本還不夠完善,能夠相互交流一下。^_^shell