主从复制延时判断以及脚本

当遇到从库延迟时,要怎么做?
首先输入 show slave status G;看一下。
 
在生产环境中,主从复制常常会有复制延迟的现象,主要是master是并发的写,而slave是单线程的应用relay log,所以会出现复制延时,在MySQL 5.6版本中有了基于库的多线程复制。还有MariaDB的并行复制。但是我们使用MySQL 5.5的版本也比较多。如何判断复制是否延时呢?工具现在可以使用的有pt-heartbeat,但是如果我们自己明白怎么样判断复制是否延时的话,自己写简单的shell脚本或者python脚本也可以完成。
 
复制是否延时的判断标准如下:
              Master_Log_File: restoredb-bin.000013
          Read_Master_Log_Pos: 2421074
               Relay_Log_File: restoredb2-relay-bin.000010
                Relay_Log_Pos: 2419771
        Relay_Master_Log_File: restoredb-bin.000013
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 2421074
              Relay_Log_Space: 2420428
不要通过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更加优秀的地方。
 
 
主从复制延时判断-脚本
#!/bin/bash
# 判断主从复制是否延迟
 
# slave
s_psswd=chengce243
s_user=root
s_port=3306
s_host=172.16.0.125
 
# master
m_psswd=chengce243
m_user=root
m_port=3306
m_host=172.16.0.125
 
 
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
 
 
原文地址:https://www.cnblogs.com/l10n/p/9400820.html