mysql主从同步监控小脚本(加强版): 新版本脚本增加了“当发现同步出现无法同步的时候”会自动提取主库的file号,以及pos,进行同步主库,脚本内容如下:
#!/bin/sh #set-x #fileisslave_repl.sh #AuthorbyKevin #dateis-11-13 mstool="/usr/local/mysql-3307/bin/mysql-h192.168.1.106-uroot-pw!zl7POg27-P3307"sltool="/usr/local/mysql-3307/bin/mysql-h192.168.1.107-uroot-pw!zl7POg27-P3307"declare-aslave_stat slave_stat=($($sltool-e"showslavestatus\G"|grepRunning|awk'{print$2}')) if["${slave_stat[0]}"="Yes"-a"${slave_stat[1]}"="Yes"] thenecho"OKslaveisrunning"exit0 elseecho"Criticalslaveiserror"echo echo"*********************************************************"echo"NowStartingreplicationwithMasterMysql!"file=`$mstool-e"showmasterstatus\G"|grep"File"|awk'{print$2}'` pos=`$mstool-e"showmasterstatus\G"|grep"Pos"|awk'{print$2}'` $sltool-e"slavestop;changemastertomaster_host='192.168.1.106',master_port=3307,master_user='repl',master_password='w!zl7POg27',master_log_file='$file',master_log_pos=$pos;slavestart;"sleep3 $sltool-e"showslavestatus\G;"|grepRunning echo echo"NowReplicationisFinished!"echo echo"**********************************************************"exit2 fi 运行后效果,如下图:
#./slave_stop3307.sh ******************************* NowstopSlaveReplication! Slave_IO_Running:NoSlave_SQL_Running:No******************************* #./slave_repl3307.sh Criticalslaveiserror ********************************************************* NowStartingreplicationwithMasterMysql! Slave_IO_Running:Yes Slave_SQL_Running:Yes NowReplicationisFinished! **********************************************************