快捷搜索:

您的位置:澳门新葡4473网站 > 项目 > 澳门新葡4473网站检测MySQL主从备份是否运行

澳门新葡4473网站检测MySQL主从备份是否运行

发布时间:2019-11-02 07:14编辑:项目浏览(146)

    透过查看 slave  状态,确定保证 Slave_IO_Running: Yes Slave_SQL_Running: Yes

    发布:thebaby   来源:net     【大 中 小】

    #!/bin/bash
    #Author:Darius-D
    mysql -uroot -p123123 -e 'show slave statusG' >a.txt
    IO=`head -12 a.txt | tail -1|awk -F ':' '{print $2}'`
    SQL=`head -13 a.txt | tail -1|awk -F ':' '{print $2}'`
    if [ $IO == 'Yes' ];then
    echo "Slave_IO_Running:Yes"
    澳门新葡4473网站,if [ $SQL == 'Yes' ];then
    echo "Slave_SQL_Running:Yes"
    else
    echo "Slave_SQL_Running:No"
    fi
    else
    echo "Slave_IO_Running:No"
    fi
    有关于mysql主从备份相关原理及备份链接踏向查看。

    分享生龙活虎例shell脚本,用于改善mysql的骨干同步难点,有须求的相恋的黄参谋下吧。

    三个得以改善mysql主导同步的shell脚本。

    例子:  

    复制代码代码示例:

    #!/bin/sh
    #修补mysql主从同步
    PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
    LOGFILE=/data/repair_mysql_sync_`date +%F`.log
    SQLCMD1="show slave status"
    #查阅MySQL是或不是运转
    retval=`ps aux | grep mysqld | grep -v grep`
    if [ "${retval}X" = "X" ]; then
        echo The MySQL is not running at: `date +%F" "%H-%M-%S` >> ${LOGFILE}
        exit 1
    fi
    #获得MySQL从端Relay binlog的路径
    retval=`grep "^relay-log" /etc/my.cnf | grep -v relay-log- | grep '/'`
    if [ "${retval}" = "X" ]; then
        RELAY_BINLOG_PATH=`ps aux | grep -w mysqld | grep -v grep | awk '{print $13}' | awk -F '=' '{print $2}'`
    else
        RELAY_BINLOG_PATH=`dirname $(echo ${retval} | awk -F '=' '{print $2}')`
    fi
    #找出master.info文件,用于固定Binlog消息
    MASTER_FILE=`ps aux | grep -w mysqld | grep -v grep | awk '{print $13}' | awk -F '=' '{print $2}'`/master.info
    if [ ! -e ${MASTER_FILE} ]; then
       echo This Server is not MySQL Slave at: `date +%F" "%H-%M-%S` >> ${LOGFILE}
       exit 1
    fi
    #赢安妥前的同台状态
    IO_STATUS=`mysql -uroot -e "${SQLCMD1}G;" | awk '$1=="Slave_IO_Running:" {print $2}'`
    SQL_STATUS=`mysql -uroot -e "${SQLCMD1}G;" | awk '$1=="Slave_SQL_Running:" {print $2}'`
    if [[ "${IO_STATUS}" = "Yes" && "${SQL_STATUS}" = "Yes" ]]; then
       echo Now, The MySQL Replication is synchronous at: `date +%F" "%H-%M-%S` >> ${LOGFILE}
       exit 0
    fi
    #从master.info文件中,得到MySQL主端的风姿浪漫道消息
    REPLI_INFO=`sed '/^$/d' ${MASTER_FILE} | tail +2 | head -5`
    REPLI_BINLOG_FILE=`echo ${REPLI_INFO} | awk '{print $1}'`
    REPLI_IPADDR=`echo ${REPLI_INFO} | awk '{print $3}'`
    REPLI_USER=`echo ${REPLI_INFO} | awk '{print $4}'`
    REPLI_PWD=`echo ${REPLI_INFO} | awk '{print $5}'`
    #除去无用的Relay binlog
    rm -rf ${RELAY_BINLOG_PATH}/*-relay-bin.*
    #向来从0位置上马联合
    SQLCMD2="change master to master_host='${REPLI_IPADDR}', master_user='${REPLI_USER}', master_password='${REPLI_PWD}',"
    SQLCMD2="${SQLCMD2} master_log_file='${REPLI_BINLOG_FILE}', master_log_pos=0"
    mysql -uroot -e "stop slave;"
    mysql -uroot -e "${SQLCMD2};"
    mysql -uroot -e "start slave;"
    #若果同步的长河中,现身重复记录导致同步战败,就跳过
    while true
    do
       sleep 2
       IO_STATUS=`mysql -uroot -e "${SQLCMD1}G;" | awk '$1=="Slave_IO_Running:" {print $2}'`
       SQL_STATUS=`mysql -uroot -e "${SQLCMD1}G;" | awk '$1=="Slave_SQL_Running:" {print $2}'`
       BEHIND_STATUS=`mysql -uroot -e "${SQLCMD1}G;" | awk '$1=="Seconds_Behind_Master:" {print $2}'`
       SLAVE_BINLOG1=`mysql -uroot -e "${SQLCMD1}G;" | awk '$1=="Master_Log_File:" {print $2}'`
       SLAVE_BINLOG2=`mysql -uroot -e "${SQLCMD1}G;" | awk '$1=="Relay_Master_Log_File:" {print $2}'`
       #并发谬误,就将错误音讯记录到日志文件,并跳过错误继续一同
       if [[ "${IO_STATUS}" != "Yes" || "${SQL_STATUS}" != "Yes" ]]; then
           ERRORINFO=`mysql -uroot -e "${SQLCMD1}G;" | awk -F ': ' '$1=="Last_Error" {print $2}'`
           echo "The MySQL synchronous error information: ${ERRORINFO}" >> ${LOGFILE}
           mysql -uroot -e "stop slave;"
           mysql -uroot -e "set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;"
           mysql -uroot -e "start slave;"
           #已做到意气风发道,就平常退出
       elif [[ "${IO_STATUS}" = "Yes" && "${SQL_STATUS}" = "Yes" && "${SLAVE_BINLOG1}" = "${SLAVE_BINLOG2}" && ${BEHIND_STATUS} -eq 0 ]]; then
          echo The MySQL synchronous is ok at: `date +%F" "%H-%M-%S` >> ${LOGFILE}
          break
     fi
    done

    你只怕感兴趣的随笔:
    电动配置mysql主从的shell脚本
    督察mysql主从好端端状态的shell脚本
    mysql主从督察的shell脚本

    正文地址:

    本文由澳门新葡4473网站发布于项目,转载请注明出处:澳门新葡4473网站检测MySQL主从备份是否运行

    关键词:

上一篇:没有了

下一篇:没有了