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_connections set too low for the total tenant demand

Step-by-Step Fix

  1. 1.Verify the max connections error: Confirm the root cause.
  2. 2.```bash
  3. 3.# If you have MySQL access (via SSH or phpMyAdmin)
  4. 4.mysql -u username -p -e "SHOW STATUS LIKE 'Max_used_connections';"
  5. 5.mysql -u username -p -e "SHOW VARIABLES LIKE 'max_connections';"
  6. 6.`
  7. 7.Optimize the application to use fewer connections: Reduce connection usage.
  8. 8.```php
  9. 9.// Use persistent connections carefully
  10. 10.$pdo = new PDO($dsn, $user, $pass, [
  11. 11.PDO::ATTR_PERSISTENT => false, // Disable persistent connections
  12. 12.PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  13. 13.]);

// Ensure connections are closed after use $pdo = null; ```

  1. 1.Implement connection pooling: Reuse connections efficiently.
  2. 2.```php
  3. 3.// Use a connection pool library
  4. 4.// Or configure WordPress to limit connections
  5. 5.// wp-config.php
  6. 6.define('WP_MAX_CONNECTIONS', 5);
  7. 7.`
  8. 8.Contact the hosting provider: Request a connection limit increase or migration.
  9. 9.`
  10. 10.# Open a support ticket:
  11. 11.# - Report the max connections error
  12. 12.# - Request the current connection usage breakdown
  13. 13.# - Ask for a dedicated database server if the shared server is overloaded
  14. 14.`
  15. 15.Monitor connection usage and implement fallback: Add connection retry logic.
  16. 16.```php
  17. 17.// Retry connection with exponential backoff
  18. 18.$maxRetries = 3;
  19. 19.for ($i = 0; $i < $maxRetries; $i++) {
  20. 20.try {
  21. 21.$pdo = new PDO($dsn, $user, $pass);
  22. 22.break;
  23. 23.} catch (PDOException $e) {
  24. 24.if (strpos($e->getMessage(), 'Too many connections') !== false && $i < $maxRetries - 1) {
  25. 25.sleep(pow(2, $i));
  26. 26.continue;
  27. 27.}
  28. 28.throw $e;
  29. 29.}
  30. 30.}
  31. 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