Add Monit to monitor MariaDB or MySQL database on a CloudPanel instance on Ubuntu 22.04

Get up and running with Monit on Ubuntu 22.04 on CloudPanel. Monit can be used to monitor your MariaDB or MySQL database and restart it if it is stopped – the database may be stopped due to a lack of server resources, or maybe killed by the OOM-Killer process which occurs when there is a lack of RAM.

Rather than a set and forget process, Monit should be used as a temporary solution whilst trying to find the primary reason why a database service continues to stop.

Install Monit

SSH as root into your VM instance and install Monit

apt update
apt install monit

Start/stop/restart and see the status of monit

systemctl status monit
systemctl start monit
systemctl stop monit
systemctl restart monit

Status will tell if it’s running

root@mel1:~# systemctl status monit
● monit.service - LSB: service and resource monitoring daemon
Loaded: loaded (/etc/init.d/monit; generated)
Active: active (running) since Sat 2023-06-17 16:23:58 AEST; 42s ago

Get the monit version

monit --version

View and tail the monit log

tail -f /var/log/monit.log

Configure Monit

Back up the original monit config file

cp /etc/monit/monitrc /etc/monit/monitrc.bak

Optional Web Access

nano /etc/monit/monitrc

In the config file, look for set httpd port 2812 and uncomment the lines below and set your password.

set httpd port 2812 and
use address 0.0.0.0 # only accept connection from localhost (drop if you use M/Monit)
allow 0.0.0.0/0 # allow localhost to connect to the server and
allow admin:password # require user 'admin' with password 'password'

Open port 2812 on your firewall.

Cloudpanel Port Firewall

Restart monit:

systemctl restart monit

Now you can view via a browser, use your server IP and 2812 port number – use http protocol or configure https for use.

Monit Cloudpanel Remote

You can now also view the Monit stats via the command line with:

monit status

Email Alerts

Email alerts for Monit can also be configured in the monitrc file.

I am using postfix for sending server emails, search for ‘set alert‘ and ‘set mailserver‘ and uncomment and set your email config, there are more email options that can be set in the config file such as sending email for a certain monitoring service.

nano /etc/monit/monitrc

Find and change:

set alert [email protected]            # receive all alerts
set mailserver 127.0.0.1,               # primary mailserver
               localhost                # fallback relay

Check monit config syntax

Any changes you do to the monit config file can be syntax checked with:

monit -t

Choose either MySQL or MariaDB to monitor

Add MySQL service to be watched by Monit

Make a soft link to the mysql config from the monit config available directory to the config enabled directory.

ln -s /etc/monit/conf-available/mysql  /etc/monit/conf-enabled/

Restart monit

systemctl restart monit

Test the monitoring by intentionally stopping MySQL and waiting for 2 minutes’ish

systemctl stop mysql

Your database now should be restarted.

Add MariaDB service to be watched by Monit

After you have Monit up and running, add a monitoring process on MariaDB database which will restart it if it is terminated due to lack of resources.

Change the content of the mysql config file:

/etc/monit/conf-available/mysql

To the following content below which basically changes the content from mysql to mariadb

 check process mysqld with pidfile /var/run/mysqld/mysqld.pid
   group database
   group mysql
   start program = "/etc/init.d/mariadb start"
   stop  program = "/etc/init.d/mariadb stop"
   if failed host localhost port 3306 protocol mysql with timeout 15 seconds for 3 times within 4 cycles then restart
   if failed unixsocket /var/run/mysqld/mysqld.sock protocol mysql for 3 times within 4 cycles then restart
   if 5 restarts with 5 cycles then timeout
   depend mysql_bin
   depend mysql_rc

 check file mysql_bin with path /usr/sbin/mysqld
   group mysql
   include /etc/monit/templates/rootbin

 check file mysql_rc with path /etc/init.d/mariadb
   group mysql
   include /etc/monit/templates/rootbin

Make a soft link to the mysql config from the config available directory to the config enabled directory.

ln -s /etc/monit/conf-available/mysql  /etc/monit/conf-enabled/

Restart monit

systemctl restart monit

That’s it, test the monitoring by intentionally stopping MariaDB and waiting for 2 minutes’ish

systemctl stop mariadb

Fix MySQL logging

If you have your MySQL error logging enabled you will may get a warning output about unauthenticated users which is monit checking the MariaDB service.

Warning] Aborted connection to db: 'unconnected' user: 'unauthenticated' host: 'localhost' (This connection closed normally without authentication)

Fix this by adding a variable in:

/etc/mysql/mariadb.conf.d/50-server.cnf

Add in the logging section:

log_warnings = 1

Restart MariaDB

systemctl restart mariadb

Leave all Comment