How it shows up#
New connections from the service fail with MySQL error 1040, symbol ER_CON_COUNT_ERROR, SQLSTATE 08004, message Too many connections, while connections that are already open keep working. MySQL reports it when all permitted connections are in use; the limit is the max_connections system variable. MySQL actually permits max_connections + 1 client connections and keeps the extra one for accounts with the CONNECTION_ADMIN privilege (or the deprecated SUPER), so that an administrator can still connect and look. Error 1040 · Too many connections
This is the admission stage of the four stages, in MySQL's words.
Check 1: read the limit and the idle timeout#
SHOW VARIABLES LIKE 'max_connections';
SHOW VARIABLES LIKE 'wait_timeout';
On Cloud SQL for MySQL, the instance's machine type, through its memory, determines the default connection limit, and Cloud SQL gives this same SHOW VARIABLES statement to find it. The max_connections flag overrides it and takes effect without a restart. MySQL's own default, outside Cloud SQL, is 151. Maximum concurrent connections · Cloud SQL for MySQL flags · max_connections
wait_timeout is the number of seconds the server waits for activity on a noninteractive connection before closing it, 28,800 by default. Cloud SQL accepts it as a flag without a restart. A connection opened by a pool that no longer uses it stays in the table, sleeping, for that long unless the client closes it. wait_timeout
Check 2: group the process list#
Run this as an account with the PROCESS privilege; without it, MySQL shows each user only their own threads:
SELECT user,
command,
COUNT(*) AS threads,
MAX(time) AS longest_s
FROM information_schema.processlist
GROUP BY user, command
ORDER BY threads DESC;
command is Sleep when the session is idle, and time is the number of seconds the thread has been in its current state, so for a sleeper it is how long it has been idle. host gives the client as host_name:client_port for TCP connections. The INFORMATION_SCHEMA implementation is deprecated in favor of the Performance Schema's processlist table, but Cloud SQL turns performance_schema off by default on MySQL 8.0 and later instances with less than 12 GB of RAM, and changing that flag restarts the instance, so on those instances the deprecated table is the one you can query. SHOW PROCESSLIST · INFORMATION_SCHEMA PROCESSLIST · Cloud SQL performance_schema flag
For a user whose sleepers look wrong, group that user's threads by client:
SELECT SUBSTRING_INDEX(host, ':', 1) AS client, COUNT(*) AS threads, MAX(time) AS longest_s
FROM information_schema.processlist
WHERE user = 'USER' AND command = 'Sleep'
GROUP BY client
ORDER BY threads DESC;
On Cloud Run's built-in connection, the service reaches MySQL through the Cloud SQL Auth Proxy, and Cloud SQL's documentation does not say what host shows for those connections, so a host value does not identify a Cloud Run instance there. A client outside Cloud Run connecting by IP address shows its own address. Connect from Cloud Run
| Sleepers from the service's user | Reading | Lever |
|---|---|---|
| Many, longest a few minutes to 15 minutes | Pools of live instances, including idle instances Cloud Run keeps for up to 15 minutes | The connection budget: pool size and instance count |
| Many, longest hours, count about minimum instances × pool size | Pools of minimum instances idle through a quiet period | Not abandoned: give the pool a maximum lifetime or validation before touching wait_timeout (stale connections after idle) |
| Many, longest hours, other counts | Connections no live pool uses: a client that opens and never closes, or a pool that keeps connections far longer than it needs them | A maximum lifetime in the pool, the client's own fix, and a lower wait_timeout |
| Few; another user holds most sleepers | Another client filled the table | That client's owner, or its own limit |
The budget on the Cloud Run side#
The arithmetic is the same as for PostgreSQL: each revision's maximum instances times the connections each instance's pools can open, plus other clients, against max_connections. Cloud Run's built-in connection limits an instance to 100 connections to a Cloud SQL database. Cloud SQL's metric database/network/connections counts connections to a MySQL instance, sampled every 60 seconds, for the minutes before the error. Cloud Run connection limit · Cloud SQL metrics · Connection budget
Lowering wait_timeout has one condition. A pool that keeps a connection idle for longer than the new value will hand out a connection the server already closed. Give the pool a maximum lifetime below wait_timeout first. SQLAlchemy's documentation describes pool_recycle for exactly this case, MySQL backends that close stale connections. SQLAlchemy pool recycle · Stale connections after idle
A worked fixture: one error, two owners#
The fixture is illustrative. catalog-api on Cloud Run connects to the MySQL 8.0 instance catalog-db (shop-prod:europe-west1:catalog-db) through the built-in connection. Its pool keeps 20 connections open per instance. max_connections is set to 600 by flag; wait_timeout is at its default of 28,800.
Morning A, 2026-11-23. At 08:15 UTC, catalog-api-00031 logs error 1040. container/instance_count reads 28 for the revision: 18 active and 10 idle after a burst that ended 14 minutes earlier.
| user | command | threads | longest_s |
|---|---|---|---|
catalog_app | Sleep | 512 | 840 |
catalog_app | Query | 48 | 3 |
etl | Sleep | 30 | 410 |
analyst | Sleep | 10 | 1,250 |
catalog_app holds 560 threads, exactly 28 instances × 20, and its longest sleeper has been idle for 14 minutes. The pools of live instances filled the table; wait_timeout plays no part. The team lowers the pool to 12 per instance and sets a service-level maximum of 40 instances: 40 × 12 + 40 other = 520, inside 600.
Night B, 2026-11-24. At 03:00 UTC, with little traffic, error 1040 returns.
| user | command | threads | longest_s |
|---|---|---|---|
etl | Sleep | 537 | 27,950 |
catalog_app | Sleep | 40 | 300 |
catalog_app | Query | 8 | 1 |
analyst | Sleep | 15 | 9,400 |
Grouped by client, all 537 etl sleepers come from 10.20.0.14, a batch VM outside Cloud Run. Its loader opens a connection per input file and never closes them, and the process keeps running between batches, so the connections stay open until MySQL ends them at wait_timeout. The oldest has slept 7 hours 45 minutes. catalog-api holds 48 threads on 4 instances, 12 each, which fits. The loader is fixed by its owner. Meanwhile the team sets catalog_app's pool lifetime to 25 minutes (pool_recycle=1500) and then wait_timeout to 1,800 seconds. That is a flag change on catalog-db, applied without a restart, and --database-flags replaces the whole flag list, so the command repeats max_connections=600:
gcloud sql instances patch catalog-db --project shop-prod \
--database-flags=max_connections=600,wait_timeout=1800
gcloud sql instances patch · Configure database flags
Where the checks mislead#
The process list cannot tell revisions apart. Without the Performance Schema there are no connection attributes to read, and through the Auth Proxy the host does not name the instance. If you need per-revision counts, run each revision as its own database user, or enable the Performance Schema, which restarts the instance, and have the client send an attribute with the revision's name from K_REVISION. MySQL connectors define their own ways to set connection attributes, and names that begin with an underscore are reserved for MySQL's own attributes. Connection attributes · Container runtime contract
time counts seconds in the current state. A pool that runs a validation query now and then resets its connections' sleep time, so a pool that keeps 20 connections it rarely needs can show only short sleeps. Compare the sleeper count with instances times pool size before deciding nothing is idle.
A global wait_timeout applies to every noninteractive client, including tools that legitimately hold a connection idle. Check them before lowering it.
Questions#
What does MySQL error 1040 Too many connections mean on Cloud SQL?
All permitted connections are in use. MySQL allows max_connections client connections, plus one reserved for accounts with CONNECTION_ADMIN. On Cloud SQL the default limit depends on the machine type's memory; SHOW VARIABLES LIKE 'max_connections' shows the value in force.
Why are there so many sleeping connections in MySQL from Cloud Run?
Each Cloud Run instance's pool keeps its connections open between requests, and idle instances can stay up for up to 15 minutes. Connections that sleep for hours come from a client that does not close them; MySQL keeps them until wait_timeout, 28,800 seconds by default.
Should I lower wait_timeout on Cloud SQL for MySQL?
It ends abandoned connections sooner, and Cloud SQL applies it without a restart. First make sure every pool retires connections before the new value, for example with a maximum lifetime, or the pools will hand out connections the server has already closed.