MYSQLi error: User already has more than 'max_user_connections' active connections

MYSQLi error: User already has more than 'max_user_connections' active connections

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

MYSQLi error: User already has more than 'max_user_connections' active connections

When you see the error User dbo343879423 already has more than 'max_user_connections' active connections, it is not a mystery regarding your server configuration or a difference between localhost and production. It is a hard limit enforced by the MySQL server itself, triggered specifically because your application code is creating too many simultaneous connections for that specific user account.

The error message points to line 11 in class_database.php:

$this->connection = new mysqli($hostname,$username,$password,$database);

This line executes every time you instantiate a new instance of the database class. In your provided code, you are using a singleton-like pattern but actually creating a fresh connection object for every function call and every request block. Look at how you are instantiating the object in your view:

$object_item = new __database(DB_HOST,DB_USER,DB_PASS,DB_NAME);
$item = $object_item->fetch_assoc($sql);

$object_item_num = new __database(DB_HOST,DB_USER,DB_PASS,DB_NAME); // New connection created here
$total_item = $object_item_num->num_rows($sql);

Here, you create a new __database object to fetch the data, and then immediately create another one just to get the row count. You repeat this pattern for tags as well. While your __destruct() method calls $this->connection->close(), there is a race condition inherent in this approach. If multiple HTTP requests are processed simultaneously (which they are), or if PHP's garbage collection hasn't run immediately, those connections remain "active" from the server's perspective.

The Root Cause: Connection Leaks vs. Limits

The max_user_connections variable in MySQL dictates how many simultaneous connections a specific user can have open. By default, this is often set to 100 or 150. If your script spawns enough objects in parallel across multiple requests that exceed this threshold, the server rejects new connections with error 1203.

The fact that it works on localhost suggests you might be hitting a much higher default limit there (often unlimited or very high on local development environments), whereas your Unix host has a strict cap.

The Solution: Reuse Connections

The standard best practice for database drivers like mysqli is to reuse an existing connection rather than opening and closing one for every single query. Your current class design initializes a new connection in the constructor, but the connection is never reused across different methods within the same object instance because you are creating a new object for each task.

To fix this, you should refactor your __database class to maintain a single persistent connection that is reused throughout the lifecycle of the script or session. Here is how you can modify the constructor and the query methods:

<?php
class Database {
    protected $connection = null;

    public function __construct($hostname, $username, $password, $database) {
        // Connect once per instance
        $this->connection = new mysqli($hostname, $username, $password, $database);
        
        if ($this->connection->connect_error) {
            die("Connect failed: " . $this->connection->connect_error);
        }
    }

    // Reuse the existing connection for all methods
    public function fetch_assoc($query) {
        return $this->connection->query($query)->fetch_assoc();
    }

    public function num_rows($query) {
        return $this->connection->query($query)->num_rows;
    }

    // Ensure the connection is closed when the object is destroyed
    public function __destruct() {
        if ($this->connection) {
            $this->connection->close();
        }
    }
}
?>

Implementation Strategy in Views

Even with a single instance per page, if you are creating multiple instances within a single view (like your video library snippet), you risk exhausting the connection pool. Instead of instantiating the class multiple times in one file, instantiate it once near the top of your layout or include file, and pass that object around:

// Instantiate once
$db = new Database(DB_HOST, DB_USER, DB_PASS, DB_NAME);

// Reuse the same instance for all queries
$item = $db->fetch_assoc($sql1);
$total_item = $db->num_rows($sql2); // Note: num_rows usually requires a count query or specific logic if using LIMIT

Trade-offs and Considerations

There is a trade-off here regarding application state. A persistent connection object holds the current transaction context (autocommit settings, character set). If you are building a complex application where different parts of the code need completely isolated database contexts, relying on a single global instance can be risky. In such cases, using PDO with prepared statements and a connection pooler like `

Our network