0116MySql主从复制监控
如何自动化监控mysql主从服务
-- 第一步创建目录
mkdir -p /tmp/mysql/masterslave #日志存放目录
在/root下面创建sh目录 #存放执行的SHELL脚本
-- 第二步编写SHELL文件
#!/bin/sh
# 2017-01-16
# send mail to a@163.com
# 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 [ "$1" == "$num" ];then
$MysqlCmd -e'stop slave;set global sql_slave_skip_counter=1;start slave;'
echo "$(date +%F_%R) $1" >>$errorlog
else
echo "$(date +%F_%R) $1" >>$errorlog
((flag++))
fi
done
[ "$flag" == "${#ErrorNo[@]}" ] &&{
uniq $errorlog|mail -s "MySQL Slave is error" a@163.com
}
}
# 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" -a "${array[2]}" == "0" ]
then
echo "$(date +%F_%R) $1" >>$rightlog
else
error_skip ${array[3]}
fi
}
# Defined Main Functions
function main(){
while true
do
check_slave
done
}
main
-- 第三步利用定时任务,重复执行该shell文件
-- 每隔10分钟更新一次
0-59/10 * * * * /root/sh/jk2.sh