要求:
每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" [email protected] </tmp/mysql_error.log
fi
}
function main(){
while true
do
check_error
check_again
sleep 30
done
}
main
原文地址:https://blog.51cto.com/1009516/2431734