%title缩略图

MySQL宕机自动重启脚本

脚本功能:

检测MySQL端口是否正常

使用账号链接数据库并执行“show databases”,如以上都正常则数据库运行正常,不正常会记录log并重启,重启失败发送邮件给管理者。

#!/bin/bash  
#/usr/bin/nmap localhost | grep 3306  
#lsof -i:3306  
MYSQLPORT=`netstat -na|grep "LISTEN"|grep "3306"|awk -F[:" "]+ '{print $5}'`  
 
function checkMysqlStatus(){  
    /usr/bin/mysql -uroot -p11111 --connect_timeout=5 -e "show databases;" &>/dev/null 2>&1  
    if [ $? -ne 0 ]  
    then  
        restartMysqlService  
        if [ "$MYSQLPORT" == "3306" ];then  
            echo "mysql restart successful......"  
        else  
            echo "mysql restart failure......"  
            echo "Server: $MYSQLIP mysql is down, please try to restart mysql by manual!" > /var/log/mysqlerr  
            #mail -s "WARN! server: $MYSQLIP  mysql is down" admin@yourdomain.com < /var/log/mysqlerr  
        fi  
    else  
        echo "mysql is running..."  
    fi  
}  
 
function restartMysqlService(){  
    echo "try to restart the mysql service......"  
    /bin/ps aux |grep mysql |grep -v grep | awk '{print $2}' | xargs kill -9  
    service mysql start  
}  
 
if [ "$MYSQLPORT" == "3306" ]  
then  
    checkMysqlStatus  
else  
    restartMysqlService  
fi

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

8 + 12 =

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据