Fixing MySQL Crashes Due to Low RAM: A Complete Guide

by | Mar 10, 2025 | Featured, My Blog & Thoughts, My Research & Exploration, My Tech & Innovation | 1 comment

Issue:

If MySQL keeps crashing or stopping unexpectedly on a low-RAM server, the most likely culprit is the Out of Memory (OOM) killer. This happens when the system runs out of available RAM and forcefully stops MySQL to keep the server running.

In this guide, we’ll go through step-by-step solutions to stabilize MySQL, optimize memory usage, and configure swap space properly.

Step 1: Verify MySQL Crash Logs

Before making any changes, confirm that MySQL is being killed due to memory issues. Run the following command to check logs:

journalctl -u mysql --since "1 hour ago"

If you see something like:

mysql.service: A process of this unit has been killed by the OOM killer.

It confirms that the system ran out of memory, and MySQL was terminated.

Step 2: Check RAM & Swap Usage

Run:

free -m

If the output shows very little free memory and swap is not being used, we need to enable swap properly.

Example output before fixing swap:

Mem:   960MB total,  727MB used,  84MB free,  233MB available
Swap: 1024MB total,   0MB used, 1024MB free

Step 3: Configure Swap Space

1️⃣ Check Current Swap File

swapon --show

If swap is not being used, increase it:

2️⃣ Create a Larger Swap File (2GB)

sudo swapoff -a  # Turn off swap temporarily
sudo fallocate -l 2G /swapfile  # Allocate 2GB file
sudo chmod 600 /swapfile  # Secure the file
sudo mkswap /swapfile  # Create swap space
sudo swapon /swapfile  # Activate swap

Verify:

swapon --show

To make it permanent, add to /etc/fstab:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Step 4: Adjust Swappiness for Better Performance

Swappiness controls how aggressively the system uses swap. A low value (10-40) means swap is used only when necessary.

1️⃣ Check Current Swappiness

cat /proc/sys/vm/swappiness

If it’s too low (e.g., 10), increase it:

2️⃣ Set Swappiness to 80

sudo sysctl vm.swappiness=80

Make it permanent:

echo 'vm.swappiness=80' | sudo tee -a /etc/sysctl.conf
sudo sysctl --system

Reboot:

sudo reboot

Verify:

cat /proc/sys/vm/swappiness  # Should show 80

Step 5: Optimize MySQL Memory Usage

Since MySQL consumes a lot of RAM, reducing its memory limits helps prevent crashes.

Edit MySQL config:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Add or modify these settings:

innodb_buffer_pool_size = 128M
max_connections = 30
query_cache_size = 16M

Restart MySQL:

sudo systemctl restart mysql

Final Check: Verify Stability

Run:

watch -n 5 free -m

Now, swap should be in use, and MySQL should no longer be killed.

MySQL Running SmoothlySwap Being Used EfficientlyRAM Optimized for Stability

If MySQL crashes again, check logs:

dmesg | grep -i "oom"
journalctl -u mysql --since "1 hour ago"

Conclusion

With these optimizations, MySQL should now run without unexpected crashes even on a low-memory server. Keep an eye on logs and memory usage, and fine-tune settings as needed for better performance. 🚀

Written by

Related Posts

1 Comment

  1. Rajesh Manikonda

    Nice explanation. very useful for small companies

Submit a Comment