第一种方法:利用mysql命令查看
MySQL 内建直接看 status 就可以看到系统常见讯息, 如下述范例:
检查mysql是否成功启动
#!/bin/bash#mysql checkPORT="0"PORT=`netstat -lnt | grep 3306 | wc -l `echo $PORTif [ $PORT -eq 1 ] thenecho "mysql is running"elseecho "mysql is not running"echo "progrome reeady to start mysql "sudo service mysql start./check_mysql.shfi |
检测 MySQL 是否宕掉,如果宕掉则启动
如果你的 MySQL 经常宕机,可以用这个脚本来实现宕机后自动启动,把它加到 crontab 里执行即可。
详细介绍请看 http://www.codeproject.com/Articles/988967/Mysql-Uptime-Check-Script
#!/bin/bash result=`/usr/bin/mysqladmin ping`expected='mysqld is alive' if [[ "$result" != "$expected" ]]thenecho "It's dead - restart mysql" # email subjectSUBJECT="[MYSQL ERROR] - Attempting to restart service" # Email To ?EMAIL="info@endyourif.com" # Email text/messageEMAILMESSAGE="/tmp/emailmessage.txt"echo "$result was received"> $EMAILMESSAGEecho "when we were expected $expected" >>$EMAILMESSAGE# send an email using /bin/mailmail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE sudo /etc/init.d/mysql restartfi |
Mysql bat启动和关闭服务bat版
@echo offfor /f "skip=3 tokens=4" %%i in ('sc query mysql') do set "zt=%%i" &goto :next:nextif /i "%zt%"=="RUNNING" (echo 已经发现该服务在运行,正在已经关闭服务net stop mysql) else (echo 该服务现在处理停止状态,正在现在开启服务net start mysql)exitpause |
经测试有时候不是很好用。
新闻热点
疑难解答