How to automate monitoring MySQL master-slave Service
--First step to create a directory
Mkdir-p/tmp/mysql/masterslave #日志存放目录
Create the SH directory below/root #存放执行的SHELL脚本
--second step to write the shell file
#!/bin/sh
# 2017-01-16
# Send mail to [email protected]
# Defined Variables
Mysqluser=root
mysqlpass=111111
mysqlport=3306
errorno= (1158 1159 1008 1007 1062)
Errorlog=/tmp/mysql/masterslave/error_skip.log
Rightlog=/tmp/mysql/masterslave/right.log
Mysqlcmd= "/usr/bin/mysql-u$mysqluser-p$mysqlpass"
# Defined Skip Error Functions
function Error_skip () {
Local flag
Flag=0
For num in ${errorno[@]}
Do
If ["$" = = "$num"];then
$MYSQLCMD-E ' stop slave;set global sql_slave_skip_counter=1;start slave; '
echo "$ (date +%f_%r) $" >> $errorlog
Else
echo "$ (date +%f_%r) $" >> $errorlog
((flag++))
Fi
Done
["$flag" = = "${#ErrorNo [@]}"] &&{
Uniq $errorlog |mail-s "MySQL Slave is error" [email protected]
}
}
# Defined Check Slave Functions
function Check_slave () {
myresult= ' $MYSQLCMD-e ' show slave status\g ' |egrep ' _running| behind_master| Sql_errno ' |awk ' {print $NF} '
Array= ($MyResult)
If ["${array[0]}" = = "Yes"-a "${array[1]}" = = "Yes"-"${array[2]}" = = "0"]
Then
echo "$ (date +%f_%r) $" >> $rightlog
Else
Error_skip ${array[3]}
Fi
}
# Defined Main Functions
function Main () {
While True
Do
Check_slave
Done
}
Main
--The third step is to execute the shell file repeatedly using a timed task
--Updated every 10 minutes
0-59/10 * * * */root/sh/jk2.sh
0116MYSQL master-slave Replication Monitoring