When MySQL dies for no obvious reason — sites throw “Error establishing a database connection” for a minute, then recover, and the MySQL error log shows a clean startup with no crash before it — the culprit is often not MySQL at all. It is the Linux out-of-memory killer. When the server runs out of usable RAM, the kernel does not politely ask processes to free memory; it picks the process with the highest “badness” score (usually the biggest one, which is frequently mysqld) and kills it outright to keep the system alive. mysqld then restarts via its service manager, the site recovers, and the cycle repeats at the next memory pressure peak. The fix is to stop the server from running out of memory in the first place, and that means finding what is consuming it.
What’s happening
Linux over-commits memory: it lets processes allocate more than physically exists, betting that not all of it will be used at once. When that bet fails and the system genuinely runs out of RAM (and swap), the OOM killer activates. It scores every process by memory footprint and a few heuristics, then kills the highest scorer to reclaim the most memory fastest. mysqld, holding a large InnoDB buffer pool and per-connection buffers, is often the single biggest process on a web server, which makes it the OOM killer’s frequent victim — even though it is rarely the cause of the shortage.
The real cause is usually something else eating memory until there is none left for everyone: too many PHP-FPM workers (each one holding tens of megabytes, multiplied across pools and accounts), a memory-leaking process, a backup or import buffering too much, or simply a server whose committed memory (MySQL buffers + max PHP workers + everything else) exceeds physical RAM under peak load. There is also the misconfiguration case: innodb_buffer_pool_size set so large that MySQL itself, plus normal web load, cannot fit. The diagnostic signature is unmistakable — Out of memory: Killed process <pid> (mysqld) in the kernel log — and once you see it, the question becomes what filled the RAM, not why did MySQL crash.
How to verify
Confirm it is the OOM killer, not a MySQL crash — the kernel log is definitive:
# The smoking gun — kernel killing a process to reclaim memory
dmesg -T | grep -iE 'out of memory|killed process|oom'
grep -iE 'oom|killed process' /var/log/messages | tail -20
# MySQL's own log will show a clean restart with NO crash before it (= killed externally)
tail -40 /var/lib/mysql/$(hostname).err
See the memory picture: how much RAM and swap, and what is committed:
# Current memory and swap headroom
free -m
# Committed vs available — over-commit is where OOM lives
grep -E 'Commit|MemAvailable|SwapTotal' /proc/meminfo
Find what is actually consuming the memory — usually PHP workers or MySQL buffers:
# Top memory consumers right now
ps -eo pid,user,rss,comm --sort=-rss | head -15
# How many PHP workers, and their combined footprint
ps -C lsphp,php-fpm -o rss= | awk '{s+=$1} END {print s/1024 " MB across " NR " procs"}'
# MySQL's configured buffer pool vs physical RAM
mysql -e "SHOW VARIABLES LIKE 'innodb_buffer_pool_size';"
The fix
- Confirm the OOM killer is the cause — the
dmesg/messageslineOut of memory: Killed processis proof; a MySQL error log that shows a clean restart with no preceding crash confirms mysqld was killed from outside, not that it failed. If there is no OOM line, the problem is a genuine MySQL crash and a different investigation — do not chase memory. - Find what is consuming RAM, not just what got killed — mysqld is usually the victim, not the cause. The
ps --sort=-rssand the PHP-worker footprint tell you where the memory actually went. The fix targets the consumer (often PHP-FPM workers in aggregate), not the process the kernel happened to kill. - Cap PHP-FPM workers to fit physical memory — the most common real cause is
pm.max_childrenset too high across pools, so peak concurrency allocates more memory than exists. Size each pool’spm.max_childrenagainst per-worker RSS and available RAM: total workers × average worker size must leave room for MySQL and the OS. On a multi-account host, the sum across all pools is what matters. Trimming this is usually what stops the OOM cycle. - Right-size
innodb_buffer_pool_sizedownward if it is too large — a buffer pool set to most of RAM leaves nothing for PHP and the OS, guaranteeing OOM under load. Set it to the size of the hot dataset within a budget that coexists with peak web load — a common safe figure is well under half of RAM on a shared web+DB host, not the “70% of RAM” advice meant for dedicated database servers. This is the misconfiguration case, and it is a one-line fix. - Add or enlarge swap as a shock absorber — a server with little or no swap has no cushion when memory spikes briefly; the OOM killer fires immediately. A reasonable swap file (and a moderate
vm.swappiness) gives transient peaks somewhere to go instead of triggering a kill. Swap is not a substitute for enough RAM — a server that lives in swap will thrash — but it prevents a momentary spike from killing MySQL. - Protect mysqld from the OOM killer if it is wrongly targeted — when MySQL is the victim but not the cause, you can lower its OOM score so the kernel kills a less critical process first: set
OOMScoreAdjust=-600in the MySQL systemd unit (viasystemctl edit mysql). This is a guardrail, not a cure — it redirects the kill rather than preventing the shortage — so pair it with fixing the actual memory consumer. - Confirm the cycle stops under real load — after capping workers and right-sizing buffers, watch
free -manddmesgacross a full traffic peak. No newKilled processlines and stable available memory under load mean it is fixed. If OOM recurs, either the workers are still over-provisioned or the server genuinely needs more RAM for its workload — at which point more memory, not more tuning, is the answer.
OOM-killed MySQL is a memory-budget problem wearing a database-crash costume: the kernel reclaimed RAM by shooting the biggest process, but the real fault is total committed memory exceeding what exists. Find the consumer, cap PHP workers, size the buffer pool to coexist, and give the system a little swap cushion. Stack Harbor diagnoses memory exhaustion and OOM cycles under managed cPanel operations — start a conversation when MySQL keeps getting killed.