时间:2021-07-01 10:21:17 帮助过:22人阅读
脚本设计思路:
1、此脚本应该能适应各种各样不同的内外网环境,即IP不同的环境;
2、让脚本也顺便监控下MySQL是否正常运行;
3、Slave机器的IO和SQL状态都必须为YES,缺一不可,这里用到了多重条件判断-a。
#!/bin/bash #check MySQL_Slave Status #crontab time 00:10 Mail-list= MysqlUser= MysqlPass= 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 $MysqlUser -$MysqlPass -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 "warning!server: $MYSQLIP mysql is down" $Mail-list 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" $Mail-list << /tmp/check_mysql_slave.log echo "`cat /tmp/check_mysql_slave.log`" |mail -s "Warning...slave is not running!!" $Mail-list fi
使用方式:
用crontab设置定期运行,建议每十分钟运行一次
*/10 * * * * /root/mysql-slave-status.sh
记得在每台MySQL从机上分配一个jiankong的用户,权限大些也没关系,只限定在本地运行,如下所示:
grant all privileges on *.* to "jiankong"@"127.0.0.1" identified by "jiankong1*0*1";
grant all privileges on *.* to "jiankong"@"localhost" identified by "jiankong1*0*1";
以上内容就是自动监控从MySQL同步的脚本,希望大家能有所领会。
相关推荐:
Python自动监控网站并发送邮件告警
Linux服务器监控的实例详解
监控MySQL的同时收集表信息代码详解(图文)
以上就是mysql实现自动监控同步的脚本的详细内容,更多请关注Gxl网其它相关文章!