Connection Tuning
The database connection layer sits between PHP workers (PHP-FPM/LSAPI) and the MySQL/MariaDB engine. If PHP concurrency is higher than what the database is configured to accept, requests fail with connection errors. If the database allows too many connections for the available RAM, the server becomes unstable.
- Align
max_connectionswith your PHP worker pool (pm.max_children/ LSAPI children). - Reduce idle connection lifetime with
wait_timeoutfor typical WordPress workloads. - Use
thread_cache_sizeto reduce thread creation overhead under bursts. - Monitor
Threads_connected,Threads_running, andMax_used_connectionsto validate the sizing.
Core Variables & Parameter Tuning
Connection limits are a RAM trade-off. Each connection and thread can consume memory, so raising limits without headroom can make things worse.
| Variable Directive | Safe Default | Performance Target | Operational Purpose |
|---|---|---|---|
max_connections | 151 | 150-300 | Defines the absolute total simultaneous database connections allowed dynamically across the host. |
max_user_connections | 0 (Unlimited) | 60-100 | Defines the per-database-user limit physically preventing a single hijacked site from saturating the global matrix. |
wait_timeout | 28800 (8 Hours) | 60-120 | Defines the exact seconds the database retains idle "sleep" connections before terminating them cleanly to save RAM. |
thread_cache_size | 9 | 32-128 | Retains idle execution threads actively in RAM, bypassing the CPU overhead required to spawn fresh threads for new visitors. |
open_files_limit | 1024 | 65535+ | Necessary for concurrent servers or WordPress Multisite holding thousands of custom tables open. |
Targets vary by workload. A cache-heavy blog may be stable with fewer connections than a checkout-heavy store.
The Implementation Procedure
Configure the core settings inside the root .cnf file systematically.
# Ubiquity defaults often route strictly through the MariaDB or MySQL specific directories
sudo nano /etc/mysql/my.cnf
1. Audit Current Usage & Align Limits
Before increasing connection counts blindly, audit the specific existing PHP worker maximums (e.g., pm.max_children or lsphp_children). The database requires exactly mathematical headroom exceeding the maximum potential PHP processes.
[mysqld]
# Multiply the total PHP worker capacity natively by 1.25 safely to calculate proper headroom
max_connections = 250
max_user_connections = 120
# Drop idle connections to preserve RAM
wait_timeout = 90
interactive_timeout = 180
# Keep threads actively warm for WooCommerce surges safely
thread_cache_size = 64
table_open_cache = 4096
open_files_limit = 65535
2. Monitor Active Connections Safely
Restart the database safely to instantiate the logic, then forcefully inspect the active runtime metrics directly immediately during peak traffic events accurately.
sudo systemctl restart mysql
mysql -e "SHOW STATUS LIKE 'Threads_connected';"
mysql -e "SHOW STATUS LIKE 'Threads_running';"
mysql -e "SHOW STATUS LIKE 'Max_used_connections';"
If Threads_connected routinely approaches max_connections during business hours, you are one burst away from connection errors.
Sizing Matrix Application
- Static Publishers
- E-commerce (WooCommerce)
The Lean Stack Requirements
If most traffic is served from cache (Cloudflare/LSCache), database connection pressure is usually low.
- Strategy: keep
max_connectionsmoderate and use a shorterwait_timeout. - Outcome: less idle memory usage.
The High Concurrency Deployment
Checkout and logged-in flows generate more uncached traffic.
- Strategy: size
max_connectionsbased on PHP worker concurrency and RAM headroom. - Outcome: fewer connection errors during bursts.
Common Mistakes & Troubleshooting
| Configuration Failure | Operational Symptom | Remediation Protocol |
|---|---|---|
| Pool saturation | Error establishing a database connection appears under load. | Align PHP worker counts and max_connections, and reduce slow queries. |
| Idle connection buildup | RAM usage slowly rises due to many sleeping connections. | Reduce wait_timeout and confirm application behavior isn't leaking connections. |
| Single Tenant Lockout | In a shared VPS hosting 10 completely isolated web properties securely, one specific site completely crashes while the other exactly 9 operate flawlessly dynamically. | The specific crashed tenant exceeded the strictly enforced max_user_connections limitation locally firmly. It requires debugging completely uniquely identifying the offending script. |