I have a problem where MySQL (running on Ubuntu 12.04 with all latest updates) dies after a few days so put this script into a cron job that runs as root every 60 seconds.
Everything works except the mysql service won't restart, I've tried using "service mysql restart" and "/usr/bin/mysqld" and they don't work either.
I am using the full path to restart command so I don't believe that's an issue. Right now the only thing that works is replacing the restart command with a "reboot" and that is very ugly and I want to avoid it.
#!/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="some@email.net"
# 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 some@email.net -t $EMAILID -u MySQL GURU ALARM -m < $MAILMESSAGE
else # MySQL is running :) and do nothing
:
fi
# remove file
rm -f $MAILMESSAGE
Best Answer
Without seeing the startup error log, it would be near impossible to diagnose.
If I had to hazard a guess, it sounds likely to be
ulimit
at fault, the process is probably unable to allocate the memory it needs from a cron, but isn't limited when run via interactive shell. The MySQL log would confirm this, albeit in a non-descript fashion.Also, don't re-invent the wheel with half cocked scripts, just use a recognized monitoring application like
monit