首页 > 代码库 > 监控MYSQL主从同步配置中监控从库运行状态的脚本
监控MYSQL主从同步配置中监控从库运行状态的脚本
代码如下:
[java] view plain copy
- #!/bin/bash
- #Check MySQL Slave‘s Runnning 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/bin/mysql -uroot -p11111 -S /var/lib/mysql/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}‘`
- DATA=http://www.mamicode.com/`date +"%y-%m-%d %H:%M:%S"`
- function checkMysqlStatus(){
- if [ "$MYSQLPORT" == "3306" ]
- then
- /usr/bin/mysql -uroot -p11111 --connect_timeout=5 -e "show databases;" &>/dev/null 2>&1
- if [ $? -ne 0 ]
- then
- echo "Server: $MYSQLIP mysql is down, please try to restart mysql by manual!" > /var/log/mysqlerr
- mail -s "WARN! server: $MYSQLIP mysql is down." admin@yourdomain.com < /var/log/mysqlerr
- else
- echo "mysql is running..."
- fi
- else
- mail -s "WARN!Server: $MYSQLIP mysql is down." admin@yourdomain.com
- fi
- }
- checkMysqlStatus
- if [ "$IO_env" = "Yes" -a "$SQL_env" = "Yes" ]
- then
- echo "MySQL Slave is running!"
- else
- echo "####### $DATA #########">> /data/mysql/mysql_slave_status.log
- echo "MySQL Slave is not running!" >> /data/mysql/mysql_slave_status.log
- echo "MySQL Slave is not running!" | mail -s "WARN! $MYSQLIP MySQL Slave is not running." admin@yourdomain.com
- fi
建议每十分钟运行一次
*/10 * * * * root /bin/sh /root/mysql_slave_status.sh
原理:
1)监控MYSQL的运行状态;
2)Slave机器的IO和SQL状态都必须为YES,缺一不可;
注意,请结合自身的实际环境使用。
监控MYSQL主从同步配置中监控从库运行状态的脚本
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。