数据库错误:[mysqli.mysqli]:用户已经有超过'max_user_connections'活动连接

jcs*_*lzr 0 php mysql database mysqli

我有一个每天只有大约100人的网站但是当我以用户身份登录时收到此错误消息:

Warning: mysqli::mysqli() [mysqli.mysqli]: (42000/1203): User mexautos_Juan already has more than 'max_user_connections' active connections in /home/mexautos/public_html/kiubbo/data/model.php on line 26

Warning: mysqli::query() [mysqli.query]: Couldn't fetch mysqli in /home/mexautos/public_html/kiubbo/data/model.php on line 87
Query failed 
Run Code Online (Sandbox Code Playgroud)

我刷新页面几次,现在还可以,但是由于我没有那么多用户,我怀疑我的代码中出现了错误,我应该在哪里寻找它?

谢谢

编辑:这是模型文件:

<?php
/* 

    Model is the base class from which the other
    model classes will be derived. It offers basic
    functionality to access databases

*/ 
require_once($_SERVER['DOCUMENT_ROOT'].'/config.php'); 
require_once(SITE_ROOT.'includes/exceptions.php'); 

class Model {

    private $created;
    private $modified;

    static function getConnection()
    {
        /* 

            Connect to the database and return a
            connection or null on failure

        */

        $db = new mysqli (DB_HOST, DB_USER, DB_PASS, DB_NAME);
        if(!$db) {
            echo mysql_error();
            throw new Exception('Could not connect to database', EX_NO_DATABASE_CONNECTION);
        }

        return $db;

    }

    static function execSQL($query)
    {
    /*
            Execute a SQL query on the database
            passing the tablename and the sql query.
            Returns the resultset
    */


        $db = null;
        $results = null;
        //echo "query is $query";

        try
        {
            $db = Model::getConnection();
            $results = $db->query($query);
            if(!$results) {
                throw new Exception('Query failed', EX_QUERY_FAILED );
            }
        }
        catch(Exception $e)
        {
            /*  errors are handled higher in the
                    object hierarchy
            */

            throw $e;
        }

        Model::closeConnection($db);

        return $results;
    }

    static function execSQl2($query)
    {
    /*
            Execute a SQL query on the database
            passing the tablename and the sql query.
            Returns the LAST_INSERT_ID
    */


        $db = null;
        $lastid = null;
        //echo "query is $query";

        try
        {
            $db = Model::getConnection();
            $results = $db->query($query);
            if(!$results) {
                throw new Exception('Query failed', EX_QUERY_FAILED );
            }
            $lastid = $db->insert_id;
        }
        catch(Exception $e)
        {
            /*  errors are handled higher in the
                    object hierarchy
            */

            throw $e;
        }

        Model::closeConnection($db);

        return $lastid;
    }

    function delete($table, $id, $conditions = '')
    {
        $query = "delete from $table where id = $id";
        try
        {
            $db = Model::getConnection();
            $results = Model::execSQL($query);
            if(!$results){
                throw new Exception('Could not delete this object', EX_DELETE_ERROR);
            }
            return $results->num_rows;
        }

        catch(Exception $e)
        {
            throw $e;
        }
    }

    static function closeConnection($db)
    {
        $db->close();
    }

    function getCreated()
    {
        return $this->created;
    }

    function setCreated($value)
    {
        $this->created = $value;
    }

    function getModified()
    {
        return $this->modified;
    }

    function setModified($value)
    {
        $this->modified = $value;
    }



}

?>
Run Code Online (Sandbox Code Playgroud)

zom*_*bat 5

您网站上打开数据库连接的每次点击都使用相同的数据库用户名和密码.您的数据库设置限制了每个用户的连接数,并且您将超过该最大值.

查看此MySQL手册页:http: //dev.mysql.com/doc/refman/5.0/en/user-resources.html

您的模型类不是那么好,因为它似乎是在每个单独的查询上打开和关闭数据库连接.这是一种非常糟糕的资源使用,因为打开和关闭连接很昂贵.我会在你的模型对象上编写一个析构函数,它调用$ db-> close(),然后改变getConnection()来打开一次连接,然后每次都返回它.这意味着将模型类转换为非静态用法,但在数据库上这样做会容易得多.

无论如何,你可以通过所有连接和断开连接来确保MySQL有连接备份,并且在达到最大用户限制之前它们没有足够快地清除.