Skip to content
FunnelGuard

Fix guide by FunnelsGuard

Database too many connections: how to find and fix it

OPS-076High impactPartlyBy FunnelGuardPublished Updated
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.

What happens during a spike
  1. Ad or email spikeMany visitors arrive at once
  2. Database connections fullmax_connections reached
  3. Page or form errorsError 1040, 500 or blank page
  4. Lead or orderNever saved

Once connection slots run out, every step that needs the database fails.

Form submissions during a spikeExample numbers
Form submissions during a spike (example)Normal hour: 40 submissions per hour; Spike, limit not hit: 80 submissions per hour; Spike, limit hit: 12 submissions per hour03161408012Normal hourSpike, limit not hitSpike, limit hitsubmissions per hour

Example: illustrative numbers only. Traffic doubles, but submissions drop once the connection limit is hit.

How to check if your site has it

Confirm connection exhaustion
  1. 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.

  2. 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.

  3. 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.

  4. 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.

MySQL / MariaDB checkstext
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.

Fix too many connections
  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. Scale the hosting

    If the site is healthy but simply outgrown its plan, move to more RAM and CPU or a managed database.

Platform specifics
  1. Add page caching

    Use a caching plugin or your host's built-in cache so anonymous pages skip PHP and the database.

  2. Audit plugins

    Deactivate plugins one by one on a staging copy. Related-posts, statistics and search plugins are common heavy query sources.

  3. 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.

my.cnf example (values are examples)text
[mysqld]
max_connections = 300
wait_timeout = 60
slow_query_log = 1
long_query_time = 1

How to confirm it's fixed

Confirmation checklist
  • 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

Prevention
  • 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
How a spike turns into errors
  1. Example: 10:00

    Email goes out, traffic climbs

  2. Example: 10:03

    Slow queries start stacking up

  3. Example: 10:05

    Limit reached, visitors get errors

  4. 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 and after

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.

Does FunnelGuard catch this?

Partly

FunnelGuard checks part of this.

Stop finding out from your customers

FunnelGuard runs your real forms and checkouts on a schedule, checks the tracking and the leads behind them, and alerts you once, with a screenshot. Try any paid plan free for 3 days.