Rizwan Rizwan
HOME SERVICES CASE STUDIES BLOG ABOUT $100 USD FREE Consultation Call →
DEVOPS

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.

Rizwan Rizwan May 27, 2025 5 min read
How to Automatically Restart MySQL Database on Low Memory DigitalOcean Droplets

Table of Contents

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.

Rizwan

About Rizwan

Full-stack developer and technical leader with 13+ years building scalable web applications. I help agencies and startups ship faster through strategic guidance and hands-on development.

MORE INSIGHTS

Keep reading for more development wisdom.

DevOps

Ultimate LEMP Stack Installation Guide for Ubuntu

In this article, you'll install PHP 8.3, NGINX, MySQL, PHPMyAdmin, SSL, Composer and NPM on a fresh Ubuntu installation.

Rizwan Rizwan July 19, 2025 5 min read
DevOps

How to Set Up a Secure Deploy User for GitHub Actions (And Why You Should Never Use Root)

Avoid root in Laravel CI/CD. Here’s how to create a secure "deploy" user for GitHub Actions, with exact commands and a full checklist.

Rizwan Rizwan July 13, 2025 5 min read
DevOps

Redis Object Cache on OpenLiteSpeed: Halve WooCommerce TTFB

Learn how Redis object cache on OpenLiteSpeed can cut WooCommerce Time-to-First-Byte by up to 50%. Follow this step-by-step guide with real benchmarks.

Rizwan Rizwan May 27, 2025 5 min read

HOW I CAN HELP YOU

I work with founders, agencies, and developers who need that extra push to get projects live. Whether it's fixing a stubborn bug, steering your tech strategy, or building full apps with my team. You don't have to do it alone

GET UNSTUCK

60-minute call to debug your specific problem. Stop spinning your wheels.

$249
BOOK NOW →

FRACTIONAL CTO

Ongoing strategic guidance to prevent disasters like this.

$2k-7k/mo
LEARN MORE →

CUSTOM DEV

Full project rebuild with our expert team. Done right the first time.

Custom
GET QUOTE →