# Cloud SQL: FATAL: remaining connection slots are reserved / MySQL: ERROR 1040 (08004): Too many connections

**Code:** `Cloud SQL: too many connections` · **Category:** Google Cloud / Firebase · **Seen on:** Google Cloud, SQL

The instance's maximum connections was reached, often because serverless instances each open their own connection pool.

## Meaning

Cloud Run/Functions scale out and each instance opens connections. Set small pools (1-5 per instance), use connection pooling (PgBouncer / managed connection pooling), lower max-instances, and close idle connections. Raise `max_connections` flag only after sizing memory.

## Common causes

- Many serverless instances each with a pool
- Pool max size too large
- Connections never closed/leaked
- Low max_connections on a small tier
- Long-running transactions holding slots

## Quick fix

1. Limit pool size per instance (e.g. 2-5) and instances
2. Use managed connection pooling or PgBouncer
3. Increase `max_connections` and instance size if justified

## Detailed fix by platform

### gcloud

1. `gcloud sql instances patch INSTANCE --database-flags=max_connections=300`

## How to diagnose

1. **Count** — Connections vs max?
2. **Source** — Instances x pool?
3. **Leaks** — Idle connections?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/gcp/cloudsql-too-many-connections)
