How to fix Error 1040: Too Many Connections
Error Message
When encountering MySQL Error 1040, you'll see a message similar to:
ERROR 1040 (HY000): Too many connections
What It Means
This error occurs when the MySQL server has reached its maximum allowed number of concurrent client connections. The server is unable to accept any new connections until existing connections are closed or terminated.
Each connection to MySQL consumes server resources (memory, file descriptors, etc.), and this limit exists to prevent the server from becoming overloaded and potentially crashing.
Common Causes
- Low max_connections setting: Default MySQL max_connections value is often too low (typically 151)
- Connection leaks: Applications not properly closing database connections
- Connection pooling issues: Inefficient connection pool configuration
- Long-running queries: Queries that take excessive time, holding connections open
- Inactive but open connections: Sleeping connections consuming connection slots
- High traffic spikes: Sudden increases in application traffic
- Missing transaction commits/rollbacks: Unclosed transactions keeping connections active
- Insufficient server resources: Not enough system resources to handle more connections
How to Fix
Solution 1: Increase max_connections Limit
Check and increase the maximum connections limit:
-- Check current max_connections value
SHOW VARIABLES LIKE 'max_connections';
-- Increase max_connections temporarily
SET GLOBAL max_connections = 500;
For permanent changes, modify the MySQL configuration file (my.cnf/my.ini):
[mysqld]
max_connections = 500
Then restart MySQL to apply the changes.
Solution 2: Implement or Optimize Connection Pooling
Use connection pooling in your applications:
- Java: HikariCP, Apache DBCP, C3P0
- PHP: PDO persistent connections, PHP-FPM persistent connections
- Node.js: mysql2 with connection pooling
- Python: SQLAlchemy connection pools
- Ruby: ActiveRecord connection pools
Example configuration for a typical web application:
# Optimal pool size formula:
# connections = ((core_count * 2) + effective_spindle_count)
# Example pool settings:
pool_size = 10 # Base connections
pool_max_size = 20 # Maximum connections
pool_idle_timeout = 300 # Seconds before idle connection is closed
Solution 3: Terminate Idle Connections
Identify and close idle connections:
-- Show current connections and their state
SHOW PROCESSLIST;
-- Kill long-running or sleeping connections
KILL CONNECTION connection_id;
-- Kill multiple connections by state (requires MySQL 8.0+)
SELECT CONCAT('KILL ', id, ';')
FROM information_schema.processlist
WHERE command = 'Sleep' AND time > 3600
INTO OUTFILE '/tmp/kill_connections.sql';
-- Then run generated SQL
SOURCE /tmp/kill_connections.sql;
Solution 4: Configure wait_timeout and interactive_timeout
Reduce the time for which idle connections stay open:
-- Check current timeout values
SHOW VARIABLES LIKE '%timeout%';
-- Reduce timeout values
SET GLOBAL wait_timeout = 300; -- Close inactive connections after 5 minutes
SET GLOBAL interactive_timeout = 300; -- Close inactive client connections after 5 minutes
In my.cnf for persistence:
[mysqld]
wait_timeout = 300
interactive_timeout = 300
Solution 5: Optimize Application Connection Management
Ensure your application properly manages connections:
- Always close connections after use
- Use try-finally blocks to ensure connections are released
- Implement connection reuse
- Add connection timeout handling
- Use prepared statements to reduce overhead
Example pseudo-code:
connection = null
try {
connection = getConnectionFromPool()
// Use connection for query
} catch (Exception e) {
// Handle exception
} finally {
if (connection != null) {
connection.close() // Return to pool, don't actually close
}
}
Solution 6: Optimize Queries to Reduce Connection Time
Identify and optimize slow queries that hold connections open:
-- Enable slow query log
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 1; -- Log queries taking longer than 1 second
-- Review slow query log
-- Add indexes and optimize problem queries
Solution 7: Scale Your Database Infrastructure
If legitimate traffic demands more connections:
- Implement read replicas for read queries
- Shard your database
- Consider database clustering
- Use a caching layer to reduce database load
Cloud Vendor Considerations
When using cloud-based MySQL services:
-
AWS RDS/Aurora:
- Max connections is calculated based on instance size
- Set via Parameter Groups
- Monitor using CloudWatch metrics
- Consider Aurora Serverless for auto-scaling
-
Google Cloud SQL:
- Default limits vary by machine type
- Configure using database flags
- Monitor using Cloud Monitoring
- Consider user limits per connection
-
Azure Database for MySQL:
- Connection limits tied to pricing tier
- Configure server parameters
- Scale compute resources as needed
- Use connection pooling with Azure App Service
Common cloud vendor strategies:
- Enable performance insights/monitoring to identify connection issues
- Use managed proxies where available (e.g., Amazon RDS Proxy, ProxySQL)
- Scale compute resources when connection limits are consistently reached
- Implement retry logic with exponential backoff in applications