actual production case: monitoringMysqlMaster-Slave synchronization is abnormal, if the exception, send SMS or mail to the administrator. Tip: If there is no master-slave synchronization environment,The following text can be placed in the file to be read to simulate:
Stage1: Develop a daemon script for each -The second implementation is detected once.
Stage2: If the synchronization appears with the following error number (1158,1159,1008,1007,1062), the error is skipped.
Stage3: Use array technology to implement the above script (get master-slave judgment and Error number section)
Answer:
#!/bin/sh
# NO1 Define Variables
array= (1158 1159 1008 1007 1062)
date=$ (Date +%f)
Mysql_cmd= "Mysql-uroot-poldboy123-s/data/3307/mysql.sock-e"
ip=$ (Ifconfig eth2|awk-f "[:]+" ' Nr==2{print $4} ')
port=3307
# No2 Daemon for monitor MySQL replication
While True
Do
# SKIP Error num 1158 1159 1008 1007 1062
error_num=$ (${mysql_cmd} "show slave Status\g" |grep "Last_sql_errno" |awk ' {print $NF} ')
For n in${array[@]}
Do
If [$Error _num-eq $n];then
${mysql_cmd} "Stop Slave;set global sql_slave_skip_counter=1;startslave;"
Fi
Done
# No3 Check MySQL replication Status
Mysql_status= ($ ($Mysql _cmd "show slave Status\g" |egrep "_running|_behind_master" |awk ' {print $NF} '))
Flag=0
For n in${mysql_status[@]}
Do
If ["$n"! = "Yes"-a "$n"! = "0"];then
Letflag=flag+1
Fi
Done
# No4 Mail to System Manager
If [$Flag-ne 0];then
echo "[$Date] there is $Flag problems on MySQL slave. IP: $IPPort: $Port ">>/tmp/slave.log
Tail-1/tmp/slave.log|mail-s "Slave Warning" [email protected]
Sleep 300
Else
echo "Mysqlslave is OK"
Fi
# No5 complete!
Sleep 30
Done
This article is from the "architects of the Day" blog, be sure to keep this source http://wanyuetian.blog.51cto.com/3984643/1737976
Scripting MySQL master-slave replication status monitoring