Home > Database > Mysql Tutorial > mysql script to implement automatic monitoring and synchronization

mysql script to implement automatic monitoring and synchronization

小云云
Release: 2017-11-24 09:29:34
Original
2912 people have browsed it

MySQL越来越被更多企业接受,随着企业发展,MySQL存储数据日益膨胀,MySQL的性能分析、监控预警、容量扩展议题越来越多。“工欲善其 事,必先利其器”,那么我们如何在进行MySQL性能分析、监控预警、容量扩展问题上得到更好的解决方案,就要利用各种工具来对MySQL各种指标进行分析。本篇内容我们就讲解自动监控从MySQL同步的脚本。

脚本设计思路:

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
Copy after login

使用方式:

用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的同时收集表信息代码详解(图文)

The above is the detailed content of mysql script to implement automatic monitoring and synchronization. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template