脚本设计思路:
1、此脚本应该能适应各种各样不同的内外网环境,即IP不同的环境;
2、让脚本也顺便监控下MySQL是否正常运行;
3、Slave机器的IO和SQL状态都必须为YES,缺一不可,这里用到了多重条件判断-a。
脚本内容:
#!/bin/bash
#check MySQL_Slave Status
#crontab time 00:10
MYSQLPORT=`netstat -na|grep "LISTEN"|grep "3306"|awk -F[:" "]+ '{print $5}'`
MYSQLIP=`ifconfig eth0|grep "inet addr" | awk -F[:" "]+ '{print $4}'`
STATUS=$(/usr/local/mysql/bin/mysql -u zhaoyuefei -pzhaoyuefei1*0*1 -S /tmp/mysql.sock -e "show slave status\G" | grep -i "running")
IO_env=`echo $STATUS | grep IO | awk ' {print $2}'`
SQL_env=`echo $STATUS | grep SQL | awk '{print $2}'`
if [ "$MYSQLPORT" == "3306" ]
then
echo "mysql is running"
else
/bin/mail -s "warn!server: $MYSQLIP mysql is down" zalifei@126.com
fi
if [ "$IO_env" = "Yes" -a "$SQL_env" = "Yes" ]
then
echo "Slave is running!"
else
echo "####### $date #########" >> /tmp/check_mysql_slave.log
echo "Slave is not running!" >> /tmp/check_mysql_slave.log
echo "Slave is not running!"
#mail -s "warn! $MySQLIP_replicate_error" zalifei@126.com << /tmp/check_mysql_slave.log
echo "`cat /tmp/check_mysql_slave.log`" |mail -s "Warning...slave is not running!!" zalifei@126.com
fi
使用方式:
用crontab设置定期运行,建议每十分钟运行一次
*/10 * * * * /root/mysql-slave-status.sh
记得在每台MySQL从机上分配一个yuhongchun的用户,权限大些也没关系,只限定在本地运行,如下所示:
grant all privileges on *.* to "zhaoyuefei"@"127.0.0.1" identified by "zhaoyuefei1*0*1";
grant all privileges on *.* to "zhaoyuefei"@"localhost" identified by "zhaoyuefei1*0*1";
阅读(1658) | 评论(2) | 转发(1) |