skip to content

In MySQL, what does a 'Too many connections' error mean, and which setting and status counters do you check first?

level: juniorimportance: should knowfreq 50%

answer

  1. a server-wide cap on sessions
  2. default 151, global and dynamic
  3. one slot held back for admins
  4. Max_used_connections versus the cap

basics

~10 s

MySQL refused a new session because max_connections (default 151) client connections were already open. Check Threads_connected, Max_used_connections and Connection_errors_max_connections, logging in through the extra slot kept for CONNECTION_ADMIN.

solid answer

~40 s

The error means every slot allowed by `max_connections` is taken. It is a global, dynamic variable with a default of 151, and the server refuses further clients and increments `Connection_errors_max_connections`. MySQL actually permits `max_connections + 1`: the extra connection is reserved for an account with `CONNECTION_ADMIN`, so a DBA can still get in and run `SHOW PROCESSLIST`. I compare `Threads_connected` and `Max_used_connections` with the limit, and look at `Threads_running` to see whether those sessions are working or just sleeping. Raising the limit with `SET PERSIST max_connections = ...` is quick, but if most sessions are idle the real fix is on the client side, such as an oversized pool or connections that never close.

code

sql · 9 lines
sql
SHOW VARIABLES LIKE 'max_connections';

SHOW GLOBAL STATUS WHERE Variable_name IN (
  'Threads_connected', 'Threads_running',
  'Max_used_connections', 'Max_used_connections_time',
  'Connection_errors_max_connections');

-- after confirming memory headroom
SET PERSIST max_connections = 400;

go deeper

for a junior

Recall that max_connections caps simultaneous clients, defaults to 151 and is global and dynamic, and that the error is a refusal, not a crash.

for a middle

Explain the max_connections + 1 slot for CONNECTION_ADMIN, the effective limit tied to open_files_limit, and how Threads_connected, Threads_running and Max_used_connections separate busy sessions from idle ones.

for a senior

Show that you look at idle sessions, wait_timeout and per-account max_user_connections before raising the cap, and that you check per-connection memory before accepting a much larger value.

for a principal

Treat the connection cap as a contract between the database and every client fleet: set per-service account limits and pool budgets so one deploy cannot take all the slots.

## What the error means A MySQL server accepts only a fixed number of **simultaneous client connections**. When a new client arrives and that number is already reached, the server refuses it with the **Too many connections** error and adds one to the `Connection_errors_max_connections` status counter. Nothing has crashed. The server is healthy enough to answer, and it is protecting itself from taking on more sessions than it was configured for. The cap is the `max_connections` system variable: - **Scope and type:** global only, and **dynamic**, so it can change at runtime without a restart. `SET SESSION max_connections = ...` fails with an error because no session value exists. - **Default:** `151` in MySQL 8.4. The maximum is 100000. - **Effective value:** the lesser of the configured value and `open_files_limit - 810`. If the server cannot get enough file descriptors, it lowers `max_connections` on its own, so the value you set is not always the value you get. - **Persisting a change:** `SET GLOBAL` lasts until restart. `SET PERSIST` also writes it to `mysqld-auto.cnf` so it survives one. ## Getting in when the server is full `mysqld` actually permits **`max_connections + 1`** client connections. The extra one is reserved for accounts holding the **`CONNECTION_ADMIN`** privilege (or the deprecated `SUPER`). That is why application accounts should never hold the privilege: if they did, they could take the emergency slot themselves. For more than one emergency session, MySQL can open a separate **administrative interface**. Set `admin_address` at startup (there is no default, so the interface does not exist until you configure it) and optionally `admin_port` (default `33062`). That interface has no connection limit, but only accounts with `SERVICE_CONNECTION_ADMIN` may use it. ## The counters to read | Status variable | What it tells you | |---|---| | `Threads_connected` | Connections open right now | | `Threads_running` | Threads that are not sleeping, meaning sessions actually doing work | | `Max_used_connections` | The most connections open at once since startup | | `Max_used_connections_time` | When that peak happened | | `Connection_errors_max_connections` | Connections refused because the limit was hit | | `Threads_created`, `Threads_cached` | Whether the thread cache keeps up with connection churn | A first diagnosis usually runs in this order: 1. Connect through the reserved slot or the admin port. 2. Compare `Threads_connected` and `Max_used_connections` with `max_connections`. A peak that sits on the limit confirms that the cap, not something else, refused the clients. 3. Look at `Threads_running`. A few running threads beside hundreds of connections means most sessions are idle (`Sleep` in `SHOW PROCESSLIST`). 4. Group `SHOW PROCESSLIST` (or `performance_schema.threads`) by user and host to find which client fleet holds the connections. ## Telling it apart from look-alike errors Three different limits refuse connections, and each needs a different fix: - **Too many connections:** the server-wide `max_connections` cap. Every new client is refused, whatever its account. - **A per-account limit:** an account created or altered `WITH MAX_USER_CONNECTIONS n`, or the global `max_user_connections` (default 0, meaning no limit), refuses only that account while other accounts still connect. - **Host 'host_name' is blocked because of many connection errors:** one client host has made `max_connect_errors` (default 100) interrupted connection attempts in a row without a success. Only that host is refused. Flushing the host cache (for example `TRUNCATE TABLE performance_schema.host_cache`) unblocks it, and the interrupted handshakes themselves, often a network or TLS problem, are the real cause. Reading the exact message first saves you from raising `max_connections` when one host or one account is the problem. ## Why raising the number is not the whole fix By default `thread_handling` is `one-thread-per-connection`, so every connection is a server thread with its own stack and its own per-session buffers (`sort_buffer_size`, `read_buffer_size`, `join_buffer_size` and others, allocated when a statement needs them). The manual's rough memory estimate multiplies those buffers by `max_connections`. A limit raised from 151 to several thousand can turn a refused-connection problem into a swapping server. Idle sessions add to it. A non-interactive connection that nobody closes is held until `wait_timeout` expires, and the default is 28800 seconds (eight hours). When `Threads_running` is low and `Threads_connected` is at the cap, the usual causes sit in the clients: each application instance keeps a large pool, a deploy leaves old instances connected, or code leaks connections. Pool sizing in general is covered with connection pooling. The MySQL-side levers are a sensible `max_connections`, a shorter `wait_timeout` for application accounts, and a per-account `MAX_USER_CONNECTIONS` limit so one service cannot take every slot. MySQL Enterprise Edition also ships a thread pool plugin (it sets `thread_handling` to `loaded-dynamically`), but Community servers run one thread per connection.

  • Why can a DBA still log in to MySQL when ordinary clients are refused?
    MySQL permits `max_connections + 1` connections and keeps the extra one for an account with `CONNECTION_ADMIN`. It is a single slot, so keep the privilege away from application accounts. For more emergency sessions, configure `admin_address` (and `admin_port`, default 33062): that interface has no connection limit but only admits accounts with `SERVICE_CONNECTION_ADMIN`.
  • You set max_connections to 5000 but MySQL reports a smaller value. Why?
    The effective value is the lesser of what you set and `open_files_limit - 810`, and the server lowers `max_connections` itself when it cannot get the file descriptors it needs. Raise `open_files_limit`, and the operating system's descriptor limit behind it, before asking for that many connections.

  • MySQL has no default connection limit, so the error means the server crashed.
  • Running SET SESSION max_connections in the application fixes it for that app.
  • Give the application account CONNECTION_ADMIN so it is never refused.
  • Raising max_connections is free because idle connections use no memory.
  • Threads_connected near the limit proves the server is overloaded with work.