#!/bin/sh
#check_mysql_slave status
ip=eth1 #网卡名称
mysql_binfile=/usr/local/mysql/bin/mysql
mysql_user=root #MySQL数据库账号
mysql_pass=123456 #密码
mysql_sockfile=/tmp/mysql.sock
datetime=`date +"%Y-%m-%d/%H:%M:%S"` #获取当前时间
mysql_slave_logfile=/opt/slave.log #日志文件路径,必须提前创建好
slave_ip=`ifconfig $ip|grep "inet addr" | awk -F[:" "]+ ‘{print $4}‘`
status=$($mysql_binfile -u$mysql_user -p$mysql_pass -S $mysql_sockfile -e "show slave status\G" | grep -i "running")
Slave_IO_Running=`echo $status | grep Slave_IO_Running | awk ‘ {print $2}‘`
Slave_SQL_Running=`echo $status | grep Slave_SQL_Running | awk ‘{print $2}‘`
if [ "$Slave_IO_Running" = "Yes" -a "$Slave_SQL_Running" = "Yes" ]
then echo "Slave is Running!" >> /opt/s.log
#mail -s "$ip Slave is running!(主从关系正常,放心使用啦。)" 243652136\@qq.com < /opt/s.log
else
echo " $datetime $slave_ip Slave is not running!" >> $mysql_slave_logfile
mail -s " Slave is not running!(主从关系宕机啦,赶紧去修复吧。)" 243652136\@qq.com < $mysql_slave_logfile
$mysql_binfile -u$mysql_user -p$mysql_pass -S $mysql_sockfile -e "stop slave;"
$mysql_binfile -u$mysql_user -p$mysql_pass -S $mysql_sockfile -e "set global sql_slave_skip_counter=1;"
$mysql_binfile -u$mysql_user -p$mysql_pass -S $mysql_sockfile -e "start slave;"
$mysql_binfile -u$mysql_user -p$mysql_pass -S $mysql_sockfile -e "exit"
fi
本文出自 “系统网站运维” 博客,请务必保留此出处http://cgc243652136qq.blog.51cto.com/3989433/1829623
原文地址:http://cgc243652136qq.blog.51cto.com/3989433/1829623