本文絕對【干貨】,我們將分享一個shell,他的作用是能夠自動恢復mysql主從錯誤,讓你想象不到的強大,想用的朋友直接復制吧。
本shell的功能是自動恢復mysql主從錯誤,是不是感覺非常強大?好吧,直接上代碼。
代碼如下
復制代碼
#!/bin/sh
# check_mysql_slave status
ip=eth0
mysql_command=/home/server/mysql/bin/mysql
mysql_user=root
mysql_pass=123456
mysql_sockfile=/tmp/mysql.sock
datetime=`date +"%Y-%m-%d_%H:%M:%S"`
mysql_slave_logfile=/home/server/logs/check_mysql_slave.log
slave_ip=`ifconfig $ip|grep "inet addr" | awk -F[:" "]+ '{print $4}'`
#status=$($mysql_command -u$mysql_user -p$mysql_pass -S $mysql_sockfile -e "show slave status\G" | grep -i "running")
Slave_IO_Running=`grep Slave_IO_Running $mysql_slave_logfile| awk ' {print $2}'`
Slave_SQL_Running=`grep Slave_SQL_Running $mysql_slave_logfile | awk '{print $2}'`
if [ "$Slave_IO_Running" = "Yes" -a "$Slave_SQL_Running" = "Yes" ]
then
echo "Slave is Running!"
else
echo " $datetime $slave_ip Slave is not running!" >> $mysql_slave_logfile
cat | $mysql_command -u$mysql_user -p$mysql_pass -S $mysql_sockfile << EOF
stop slave;
SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
start slave;
exit
EOF
fi