监控MySQL主从同步是否异常,如果异常,则发送短信或者邮件给管理员
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了监控MySQL主从同步是否异常,如果异常,则发送短信或者邮件给管理员相关的知识,希望对你有一定的参考价值。
要求:每30秒实现检测一次。
如果同步出现如下错误号(1158,1159,1008,1007,1062),则跳过错误。
#!/bin/sh
#monitor mysql master to salve connection
#by zkg 2019-08-22
. /etc/init.d/functions
#Define variables
MYSQLUSER=root
MYSQLPASSWD=DbApp
SOCKET=/data/mysql/tmp/mysql.sock
MYSQLCMD="mysql -u$MYSQLUSER -p$MYSQLPASSWD -S $SOCKET"
#Define array
array_status=($($MYSQLCMD -e "show slave status;"|grep -E "_Running|Behind_Master|_IO_Errno"|awk -F "[:]+" ‘print $NF‘|sed ‘s/ //g‘))
error_num=(1158 1159 1008 1007 1062)
#Define function
function check_status()
if [ "$array_status[0]" = "Yes" -a "$array_status[1]" = "Yes" -a "$array_status[2]" = "0" ];then
action "slave is ok" /bin/true
status=0
return $status
else
status=1
return $status
fi
function check_error()
check_status
if [ $? -ep 1 ];then
for ((i=0;i<$#error_num[*];i++))
do
if [ "$error_num[i]" == "$array_status[3]" ];then
$MYSQLCMD -e "stop slave"
$MYSQLCMD -e "set global sql_slave_skip_counter=1"
$MYSQLCMD -e "start slave"
fi
done
fi
function check_again()
array_status=($($MYSQLCMD -e "show slave status;"|grep -E "_Running|Behind_Master|_IO_Errno"|awk -F "[:]+" ‘print $NF‘|sed ‘s/ //g‘))
check_status &>/dev/null
if [ $? -ep 1 ];then
echo "mysql slave is failed,please check quick" >/tmp/mysql_error.log
mail -s "mysql slave is failed" 67897796@qq.com </tmp/mysql_error.log
fi
function main()
while true
do
check_error
check_again
sleep 30
done
main
以上是关于监控MySQL主从同步是否异常,如果异常,则发送短信或者邮件给管理员的主要内容,如果未能解决你的问题,请参考以下文章