一、具体代码
#!/bin/bash
#--------------------------------------------------
#Author:jimmygong
#Email:jimmygong@taomee.com
#FileName:checkslave.sh
#Function:
#Version:1.0
#Created:2015-12-29
#--------------------------------------------------
if [[ `id -u` -ne 0 ]]
then
echo "script need root"
exit 1
fi
#根据情况修改变量信息
mysqluser="root"
mysqlpass="123456"
mysqlport=3398
mysqlsocket="/opt/mysql/socket/$mysqlport.sock"
nowtime=$(date +"%m-%d %H:%M:%S")
#各个错误代码解释
1158网络错误,出现读错误,请检查网络连接状况
1159网络错误,读超时,请检查网络连接状况
1008数据库不存在,删除数据库失败
1007数据库已存在,创建数据库失败
1062字段值重复,入库失败
1452不能删除或更新父行,外键约束失败
PS:1452这个根据情况跳过错误。
allerror=(1158 1159 1008 1007 1062 1452)
function redirectlog ()
{
currdate=$(date +%Y%m%d)
logdir="/root/checkslave"
logfile=$logdir/${currdate}
mkdir -p $logdir
exec 1>$logfile
exec 2>$logfile
}
function mysqlconn ()
{
comm=$1
mysql -u${mysqluser} -p${mysqlpass} -S ${mysqlsocket} -e "$comm"
}
function checkrun ()
{
esport=$(lsof -i:$mysqlport|wc -l)
if [[ $esport -lt 2 ]]
then
echo "Mysql Server Failed"
exit 1
fi
}
function statuserror ()
{
for((i=0;i<${#allerror[*]};i++))
do
if [[ "$1" == "${allerror[$i]}" ]]
then
mysqlconn "stop slave;set global sql_slave_skip_counter=1;start slave;"
else
echo "$nowtime MySQL slave is failed $1"
fi
done
}
function checkslave ()
{
status=($(mysqlconn "show slave status\G"|egrep -i "_running|last_errno"|awk ‘{print $NF}‘))
if [[ "${status[0]}" == "Yes" ]] && [[ "${status[1]}" == "Yes" ]]
then
echo "MySQL Slave Ok"
else
statuserror ${status[2]}
fi
}
function main ()
{
while true
do
checkrun
checkslave
sleep 60
done
}
redirectlog
main
二、相关日志都记录在
cat checkslave/20151226 |more
12-26 16:16:00 MySQL slave is failed 1452
12-26 16:16:00 MySQL slave is failed 1452
12-26 16:16:00 MySQL slave is failed 1452
12-26 16:16:00 MySQL slave is failed 1452
12-26 16:16:00 MySQL slave is failed 1452
12-26 16:16:00 MySQL slave is failed 1452
12-26 16:16:00 MySQL slave is failed 1452
12-26 16:16:00 MySQL slave is failed 1452
12-26 16:16:00 MySQL slave is failed 1452
12-26 16:16:00 MySQL slave is failed 1452本文出自 “7928217” 博客,请务必保留此出处http://7938217.blog.51cto.com/7928217/1729738
原文:http://7938217.blog.51cto.com/7928217/1729738