On this page
What this issue is
Every time a page needs data, your site opens a connection to the database (MySQL or MariaDB on most sites). The server allows only a fixed number at once, set by max_connections. When a spike arrives, or queries run slowly and hold connections open, that limit is reached. The next request is refused with ERROR 1040: Too many connections.
Why it costs you leads or sales
Spikes usually come from the moments you pay for: an ad launch, an email send, a sale or a mention. That is when the database is most likely to refuse visitors. Form submissions that cannot be saved are lost, checkouts fail mid-order, and ad clicks land on an error page you still pay for.
- Ad or email spikeMany visitors arrive at once
- Database connections fullmax_connections reached
- Page or form errorsError 1040, 500 or blank page
- Lead or orderNever saved
Once connection slots run out, every step that needs the database fails.
Example: illustrative numbers only. Traffic doubles, but submissions drop once the connection limit is hit.
How to check if your site has it
Read the error logs
Search the PHP and database error logs for "Too many connections" or "1040". Match the timestamps to your campaign or traffic spike.
Check the current limit and peak use
If you have database access, run the queries below. If Max_used_connections is equal or close to max_connections, you have hit the limit.
See what is holding connections
Run SHOW FULL PROCESSLIST during a busy period. Many rows in the same slow query, or in the Sleep state, point to the cause.
Check your host's panel
Shared hosts often cap connections per account and show usage or resource errors in the control panel (for example, a resource usage page). Ask support for the limit if it is not shown.
SHOW VARIABLES LIKE 'max_connections';
SHOW STATUS LIKE 'Max_used_connections';
SHOW STATUS LIKE 'Threads_connected';
SHOW FULL PROCESSLIST;How to fix it
Raising the limit alone often just moves the crash: more connections use more memory, and slow queries still pile up. Reduce the demand first, then raise the limit if the server has memory to spare.
Cache pages for anonymous visitors
Landing pages that serve the same content to everyone should come from a page cache or CDN, so they do not touch the database at all.
Find and fix slow queries
Turn on the slow query log (slow_query_log = 1, long_query_time = 1), then add missing indexes or remove the heavy plugin or query behind them. Slow queries hold connections longest.
Block abusive traffic
Check access logs for bots hammering search, login or admin-ajax URLs. Rate-limit or block them at your CDN or firewall, without blocking real visitors.
Lower wait_timeout
Idle connections in the Sleep state waste slots. A lower wait_timeout (for example 60 seconds) frees them sooner. Test first, as long jobs may need more.
Raise max_connections if memory allows
On a server you control, increase it moderately and watch memory. On shared hosting you usually cannot, so ask the host or upgrade.
Scale the hosting
If the site is healthy but simply outgrown its plan, move to more RAM and CPU or a managed database.
Add page caching
Use a caching plugin or your host's built-in cache so anonymous pages skip PHP and the database.
Audit plugins
Deactivate plugins one by one on a staging copy. Related-posts, statistics and search plugins are common heavy query sources.
Limit the Heartbeat API and admin-ajax
Frequent admin-ajax.php calls hold connections. Reduce Heartbeat frequency with a plugin or your host's tools.
Nothing to tune
Shopify manages the database for you. If checkout fails under load, check your apps and custom scripts and contact Shopify support.
Nothing to tune
Wix manages the infrastructure. If a Velo app queries a database collection heavily, reduce and cache those queries.
Use connection pooling or persistent connections
Reuse connections instead of opening one per request, for example with a pooler such as ProxySQL or your framework's pool settings.
Close connections and avoid leaks
Make sure every code path releases its connection, including error paths.
Raise the limit in config
Set max_connections in the [mysqld] section of my.cnf and restart or set it at runtime with SET GLOBAL max_connections = 300; (an example value).
[mysqld]
max_connections = 300
wait_timeout = 60
slow_query_log = 1
long_query_time = 1How to confirm it's fixed
- Max_used_connections stays well below max_connections after a busy period
- No new "Too many connections" lines in the logs
- Landing pages are served from cache (check response headers or your cache plugin's stats)
- A test form submission and a test checkout succeed while traffic is high
- A load test on a staging copy, with permission from your host, stays error-free
How to prevent it happening again
- Tell your developer or host before big campaigns and email sends
- Keep full-page caching or a CDN in front of landing pages
- Review slow query logs monthly
- Set an alert on database connection usage with your host or monitoring tool (for example at 80% of the limit)
- Retest after adding plugins, themes or tracking scripts that call the database
Example: 10:00
Email goes out, traffic climbs
Example: 10:03
Slow queries start stacking up
Example: 10:05
Limit reached, visitors get errors
Example: 10:40
Traffic falls, the site recovers on its own
How FunnelGuard handles this
FunnelGuard detects this only partly. A robot visitor runs your real journeys on a schedule, from the EU or the US. If a check lands while the database is refusing connections, the page, form or checkout step fails, and FunnelGuard re-checks from a second region and sends one alert with a screenshot and a likely cause.
Before
- Every page view hits the database
- Slow queries hold connections
- Spike ends in error 1040
After
- Landing pages served from cache
- Slow queries indexed or removed
- Headroom under the connection limit
Questions people ask
Why does my site say "too many connections" during traffic spikes?
Your database allows only a set number of simultaneous connections. A spike, slow queries or bots can use them all, and new requests are refused with error 1040. Caching and query fixes reduce the load.
How do I fix MySQL error 1040 too many connections?
Find what holds connections with SHOW FULL PROCESSLIST, add page caching, fix slow queries and block abusive bots. Then raise max_connections if the server has spare memory, or upgrade your hosting.
How do I increase max_connections in MySQL?
Run SET GLOBAL max_connections = 300; for a temporary change, or set it under [mysqld] in my.cnf and restart for a permanent one. The number 300 is an example. Check your RAM first, as each connection uses memory.
Is it safe to just raise max_connections?
Not on its own. More connections use more memory, and slow queries can still pile up, so the server may swap or crash. Reduce demand with caching and query fixes first.
Why does my WordPress site show a database connection error only when busy?
It is often the connection limit rather than wrong credentials. When traffic is low the site works, but a spike fills the slots. Check the logs for "Too many connections" at the same times.
Can I change max_connections on shared hosting?
Usually not. Shared hosts set a per-account limit. Ask support to raise it, reduce your usage with caching, or move to a VPS or managed plan.
Can FunnelGuard tell me when my database hits its connection limit?
Only partly. It will alert you if a scheduled check hits an error page or a failed form or checkout step. It does not read database metrics, so you still need host-level alerts.
Related issues
- OPS-075Database connection errors ('Error establishing a database connection')Fix guide
- OPS-073Memory / PHP memory_limit exhausted (white screen)Fix guide
- OPS-074CPU throttling / entry process limits on shared hostingFix guide
- OPS-091Auto-scaling failure during traffic peakFix guide
- OPS-094Rate limiting on host blocks normal visitorsFix guide
- OPS-090One bad node behind load balancer (random failures)Fix guide