检查mysql是否成功启动的方法(bat+bash)

发布时间:2017-07-08 10:57:21编辑:丝画阁阅读(821)

linux系统中通过shell命令实现

检测 MySQL 是否宕掉,如果宕掉则启动

如果你的 MySQL 经常宕机,可以用这个脚本来实现宕机后自动启动,把它加到 crontab 里执行即可。
详细介绍请看 http://www.codeproject.com/Articles/988967/Mysql-Uptime-Check-Script


#!/bin/bash
  
result=`/usr/bin/mysqladminping`
expected='mysqld is alive'
  
if[["$result"!="$expected"]]
then
echo"It's dead - restart mysql"
  
# email subject
SUBJECT="[MYSQL ERROR] - Attempting to restart service"
  
# Email To ?
EMAIL="info@endyourif.com"
  
# Email text/message
EMAILMESSAGE="/tmp/emailmessage.txt"
echo"$result was received"> $EMAILMESSAGE
echo"when we were expected $expected">>$EMAILMESSAGE
# send an email using /bin/mail
mail -s"$SUBJECT""$EMAIL"< $EMAILMESSAGE
  
sudo/etc/init.d/mysqlrestart
fi





#!/bin/bash
#mysql check
PORT="0"
PORT=`netstat-lnt |grep3306 |wc-l `
echo$PORT
if[ $PORT -eq1 ]
 then
echo"mysql is running"
else
echo"mysql is not running"
echo"progrome reeady to start mysql "
 
sudoservice mysql start
./check_mysql.sh
fi


#!/bin/bash
pgrep mysqld &> /dev/null
if [ $? -gt 0 ]
then
echo "`date` mysql is stop" >> /home/wwwlogs/mysql_listen.log
sudo /etc/init.d/mysql start
echo "`date` mysql to running" >> /home/wwwlogs/mysql_listen.log
else
echo "`date` mysql running" >> /home/wwwlogs/mysql_listen.log
fi







关键字