• mysql主从复制延时判断+脚本检查


    在生产环境中,主从复制常常会有复制延迟的现象,主要是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
    复制代码

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

    作者:Atlas

    出处:Atlas的博客 http://www.cnblogs.com/gomysql

    您的支持是对博主最大的鼓励,感谢您的认真阅读。本文版权归作者所有,欢迎转载,但请保留该声明

  • 相关阅读:
    CefSharp应用——High DPI问题
    CefSharp应用——程序输出
    CefSharp应用——环境搭建
    QTTabBar加载项被禁用
    OCR 中文汉字识别,可用于文档识别,身份证识别,名片识别,采用字库+卷积神经网络
    springboot中Thymeleaf和Freemarker模板引擎的区别
    一种mysql 实现用户前两条语句方案
    Elasticsearch java.lang.ClassNotFoundException: org.elasticsearch.common.transport.InetSocketTransportAddress
    版本6.2.4的elasticsearch包里面没有InetSocketTransportAddress
    ES spring数据JPA&spring data elasticsearch;找不到类型的属性索引
  • 原文地址:https://www.cnblogs.com/cyt1153/p/6557129.html
Copyright © 2020-2023  润新知