复制是否延时的判断标准如下:
不要通过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
-
# 判断主从复制是否延迟
-
# http://www.cnblogs.com/xuanzhi201111/
-
-
# 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