假设,当前MySQL服务的root密码为123456,写脚本检测MySQL服务是否正常(比如,可以正常进入mysql执行show processlist),
并检测一下当前的MySQL服务是主还是从,如果是从,请判断它的主从服务是否异常。如果是主,则不需要做什么。
#!/bin/bash
mysql="/usr/local/mysql/bin/mysql -uroot -p123456"
if ! $mysql -e "show processlist" >/dev/null 2>/dev/null
then
echo "MySQL service is down."
exit
else
$mysql -e "show slave status\G" 2>/dev/null >/tmp/slave.stat
n=`wc -l /tmp/slave.stat|awk '{print $1}'`
if [ $n -eq 0 ]
then
echo "This is master."
else
echo "This is slave."
egrep 'Slave_IO_Running:|Slave_SQL_Running:'/tmp/slave.stat|awk -F ': ' '{print $2}' > /tmp/SQL.tmp
if grep -qw "No" /tmp/SQL.tmp
then
echo "The slave is down."
fi
fi
fi