Fork me on GitHub
瞬间的惊奇真是种确幸,每种确幸都是支玄妙的俳句。吊桥抬起,退回到内心幽静的花园,邂逅的还是那种熟悉的冷冷清清又轰轰烈烈的美妙质感。这真令人欢喜。

shell(3)-mysql主从监控shell

需要先明白数据库主从同步正常的标准是来查看两个线程Slave_IO和Slave_SQL两个线程的工作状态;


#!/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=`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,缺一不可;

posted @ 2018-10-01 09:20  干嘛那么贪睡  阅读(185)  评论(0编辑  收藏  举报