• 主从复制延时判断


    在生产环境中,主从复制常常会有复制延迟的现象,主要是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 statusG" | 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 statusG" | 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 statusG" | 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 statusG" | grep -w Exec_Master_Log_Pos | awk -F": " '{print $2}'|sed 's/[ 	]*$//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/[ 	]*$//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_ipe[0m"
                    echo -e "e[1;31mMaster当前binlog: $Master_File"
                    echo -e "e[1;31mSlave当前binlog:  $Master_Log_File"
                    echo -e "e[1;31mbinlog相差文件数: $log_counte[0m"
                    echo -e "e[1;31mPos点相差:        $pos_counte[0m"
                fi
            fi
        fi
    done

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

  • 相关阅读:
    【二次开发】shopxo商城
    经典的PHPer为什么被认为是草根?
    PowerDesigner生成Access数据库
    NET开发学习项目资源
    ASP.NET程序开发范例宝典
    Web.Config文件配置小记
    NET开发学习项目资源(2)
    VS 2010一步步开发windows服务(windows service)
    jquery.mobile手机网页简要
    Android置底一个View后运行报错
  • 原文地址:https://www.cnblogs.com/gomysql/p/3862018.html
Copyright © 2020-2023  润新知