mysqld killed by the OOM killer is one of the worst failure modes — silent shutdown, dirty pages to recover on restart, often a crash recovery that takes longer than the original outage. This article is the memory math we apply when sizing MySQL on a host: how much the buffer pool gets, how the per-connection buffers stack up, what Performance Schema actually consumes, and how a cgroup limit gives the operator a guard rail before the OOM killer wakes up.
How to verify
Measure actual memory consumption, not just configured limits. mysqld allocates lazily; what my.cnf allows is not what is currently in use.
mysql -e "SHOW VARIABLES LIKE 'innodb_buffer_pool_size';"
mysql -e "SHOW VARIABLES LIKE 'max_connections';"
ps -o pid,rss,vsz,cmd -C mysqld
sudo cat /proc/$(pgrep -f mysqld | head -1)/status | grep -E '(VmRSS|VmHWM|VmPeak)'
sudo dmesg | grep -i 'killed process\|oom-killer'
mysql -e "SELECT * FROM sys.memory_global_total;"
mysql -e "SELECT event_name, current_alloc FROM sys.x\$memory_global_by_current_bytes ORDER BY current_alloc DESC LIMIT 20;"
VmRSS is the resident memory MySQL is actually using right now. VmHWM is its all-time high-water mark; if it is approaching the host’s RAM, the OOM killer is in scope.
What’s happening
MySQL’s memory usage decomposes into two buckets. Global allocations — buffer pool, log buffer, key buffer, Performance Schema, query cache (deprecated), connection handle structures — happen at startup and grow during operation. Per-connection allocations — sort buffer, join buffer, read buffer, read random buffer, tmp table size — are allocated per connection per query.
The formula that operators carry around in their heads:
mysqld_memory_ceiling ≈ innodb_buffer_pool_size
+ log_buffer_size
+ (per_thread_buffers × max_connections)
+ performance_schema_size
+ thread_stack × max_connections
+ table_cache_overhead
In practice on a modern 64 GB host running MySQL 8 with default Performance Schema, mysqld consumes roughly the buffer pool plus 4-8 GB of overhead. The trap is the per-connection buffers: sort_buffer_size=8M, join_buffer_size=8M, read_buffer_size=2M, read_rnd_buffer_size=4M, tmp_table_size=64M adds up to 86 MB per connection. With max_connections=500, that is 43 GB of potential per-connection memory that MySQL may allocate during peak traffic.
The kernel’s OOM killer does not care about MySQL’s caching nuance. When the host runs out of memory, it kills the process with the largest oom_score, which is almost always mysqld. A cgroup memory limit (via systemd) gives MySQL a hard ceiling: when it hits the limit, the cgroup OOM killer terminates mysqld predictably instead of letting the kernel pick a victim at random and possibly take other processes with it.
The procedure
-
Decide the buffer pool size. On a dedicated database host, the rule of thumb is 50-75% of RAM, leaving room for the OS, the per-connection buffers, and Performance Schema. On a 64 GB host running only MySQL, 40 GB is a sensible buffer pool.
[mysqld] innodb_buffer_pool_size = 40G innodb_buffer_pool_instances = 8 innodb_buffer_pool_chunk_size = 128MMultiple instances let allocations and evictions run in parallel, reducing mutex contention on a high-concurrency workload.
-
Size the per-connection buffers down to what the workload actually needs. The defaults are sized for a desktop, not a production server.
sort_buffer_size = 2M join_buffer_size = 2M read_buffer_size = 256K read_rnd_buffer_size = 512K tmp_table_size = 32M max_heap_table_size = 32M -
Cap
max_connectionsto what the application actually opens, plus headroom. A common mistake is leavingmax_connections=1000when the application’s connection pool tops out at 200.max_connections = 300 thread_stack = 256K -
Tune Performance Schema down if the default footprint is too large. See /en/knowledge-base/mysql-performance-schema-tuning/ for the details.
performance_schema_max_table_instances = 5000 performance_schema_events_statements_history_long_size = 1000 -
Set a cgroup memory limit at the systemd unit level. This is the guard rail that turns “host OOM” into “cgroup OOM” — only
mysqlddies, not random other processes.# /etc/systemd/system/mysql.service.d/memory.conf [Service] MemoryMax=56G MemorySwapMax=0Reload and restart.
sudo systemctl daemon-reload sudo systemctl restart mysql systemctl show mysql | grep -i memory -
Disable swap or pin MySQL to not swap. A MySQL process whose buffer pool pages get swapped out is no longer caching anything; query latency goes up by orders of magnitude before the OOM killer arrives.
sudo sysctl vm.swappiness=1 echo "vm.swappiness=1" | sudo tee /etc/sysctl.d/99-mysql.conf -
Monitor. Plot
VmRSSand the buffer pool hit ratio over time. A buffer pool that consistently runs at 100% hit ratio is sized correctly; one that misses is too small for the working set.SELECT (1 - ( SELECT VARIABLE_VALUE FROM performance_schema.global_status WHERE VARIABLE_NAME='Innodb_buffer_pool_reads' ) / ( SELECT VARIABLE_VALUE FROM performance_schema.global_status WHERE VARIABLE_NAME='Innodb_buffer_pool_read_requests' )) * 100 AS hit_ratio_pct;
Common pitfalls
innodb_buffer_pool_size=90% of RAM— leaves no room for per-connection buffers, the OS, or backups. The first burst of connections takes the host OOM.max_connections=10000on a small host —thread_stack × 10000is multi-gigabyte even at default 256K. Reserve for what the app actually opens.- No cgroup limit — when MySQL grows past available RAM, the kernel OOM killer picks the largest process. That is
mysqld, but the kernel does not know the difference between MySQL and an evicting process; the choice can be unpredictable. tmp_table_sizemuch smaller thanmax_heap_table_size— temporary tables larger than the smaller of the two go to disk. Keep them equal.- Forgetting Performance Schema’s memory — on default settings, P_S can consume 1-2 GB of RAM. On memory-constrained hosts, prune it.
OOM tuning is the difference between a database that runs hot in steady state and one that survives the traffic spike. In the engagements we run, the memory math is part of the host’s MySQL build, the cgroup limit is in place, and the buffer pool hit ratio plus VmRSS are charts on the dashboard our operators look at first. For the broader pattern of operating MySQL with this care, see /en/services/managed-operations/.