1. 程式人生 > >mysql主從同步監控腳本

mysql主從同步監控腳本

mysql主從復制監控腳本 linux mysql shell

mysql主從同步監控腳本,利用mysql從庫中的IO和SQL進程以及延遲時間來監控主從同步是否正常,詳細shell腳本如下:

#!/bin/bash
#author wangning
#date 2017-7-17
#qq 1198143315
#Email [email protected]


################################## define variable#############################
define_variable(){
ip=`ifconfig|sed -n ‘2p‘|awk -F "[: ]+" ‘{print $4}‘`
code=(1158 1159 1008 1007 1062)
status=(`mysql -uroot -p123456 -S /data/3309/mysql.sock -e "show slave status\G"|egrep "Running|Behind_Master|Last_Errno"|awk ‘{print $2}‘`)
}

############################ judge master #######################################
judge_master(){
mysql -uroot -p123456 -S /data/3306/mysql.sock -e "show databases" &>/dev/null
if [ $? -ne 0 ];then
   echo "ip $ip the master mysql service is downed" >/mail/mysql_master.log &&   mail -s "wangning tile" [email protected]
/* */ </mail/mysql_master.log else echo "ip $ip the master mysql service is normal" >/mail/mysql_master.log && mail -s "wangning tile" [email protected] </mail/mysql_master.log fi } ################### judge IO SQL status and delay time ############################### IO_SQL_delay(){ if [ "${status[0]}" == "Yes" -a "${status[1]}" == "Yes" -a ${status[3]} -le 60 ];then echo "the master and slave replication is normal" >/mail/mysql_slave.log && mail -s "wangning tile" [email protected]
/* */ </mail/mysql_slave.log else echo "the master and slave replication is failed" >/mail/mysql_slave.log && mail -s "wangning tile" [email protected] </mail/mysql_slave.log fi } ################################ judge error code ################################## judge_error_code(){ for ((i=0;i<=${#status[*]};i++)) do if [ ${status[2]} -eq ${code[i]} ];then mysql -uroot -p123456 -S /data/3309/mysql.sock -e "stop slave;set global sql_slave_skip_counter=1;start slave" fi done } main(){ while true do define_variable judge_master IO_SQL_delay judge_error_code sleep 180 done } main


本文出自 “飛奔的駱駝” 博客,請務必保留此出處http://wn2100.blog.51cto.com/9915310/1948112

mysql主從同步監控腳本