目的: 监测mysql的存活状态,一旦监测到mysql down掉,重新启动mysql.
脚本内容:
/usr/local/mysql/bin/mysqladmin -uroot -psharpower ping > /dev/null 2>&1 if [ $? -ne 0 ] then /etc/init.d/mysql.server restart >/dev/null echo "`/bin/date ‘+%Y%m%d %H:%M:%S‘` mysql is restart " >> /root/scripts/mysql_run.log fi
利用crontab定时任务来执行脚本:
[root@Master_mysql scripts]# crontab -l */2 * * * * /bin/bash /root/scripts/mysql_ping.sh
手动模拟关闭msyql,然后间隔2分钟,查看cron执行日志"/var/log/cron"和脚本执行日
志"/root/scripts/mysql_run.log"
#cron执行日志 [root@Master_mysql scripts]# tail -n 10 /var/log/cron Jul 22 16:27:01 Master_mysql crontab[5818]: (root) BEGIN EDIT (root) Jul 22 16:27:07 Master_mysql crontab[5818]: (root) REPLACE (root) Jul 22 16:27:07 Master_mysql crontab[5818]: (root) END EDIT (root) Jul 22 16:27:38 Master_mysql crontab[5849]: (root) LIST (root) Jul 22 16:28:01 Master_mysql crond[3103]: (root) RELOAD (cron/root) Jul 22 16:28:01 Master_mysql crond[5853]: (root) CMD (/bin/bash /root/scripts/mysql_ping.sh) Jul 22 16:29:59 Master_mysql crontab[6138]: (root) LIST (root) Jul 22 16:30:01 Master_mysql crond[6140]: (root) CMD (/bin/bash /root/scripts/mysql_ping.sh) Jul 22 16:32:01 Master_mysql crond[6394]: (root) CMD (/bin/bash /root/scripts/mysql_ping.sh) Jul 22 16:34:01 Master_mysql crond[6429]: (root) CMD (/bin/bash /root/scripts/mysql_ping.sh) #脚本执行日志 [root@Master_mysql scripts]# cat mysql_run.log 20140722 16:28:14 mysql is restart 20140722 16:30:09 mysql is restart 20140722 16:34:08 mysql is restart
这样就可以实现mysql停服时自动启动了.
本文出自 “史振宁的技术博客” 博客,请务必保留此出处http://magic3.blog.51cto.com/1146917/1440963
原文地址:http://magic3.blog.51cto.com/1146917/1440963