How to control MySQL and SSH service with Monit on Linux

Akademily
3 min readAug 26, 2020

--

How to control MySQL and SSH service with Monit on Linux

If you tried to control services manually on Linux servers then here is the solution for you. First of all you will need to install Monit on Linux.

We can set up Monit to check the Tomcat and MySQL processes. We have to configure for MySQL and Tomcat using Monit. We can also test the Monit and Java processes. We will see how to control MySQL.

It is very easy to configure Monit on Linux and you can find the log file in /var/log/monit. We discussed with you some examples of monitoring using ssh and MySQL services.

SSH CONFIGURATION

We will create a file to monitor the ssh service using the Monit tool.

[root@andrex ~]# vim /etc/monit.d/ssh.conf
check process sshd with pidfile /var/run/sshd.pid
start program "/usr/sbin/sshd start"
stop program "/usr/sbin/sshd stop"
if failed host 127.0.0.1 port 22 protocol ssh then restart
if 5 restarts within 5 cycles then timeout

The above code will monitor the ssh services and restart the services on failure. It will try 5 times.

You can check the status if it works or is configured correctly using the command.

Process 'sshd'
status OK
monitoring status Monitored
monitoring mode active
on reboot start
pid 7101
parent pid 1
uid 0
effective uid 0
gid 0
uptime 1h 54m
threads 1
children 3
cpu 0.0%
cpu total 0.0%
memory 0.0% [468 kB]
memory total 0.3% [2.8 MB]
security attribute system_u:system_r:sshd_t:s0-s0:c0.c1023
disk read 0 B/s [116.6 MB total]
port response time 34.733 ms to 127.0.0.1:22 type TCP/IP protocol SSH
data collected Tue, 05 May 2020 14:22:32

MYSQL CONFIGURATION

We can also control the MySQL server as well as with this tool. Create a file to monitor MySQL.

  • Note:
    You can change the location of the configuration file according to your server.

[root@andrex ~]# vim /etc/monit.d/mysql.conf
check process mysqld with pidfile /var/run/mysqld/mysqld.pid
group database
start program = "/etc/init.d/mysqld start"
stop program = "/etc/init.d/mysqld stop"
if failed host 127.0.0.1 port 3306 then restart
if 5 restarts within 5 cycles then timeout

We can also check the configuration before rebooting the monitoring service.

[root@andrex ~]# monit -t # check the configuration before rebooting the service
[root@andrex ~]#get reload # reboot the configuration to make changes

You can check the PID location and run the program, it may be different on your computer.

Check the status by using the monitoring status

Process 'mysqld'
status OK
monitoring status Monitored
monitoring mode active
on reboot start
pid 60708
parent pid 60504
uid 27
effective uid 27
gid 27
uptime 0m
threads 22
children 0
cpu 0.1%
cpu total 0.1%
memory 45.3% [440.2 MB]
memory total 45.3% [440.2 MB]
security attribute system_u:system_r:mysqld_t:s0
disk read 0 B/s [13.7 MB total]
disk write 0 B/s [64 kB total]
port response time 0.924 ms to 127.0.0.1:3306 type TCP/IP protocol DEFAULT
data collected Tue, 05 May 2020 14:22:32

Now we have configured SSH and MySQL services to monitor and automatically restart on failure.

TEST CONFIGURATION

We will stop the MySQL service manually to check if it works or not.

As soon as it is done. You check the status by using the monitoring status

Process 'mysqld'
status Initializing
monitoring status Initializing
monitoring mode active
on reboot start
data collected Tue, 05 May 2020 14:39:08

You will see that it will be in the process of initialization for a few seconds and then you will check the state of MySQL service.

The service will be restarted automatically.

Process 'mysqld'
status OK
monitoring status Monitored
monitoring mode active
on reboot start
pid 61687
parent pid 61482
uid 27
effective uid 27
gid 27
uptime 0m
threads 22
children 0
cpu 0.1%
cpu total 0.1%
memory 45.3% [440.2 MB]
memory total 45.3% [440.2 MB]
security attribute system_u:system_r:mysqld_t:s0
disk write 0 B/s [60 kB total]
port response time 0.566 ms to 127.0.0.1:3306 type TCP/IP protocol DEFAULT
data collected Tue, 05 May 2020 14:40:11

If you have set up access with GUI for Monit monitoring panel. You can also check the status there.

--

--

Akademily
Akademily

Written by Akademily

We conduct reviews, guides and comparative tests of gaming laptops, monitors, graphics cards, keyboards, mouses, headsets and chairs to help you buy the best ga

No responses yet