// MySQL error reference

MySQL error 1040: Too many connections

The server already has max_connections client connections open. Close idle ones, size connection pools properly, or raise the limit.

Error number
1040
Name
ER_CON_COUNT_ERROR
SQLSTATE
08004
Category
Server limits and settings
Error message
Too many connections

What it means

MySQL refuses a new connection once max_connections clients are connected. It keeps one extra slot for an account with the CONNECTION_ADMIN (or SUPER) privilege, so an administrator can still get in to see what's happening.

Common causes

  • Application connection pools that together open more connections than the server allows, for example many app instances each with a large pool.
  • Connections leaked by code that never closes them.
  • Slow queries or lock waits that keep connections busy until new requests pile up.
  • A low max_connections for the workload (the default is 151).

How to fix it

  1. See how many connections there are and who holds them.

    SHOW STATUS WHERE Variable_name IN ('Threads_connected', 'Max_used_connections');
    SELECT user, host, db, command, time FROM information_schema.processlist ORDER BY time DESC;
  2. Kill idle sessions that are stuck, then fix whatever leaks them.

    KILL 12345;  -- the Id from the process list
  3. Raise the limit if the server has memory to spare, and persist it.

    SET PERSIST max_connections = 500;
  4. Cap pool sizes so that pool size times the number of app instances stays below max_connections.

Related errors

More server limits and settings errors

The code and message text are from the MySQL 8.4 Error Message Reference. Browse the full MySQL error list or search every database error.

// fewer errors, faster fixes

Work on MySQL in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for MySQL and the other databases you use.