无法通过 cron 任务重启 mysql

无法通过 cron 任务重启 mysql

我遇到一个问题,MySQL(在 Ubuntu 12.04 上运行,包含所有最新更新)在几天后死机,因此将此脚本放入每 60 秒以 root 身份运行一次的 cron 作业中。

除了 mysql 服务无法重新启动之外,一切正常,我尝试使用“service mysql restart”和“/usr/bin/mysqld”,但它们也不起作用。

我使用完整路径来重新启动命令,所以我认为这不是问题。目前唯一可行的方法是用“rebo​​ot”替换重新启动命令,这非常丑陋,我想避免它。

#!/bin/bash
#Check if MySQL is up, if not then start it
# mysql root/admin username
MUSER="root"
# mysql admin/root password
MPASS="REMOVED"
# mysql server hostname
MHOST="localhost"
#Shell script to start MySQL server i.e. path to MySQL daemon start/stop script.
# Debain uses following script, need to setup this according to your UNIX/Linux/BSD OS.
MSTART="/etc/init.d/mysql restart"
# Email ID to send notification
EMAILID="[email protected]"
# path mysqladmin
MADMIN="$(which mysqladmin)"

MAILMESSAGE="/tmp/mysql.fail.$$"

# see if MySQL server is alive or not

$MADMIN -h $MHOST -u $MUSER -p${MPASS} ping 2>/dev/null 1>/dev/null
if [ $? -ne 0 ]; then
        echo "" >$MAILMESSAGE
        echo "ALERT: MySQL Server is not responding to ping">>$MAILMESSAGE
        echo "Hostname: $(hostname)" >>$MAILMESSAGE
        echo "System Time: $(date)" >>$MAILMESSAGE
        # try to start mysql
        $MSTART>/dev/null
        # see if it is started or not
        o=$(ps cax | grep -c ' mysqld$')
        if [ $o -eq 1 ]; then
                sMess="MySQL server successfully restarted"
        else
                sMess="MySQL server FAILED to restart"
        fi
        # Email status too
        echo "Current Status: $sMess" >>$MAILMESSAGE
        echo "" >>$MAILMESSAGE
        echo "*** This email generated by $(basename $0) shell script ***" >>$MAILMESSAGE
        echo "*** Please don't reply this email, this is just notification email ***" >>$MAILMESSAGE
        # send email
        sendemail -o message-content-type=text -f [email protected] -t $EMAILID -u MySQL GURU ALARM -m < $MAILMESSAGE
else # MySQL is running :) and do nothing
        :
fi
# remove file
rm -f $MAILMESSAGE

答案1

如果没有查看启动错误日志,几乎不可能进行诊断。

如果我不得不猜测的话,这听起来可能是ulimit错误的,该进程可能无法从 cron 分配所需的内存,但通过交互式 shell 运行时不受限制。MySQL 日志将证实这一点,尽管方式不明确。

另外,不要用半生不熟的脚本重新发明轮子,只需使用公认的监控应用程序,例如monit

相关内容