Introduction
Shared hosting MySQL servers have a maximum connection limit that is shared across all tenants. When the total number of connections reaches this limit, new connection attempts are rejected with Too many connections errors. This causes websites to display database connection errors, and the affected tenant has limited control since the limit is server-wide.
Symptoms
- Website displays
Error establishing a database connection - Application logs show
SQLSTATE[HY000] [1040] Too many connections - Database queries work intermittently -- succeeding when other users disconnect
- phpMyAdmin cannot connect to the database
- Error message:
mysqli::real_connect(): (HY000/1040): Too many connections
Common Causes
- Traffic spike causing many simultaneous database connections
- Application not closing database connections properly (connection leak)
- Another tenant on the shared server consuming all available connections
- Persistent connections configured in the application holding connections open
- MySQL
max_connectionsset too low for the total tenant demand
Step-by-Step Fix
- 1.Verify the max connections error: Confirm the root cause.
- 2.```bash
- 3.# If you have MySQL access (via SSH or phpMyAdmin)
- 4.mysql -u username -p -e "SHOW STATUS LIKE 'Max_used_connections';"
- 5.mysql -u username -p -e "SHOW VARIABLES LIKE 'max_connections';"
- 6.
` - 7.Optimize the application to use fewer connections: Reduce connection usage.
- 8.```php
- 9.// Use persistent connections carefully
- 10.$pdo = new PDO($dsn, $user, $pass, [
- 11.PDO::ATTR_PERSISTENT => false, // Disable persistent connections
- 12.PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- 13.]);
// Ensure connections are closed after use $pdo = null; ```
- 1.Implement connection pooling: Reuse connections efficiently.
- 2.```php
- 3.// Use a connection pool library
- 4.// Or configure WordPress to limit connections
- 5.// wp-config.php
- 6.define('WP_MAX_CONNECTIONS', 5);
- 7.
` - 8.Contact the hosting provider: Request a connection limit increase or migration.
- 9.
` - 10.# Open a support ticket:
- 11.# - Report the max connections error
- 12.# - Request the current connection usage breakdown
- 13.# - Ask for a dedicated database server if the shared server is overloaded
- 14.
` - 15.Monitor connection usage and implement fallback: Add connection retry logic.
- 16.```php
- 17.// Retry connection with exponential backoff
- 18.$maxRetries = 3;
- 19.for ($i = 0; $i < $maxRetries; $i++) {
- 20.try {
- 21.$pdo = new PDO($dsn, $user, $pass);
- 22.break;
- 23.} catch (PDOException $e) {
- 24.if (strpos($e->getMessage(), 'Too many connections') !== false && $i < $maxRetries - 1) {
- 25.sleep(pow(2, $i));
- 26.continue;
- 27.}
- 28.throw $e;
- 29.}
- 30.}
- 31.
`
Prevention
- Use connection pooling to minimize the number of simultaneous database connections
- Set appropriate connection timeout and idle timeout values in the application
- Monitor connection usage and alert when approaching the max connections limit
- Implement connection retry logic with exponential backoff in the application
- Consider upgrading to a VPS or dedicated hosting with dedicated database resources
- Optimize slow queries to reduce connection hold time