Shell script to restart MySQL server if it is killed or not working

A simple Linux and Unix bash shell script that would restart your MySQL or MariDB server if it is killed or not working due to ANY reasons.

#!/bin/bash
# Shell script to restart MySQL server if it is killed or not working 
# due to ANY causes.
# When script detects mysql is not running (it basically sends ping request 
# to MySQL) it try to start using /etc/init.d/mysql script; and it sends an 
# email to user indicating the status.
# This script must be run from Cron Job so that it can monitor mysql server. 
# For more info visit following url:
# https://www.cyberciti.biz/tips/linux-mysql-server-monitoring.html
# --------------------------------------------------------------------------
# Copyright (C) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
# mysql root/admin username
MUSER="root"
# mysql admin/root password
MPASS="SET-ROOT-PASSWORD"
# 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 start"
# Email ID to send notification
EMAILID="notification@somewhere-corp.com"
# path to mail program 
MAILCMD="$(which mail)"
# path mysqladmin
MADMIN="$(which mysqladmin)"
 
#### DO NOT CHANGE anything BELOW ####
MAILMESSAGE="/tmp/mysql.fail.$$"
 
# see if MySQL server is alive or not
# 2&1 could be better but i would like to keep it simple and easy to
# understand stuff :)
$MADMIN -h $MHOST -u $MUSER -p${MPASS} ping 2>/dev/null 1>/dev/null
if [ $? -ne 0 ]; then
	echo "" >$MAILMESSAGE
	echo "Error: MySQL Server is not running/responding ping request">>$MAILMESSAGE
	echo "Hostname: $(hostname)" >>$MAILMESSAGE
	echo "Date & 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 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
	$MAILCMD -s "MySQL server" $EMAILID < $MAILMESSAGE
else # MySQL is running :) and do nothing
	:
fi
# remove file
rm -f $MAILMESSAGE
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
CategoryList of Unix and Linux commands
File Management cat
Firewall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN CentOS 8 Debian 10 Firewall Ubuntu 20.04
7 comments… add one
  • simbah ronggo Apr 23, 2014 @ 19:35

    #!/bin/bash
    email=’johndoe@doe.com’
    sub_mysql=’mysql proses down and up’
    MYSQL_START=’/etc/init.d/mysql start’
    MYSQL=’mysql’
    PGREP=’/usr/bin/pgrep’
    #check pid
    $PGREP $MYSQL
    if [ $? -ne 0 ]; then
    $MYSQL_START | mail -s “$sub_mysql” $email
    fi

  • Mgreen Oct 23, 2012 @ 4:35

    Would work better if it check that it could accept commands.

    Something Like….

    mysql -Dmysql -e “SELECT 1;”

    ;)

  • Ed Jul 15, 2012 @ 18:59

    Hello

    Thanks for this nice script.
    I’m quite new at server administration, and have 2 questions about your script :
    1- What do you mean by :
    # path to mail program
    MAILCMD=”$(which mail)”
    # path mysqladmin
    MADMIN=”$(which mysqladmin)”

    Do I need to enter the path of both mail and mysql servers ?

    2- If I enter wrong information for the password (and I leave the default intel as question 1), it works …
    So are login and pawd required ?
    Do it works only I 1- is correctly filled ?

    (I’m working with a ovh release 2 (gentoo))

    Thanks
    Ed

  • Meskalyn May 6, 2009 @ 1:21

    Very, very nice script.
    Simple, but so usefull ;)

  • Murat Çabuk Apr 28, 2009 @ 9:33

    Thank you. Best Shell Script.

  • Ahmet Alp Apr 23, 2009 @ 18:42

    Very Good. Thank you some much.

  • Branislav Viest May 27, 2008 @ 15:11

    very good a simple. thx

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.