max_connections defaults to 151 on MySQL and 100 on MariaDB. Both are wrong for a typical cPanel server, in opposite directions. On a 200-account host where every account runs PHP-FPM with persistent connections, 151 saturates and clients get “Too many connections” within minutes of peak traffic. On a small VPS with 1GB of RAM, 151 connections each consuming ~10MB is bigger than the host’s memory, and the first burst pushes MySQL into OOM and a server reboot. The right value comes from counting, not from copying.
What’s happening
Each MySQL connection consumes memory for connection buffers, sort buffers, join buffers, and read buffers — roughly 1–12 MB depending on workload. The host needs to fit all simultaneous connections in RAM plus the InnoDB buffer pool plus the OS plus everything else. Set max_connections higher than what RAM can hold and a burst causes OOM. Set it too low and legitimate traffic gets refused.
cPanel doesn’t help you size this — it just installs the daemon default. The real input is: how many PHP-FPM children, how many persistent connections each, how many concurrent web requests can the server actually serve.
How to verify
mysql -e "SHOW VARIABLES LIKE 'max_connections'"
mysql -e "SHOW STATUS LIKE 'Max_used_connections%'"
mysql -e "SHOW STATUS LIKE 'Threads_connected'"
mysql -e "SHOW STATUS LIKE 'Connection_errors_max_connections'"
mysql -e "SHOW VARIABLES LIKE 'innodb_buffer_pool_size'"
free -m
grep -E "pm.max_children|pm.start_servers" /etc/php-fpm.d/*.conf | head
Max_used_connections shows the historical peak. Connection_errors_max_connections non-zero means you’ve already hit the cap. The PHP-FPM children counts give you a workload-based estimate.
The fix
- Calculate the workload ceiling. For PHP-FPM on cPanel, sum
pm.max_childrenacross all configured pools. A 200-account host withpm.max_children=20per account is 4000 potential children — but in practice, traffic correlates and you’ll never see all at once. A reasonable peak is 30% of the sum, so 1200 concurrent. Add 10% headroom for cron, mail, and management connections: 1320. Round up:max_connections = 1500. - Check that RAM supports the value. Each connection consumes around 4 MB on a default cPanel install. 1500 × 4 MB = 6 GB just for connection memory. Plus
innodb_buffer_pool_size(which should be 60–70% of RAM on a dedicated MySQL host). Plus OS and other services. If your host has 8 GB RAM, you cannot support 1500 connections; cap it lower or raise RAM. - Set the value persistently:
Restart MySQL. Or change live:/etc/my.cnf.d/cpanel.cnf: [mysqld] max_connections = 500SET GLOBAL max_connections = 500. The live setting reverts on restart unless you also write it to config. - Set per-user caps to prevent monopolization (separate article). With
max_connections = 500andmax_user_connections = 50, no single account can take more than 10% of the pool. Combined, they protect against both “one runaway account” and “every account moderately busy.” - Watch for
Threads_connectedplateaus. If under peak traffic this stays well belowmax_connections, you have headroom. If it plateaus exactly atmax_connections, you’re capped and clients are getting refused. The fix is more RAM and more connections, or shorter-lived connections (don’t use persistent connections in PHP if you can avoid it). - Add a connection pool when raising the value won’t help. ProxySQL or PgBouncer-style pooling lets 5000 PHP children share a pool of 200 MySQL connections. The pool waits when full; the application waits a few milliseconds rather than failing. This is a real architectural change, not a config knob.
- Reload, do not restart, where possible.
FLUSH HOSTSand dynamicSET GLOBALcover most changes. A full restart drops every active connection.
For environments where MySQL connection tuning needs to be reactive to changing workloads, Stack Harbor’s environment management team correlates Threads_connected against application traffic and adjusts caps before the next traffic peak surfaces the limit.