码迷,mamicode.com
首页 > 其他好文 > 详细

主从复制延时判断

时间:2014-07-23 12:00:56      阅读:392      评论:0      收藏:0      [点我收藏+]

标签: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/

# 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

如果你觉得判断的标准或者脚本还不够完善,可以相互交流一下。^_^

主从复制延时判断,布布扣,bubuko.com

主从复制延时判断

标签:style   blog   http   color   使用   os   

原文地址:http://www.cnblogs.com/gomysql/p/3862018.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!