A lightweight MySQL monitoring on Ubuntu

Submitted by christophe on Fri, 01/04/2016 - 16:47

In some situations, you just need a fast way to monitor MySQL and restart your service asap in case of crash. Neither a production script nor a replacement for a real server monitoring tool (like New Relic, ...) with IP fail over, but a basic solution when you just provisioned a low budget VPS with a very limited setup and still want to get a feedback for further logs details.

1) Install mailutils

It will be necessary to send an e-mail alert.

apt-get install mailutils

2) Create a script that will be called by the cron

Create a file and edit it.

cd /root
mkdir monitoring
cd monitoring
nano mysql.sh

Change the value of MAIL.

#!/bin/bash
MAIL="[email protected]";
UP=$(sudo service mysql status | grep 'mysql start/running' | wc -l);
if [ "$UP" -eq 1 ];
then
  echo "MYSQL is up.";
else
  echo "MySQL is down. Sending alert and trying to restart.";
  mail -s "MySQL server down at $HOSTNAME" "$MAIL" <<< "MySQL server is down and has tried to restart.";
  sudo service mysql start;
fi

Make the file executable.

chmod +x mysql.sh

3) Configure the cron

Edit the global cron.

crontab -e

Paste the following.

0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash /root/monitoring/mysql.sh #MySQL monitoring : alert + try to restart service

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.