mysql主从同步监控小脚本(加强版) 



mysql主从同步监控小脚本(加强版):

 

新版本脚本增加了“当发现同步出现无法同步的时候”会自动提取主库的file号,以及pos,进行同步主库,脚本内容如下:

 

  1. #!/bin/sh

  2. #set -x

  3. #file is slave_repl.sh

  4. #Author by Kevin

  5. #date is 2011-11-13


  6. mstool="/usr/local/mysql-3307/bin/mysql -h 192.168.1.106 -uroot -pw!zl7POg27 -P 3307"

  7. sltool="/usr/local/mysql-3307/bin/mysql -h 192.168.1.107 -uroot -pw!zl7POg27 -P 3307"


  8. declare -a slave_stat

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


  10. if [ "${slave_stat[0]}" = "Yes" -a "${slave_stat[1]}" = "Yes" ]

  11.      then

  12.      echo "OK slave is running"

  13.      exit 0

  14. else

  15.      echo "Critical slave is error"

  16.      echo

  17.      echo "*********************************************************"

  18.      echo "Now Starting replication with Master Mysql!"

  19.         file=`$mstool -e "show master status\G"|grep "File"|awk '{print $2}'` 

  20.         pos=`$mstool -e "show master status\G"|grep "Pos"|awk '{print $2}'` 

  21.         $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;"

  22.         sleep 3

  23.         $sltool -e "show slave status\G;"|grep Running

  24.     echo

  25.     echo "Now Replication is Finished!"

  26.     echo

  27.     echo "**********************************************************"

  28.         exit 2

  29. fi

 

运行后效果,如下图:

 

  1. ./slave_stop3307.sh 

  2. *******************************

  3. Now stop Slave Replication!

  4.            Slave_IO_Running: No

  5.           Slave_SQL_Running: No

  6. *******************************

  7. ./slave_repl3307.sh 

  8. Critical slave is error


  9. *********************************************************

  10. Now Starting replication with Master Mysql!

  11.            Slave_IO_Running: Yes

  12.           Slave_SQL_Running: Yes


  13. Now Replication is Finished!


  14. **********************************************************