Introduction When RDS reaches its max_connections limit, new connections are rejected with "Too many connections" (MySQL) or "FATAL: sorry, too many clients already" (PostgreSQL). This cascades into application errors, failed deployments, and potential data loss.
Symptoms - MySQL error: `ERROR 1040 (HY000): Too many connections` - PostgreSQL error: `FATAL: sorry, too many clients already (max_connections=100)` - Application connection pool exhaustion with timeout errors - RDS CloudWatch metric DatabaseConnections at or near max_connections - Slow queries that hold connections open for extended periods
Common Causes - max_connections parameter too low for workload - Connection leaks in application code (connections not returned to pool) - Sudden traffic spike exceeding planned capacity - Long-running transactions holding connections - Zombie connections from crashed application instances - Missing connection pooling
Step-by-Step Fix 1. **Check current connection count and limit**: ```bash # MySQL mysql -h my-db.xxxx.us-east-1.rds.amazonaws.com -u admin -p \ -e "SHOW STATUS LIKE 'Threads_connected'; SHOW VARIABLES LIKE 'max_connections';"
# PostgreSQL psql -h my-db.xxxx.us-east-1.rds.amazonaws.com -U postgres \ -c "SELECT count(*) FROM pg_stat_activity;" ```
- 1.Identify idle connections:
- 2.```sql
- 3.-- MySQL
- 4.SELECT id, user, host, db, command, time, state
- 5.FROM information_schema.processlist
- 6.WHERE command = 'Sleep' AND time > 300 ORDER BY time DESC;
-- PostgreSQL SELECT pid, usename, state, query_start, query FROM pg_stat_activity WHERE state = 'idle' AND state_change < now() - interval '5 minutes'; ```
- 1.Kill idle connections:
- 2.```sql
- 3.-- MySQL: KILL <process_id>;
- 4.-- PostgreSQL:
- 5.SELECT pg_terminate_backend(pid)
- 6.FROM pg_stat_activity
- 7.WHERE state = 'idle' AND state_change < now() - interval '5 minutes';
- 8.
` - 9.Increase max_connections via parameter group:
- 10.```bash
- 11.aws rds modify-db-parameter-group \
- 12.--db-parameter-group-name my-param-group \
- 13.--parameters "ParameterName=max_connections,ParameterValue=500,ApplyMethod=pending-reboot"
- 14.aws rds reboot-db-instance --db-instance-identifier my-db-instance
- 15.
` - 16.Deploy RDS Proxy for connection pooling:
- 17.```bash
- 18.aws rds create-db-proxy \
- 19.--db-proxy-name my-proxy --engine-family MYSQL \
- 20.--auth '{"IAMAuth": "DISABLED", "SecretArn": "arn:aws:secretsmanager:..."}' \
- 21.--role-arn arn:aws:iam::<account>:role/RDSProxyRole \
- 22.--vpc-subnet-ids subnet-xxx subnet-yyy
- 23.
`