mysql主从同步监控自动提取主库的file号及pos

来源:互联网 发布:小学网络课程 编辑:程序博客网 时间:2024/06/03 16:33
 mysql主从同步监控自动提取主库的file号及pos
当发现同步出现无法同步的时候”会自动提取主库的file号,以及pos,进行同步主库,脚本内容如下:
 

    #!/bin/sh
    #set -x
    #file is slave_repl.sh
    #Author by Kevin
    #date is 2011-11-13

    mstool="/usr/local/mysql-3307/bin/mysql -h 192.168.1.106 -uroot daa#@897dkdlie$& -P 3307"
    sltool="/usr/local/mysql-3307/bin/mysql -h 192.168.1.107 -uroot daa#@897dkdlie$& -P 3307"

    declare -a slave_stat
    slave_stat=($($sltool -e "show slave status\G"|grep Running |awk '{print $2}'))

    if [ "${slave_stat[0]}" = "Yes" -a "${slave_stat[1]}" = "Yes" ]
         then
         echo "OK slave is running"
         exit 0
    else
         echo "Critical slave is error"
         echo
         echo "*********************************************************"
         echo "Now Starting replication with Master Mysql!"
            file=`$mstool -e "show master status\G"|grep "File"|awk '{print $2}'`
            pos=`$mstool -e "show master status\G"|grep "Pos"|awk '{print $2}'`
            $sltool -e "slave stop;change master to master_host='192.168.1.106',master_port=3307,master_user='repl',master_password='w!zl7POg27',master_log_file='$file',master_log_pos=$pos;slave start;"
            sleep 3
            $sltool -e "show slave status\G;"|grep Running
        echo
        echo "Now Replication is Finished!"
        echo
        echo "**********************************************************"
            exit 2
    fi

 
运行后效果,如下图:
 

    # ./slave_stop3307.sh
    *******************************
    Now stop Slave Replication!
               Slave_IO_Running: No
              Slave_SQL_Running: No
    *******************************
    # ./slave_repl3307.sh
    Critical slave is error

    *********************************************************
    Now Starting replication with Master Mysql!
               Slave_IO_Running: Yes
              Slave_SQL_Running: Yes

    Now Replication is Finished!

    **********************************************************