目的: 监测mysql的存活状态,一旦监测到mysql down掉,重新启动mysql.
脚本内容:
1
2
3
4
5
6
7
|
/usr/local/mysql/bin/mysqladmin -uroot -ppassword 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定时任务来执行脚本:
1
2
|
[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"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#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停服时自动启动了.
本文转自 marbury 51CTO博客,原文链接:http://blog.51cto.com/magic3/1440963