How to Automatically Restart MySQL Database on Low Memory DigitalOcean Droplets

Safeguard your WordPress site on a low-memory DigitalOcean droplet by setting up an automatic MySQL monitoring and restart service to minimize downtime and keep your site running smoothly.

Published by Rizwan on May 8, 2024
Edited by Rizwan on August 27, 2024

If you’re running a WordPress website on a DigitalOcean droplet with limited memory, you’ve likely encountered MySQL server crashes. These crashes can disrupt your site and require manual intervention to restart MySQL, causing downtime and potential loss of visitors. To address this issue, I’ll show you how to create a system daemon (service), that automatically monitors and restarts MySQL if it stops running.

Why MySQL Crashes on Low Memory

MySQL may crash on low-memory servers because when the system runs out of RAM, it starts killing processes to free up memory. MySQL, being a memory-intensive application, often becomes a target. Frequent crashes not only affect your site’s availability but also its performance and reliability.

So, the best way is to actually increase the memory of your droplet but if you can’t, continue to read and I’ll show you how to monitor MySQL and restart it automatically if it crashes.

Creating a System Daemon to Monitor and Restart MySQL

To ensure your MySQL service remains running, we will create a systemd service that checks the status of MySQL and restarts it if it’s not active. Here’s how you can set it up:

1. Create a Custom Service File

Open your terminal and create a new service file in the /etc/systemd/system/ directory.

sudo nano /etc/systemd/system/mysql-monitor.service

2. Edit the Service File

Add the following content to the file. This script will continuously check if MySQL is active every minute, and if it’s not, it will restart the service.

[Unit]
Description=MySQL Monitor
After=network.target

[Service]
Type=simple
User=root
ExecStart=/bin/bash -c 'while true; do if ! systemctl is-active --quiet mysql; then systemctl restart mysql; fi; sleep 60; done'

[Install]
WantedBy=multi-user.target

3. Enable and Start Your Service

Enable the newly created service to start on boot and activate it immediately with the following commands:

sudo systemctl enable mysql-monitor
sudo systemctl start mysql-monitor

By setting up this service, your MySQL will automatically restart if it crashes, reducing downtime and improving the reliability of your WordPress site.

Conclusion

Running a WordPress site on a low-memory DigitalOcean droplet doesn’t have to mean frequent MySQL crashes and manual restarts. By implementing a simple system daemon, you can ensure that your MySQL service remains active, keeping your site up and running smoothly. This solution not only saves time but also improves your site’s overall uptime and user experience.

How we reviewed this article:

  • Content Process
My process of publishing articles include:
  1. I get an Idea
  2. Get feedback from fellow developers if they want a detailed post on it
  3. Research existing blog posts to see if there's a well written article on it
  4. Write the post
  5. Get feedback from colleagues, improve and publish!