---
title: "MySQL error 1040 Too many connections from Cloud Run | Proofline"
description: "Group MySQL's process list by user and command, then read how long the sleeping connections have slept."
url: https://proofline.sh/learn/cloud-sql-mysql-too-many-connections-cloud-run/
---

[Learning center](/learn/)

# MySQL error 1040 Too many connections from Cloud Run

Group MySQL's process list by user and command, then read how long the sleeping connections have slept. Sleepers measured in minutes are pools of Cloud Run instances that are still alive; sleepers measured in hours are connections nobody uses, unless their count matches minimum instances times pool size, which MySQL keeps until wait\_timeout, eight hours by default.

Capacity overruns The Proofline team November 23, 2026 7 min read

## 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](https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html) · [Too many connections](https://dev.mysql.com/doc/refman/8.0/en/too-many-connections.html)

This is the admission stage of [the four stages](/learn/cloud-run-cloud-sql-connection-errors/#stage), 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](https://cloud.google.com/sql/docs/mysql/quotas) · [Cloud SQL for MySQL flags](https://cloud.google.com/sql/docs/mysql/flags) · [`max_connections`](https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html)

`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`](https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html)

## 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](https://dev.mysql.com/doc/refman/8.0/en/show-processlist.html) · [INFORMATION\_SCHEMA PROCESSLIST](https://dev.mysql.com/doc/refman/8.0/en/information-schema-processlist-table.html) · [Cloud SQL `performance_schema` flag](https://cloud.google.com/sql/docs/mysql/flags)

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](https://cloud.google.com/sql/docs/mysql/connect-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](/learn/cloud-run-idle-cpu-stale-cloud-sql-connections/)) |
| 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 |

[Idle instances](https://cloud.google.com/run/docs/about-instance-autoscaling)

## 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](https://cloud.google.com/sql/docs/mysql/connect-run) · [Cloud SQL metrics](https://cloud.google.com/monitoring/api/metrics_gcp_c) · [Connection budget](/learn/cloud-run-cloud-sql-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](https://docs.sqlalchemy.org/en/20/core/pooling.html) · [Stale connections after idle](/learn/cloud-run-idle-cpu-stale-cloud-sql-connections/)

## 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`](https://cloud.google.com/sdk/gcloud/reference/sql/instances/patch) · [Configure database flags](https://cloud.google.com/sql/docs/mysql/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](https://dev.mysql.com/doc/refman/8.0/en/performance-schema-connection-attribute-tables.html) · [Container runtime contract](https://cloud.google.com/run/docs/container-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.

## One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. [Eligibility and fair-use terms](/free-code-review/).

[Start free with GitHub](https://proofline.sh/orgs/plan?offer=free_month)

## Related reading

-   ### [How many Cloud SQL connections can a Cloud Run service open?](/learn/cloud-run-cloud-sql-connection-budget/)
    
    Capacity overruns The Proofline team November 23, 2026 7 min read
    
-   ### [Can a pull request review detect database connection limits we might exceed?](/learn/pr-database-connection-limits/)
    
    Capacity overruns The Proofline team November 19, 2026 1 min read
