MYSQLi错误:用户已经有超过'max_user_connections'的活动连接

时间:2010-11-02 15:58:34

标签: php mysql error-handling mysqli

我在运行的网站上有以下错误。我不明白为什么它在我的localhost上工作正常。这与主持人有关吗?我在Unix服务器上。

Warning: mysqli::mysqli() [mysqli.mysqli]: (42000/1203): User dbo343879423 already has more than 'max_user_connections' active connections in /homepages/9/d322397966/htdocs/dump/models/class_database.php on line 11
Connect failed: User dbo343879423 already has more than 'max_user_connections' active connections 
Warning: mysqli::close() [mysqli.close]: Couldn't fetch mysqli in /homepages/9/d322397966/htdocs/dump/models/class_database.php on line 160

错误显示'用户dbo343879423已经在第11行的/homepages/9/d322397966/htdocs/dump/models/class_database.php中有超过'max_user_connections'活动连接,所以这是脚本中的第11行 - 我看不出任何错误!

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

下面是class_database.php中的整个类,在脚本的其他部分是否有错,我应该更改?

<?php
#connects the database and handling the result
class __database {

    protected $connection = null;
    protected $error = null;

    #make a connection
    public function __construct($hostname,$username,$password,$database)
    {
        $this -> connection = new mysqli($hostname,$username,$password,$database);

        if (mysqli_connect_errno()) 
        {
            printf("Connect failed: %s\n", mysqli_connect_error());
            exit();
        }
    }

    #fetches all result rows as an associative array, a numeric array, or both
    public function fetch_all($query) 
    {
        $result = $this -> connection -> query($query);
        if($result) 
        {
            return $result -> fetch_all(MYSQLI_ASSOC);
        } 
        else
        {
            $this -> error = $this -> connection -> error;
            return false;
        }
    }

    #fetches a result row as an associative array, a numeric array, or both
    public function fetch_assoc_while($query)
    {
        $result = $this -> connection -> query($query);
        if($result) 
        {
            while($row = $result -> fetch_assoc())
            {
                $return_this[] = $row;
            }

            if (isset($return_this))
            {
                return $return_this;
            }
            else
            {
                return false;
            }
        }
        else
        {
            $this -> error = $this -> connection -> error;
            return false;
        }
    }

    #fetch a result row as an associative array
    public function fetch_assoc($query)
    {
        $result = $this -> connection -> query($query);
        if($result) 
        {
            return $result -> fetch_assoc();
        } 
        else
        {
            $this -> error = $this -> connection -> error;
            return false;
        }
    }

    #get a result row as an enumerated array
    public function fetch_row($query)
    {
        $result = $this -> connection -> query($query);
        if($result) 
        {
            return $result -> fetch_row();
        } 
        else
        {
            $this -> error = $this -> connection -> error;
            return false;
        }
    }

    #get the number of rows in a result
    public function num_rows($query)
    {
        $result = $this -> connection -> query($query);
        if($result) 
        {
            return $result -> num_rows;
        } 
        else
        {
            $this -> error = $this -> connection -> error;
            return false;
        }
    }

    #performs a query on the database
    public function query($query)
    {
        $result = $this -> connection -> query($query); 
        if($result) 
        {
            return $result;
        } 
        else
        {
            $this -> error = $this -> connection -> error;
            return false;
        }

    }

    #escapes special characters in a string for use in a SQL statement, taking into account the current charset of the connection
    public function real_escape_string($string)
    {
        $result = $this -> connection -> real_escape_string($string);   
        if($result) 
        {
            return $result;
        } 
        else
        {
            $this -> error = $this -> connection -> error;
            return false;
        }

    }

    #display error
    public function get_error() 
    {
        return $this -> error;
    }

    #closes the database connection when object is destroyed.
    public function __destruct()
    {
        $this -> connection -> close();
    }
}
?>

或者我应该只是改变主机!??

下面是数据库连接类的实现。如果我把这部分拿出来,错误将不再出现,但我也会在网站的其他部分也这样做,它们不会造成任何问题!

<!-- side-video-library -->
<div id="side-video-library" class="round-corner">

    <h4><a href="<?php echo HTTP_ROOT;?>videos"><span>ENER VIDEO LIBRARY</span></a></h4>

    <?php
    $sql = "
    SELECT *
    FROM root_pages

    WHERE root_pages.parent_id = '8'
    AND root_pages.pg_highlight = '1'
    AND root_pages.pg_hide != '1'
    ORDER BY rand() DESC
    LIMIT 1
    ";

    #instantiate the object of __database class
    $object_item = new __database(DB_HOST,DB_USER,DB_PASS,DB_NAME);
    $item = $object_item -> fetch_assoc($sql);

    #instantiate the object of __database class
    $object_item_num = new __database(DB_HOST,DB_USER,DB_PASS,DB_NAME);
    $total_item = $object_item_num -> num_rows($sql);
    //echo $total_item;
    ?>

    <?php
    if ($total_item > 0)
    {
        $sql = "
        SELECT *
        FROM root_tagged

        LEFT JOIN root_tags ON ( root_tags.tag_id = root_tagged.tag_id )

        WHERE root_tagged.pg_id = '".$item['pg_id']."'
        ";

        #instantiate the object of __database class
        $object_tagname = new __database(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $item_tagname = $object_tagname -> fetch_assoc($sql);

        #instantiate the object of __database class
        $object_tagname_num = new __database(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $total_tagname = $object_tagname_num -> num_rows($sql);
    ?>
    <p class="item-video">
        <object style="width: 183px; height: 151px;" width="183" height="151" data="http://www.youtube.com/v/<?php echo get_video_id($item['pg_content_1']) ;?>" type="application/x-shockwave-flash">
            <param name="wmode" value="transparent" />
            <param name="src" value="http://www.youtube.com/v/<?php echo get_video_id($item['pg_content_1']) ;?>" />
        </object>
    </p>

    <h3><a href="<?php echo HTTP_ROOT.str_replace(' ', '-', 'videos').'/'.$item_tagname['tag_name'].'/'.str_replace(' ', '-', strtolower($item['pg_url']));?>"><?php if(strlen($item['pg_title']) > 20) echo substr($item['pg_title'], 0,20).'...'; else echo $item['pg_title'];?></a></h3>

    <p class="item-excerpt-video"><?php if(strlen($item['pg_content_2']) > 100) echo substr($item['pg_content_2'], 0,100).'...'; else echo $item['pg_content_2'];?></p>
    <a href="<?php echo HTTP_ROOT;?>videos" class="button-arrow"><span>More</span></a>
    <?php
    }
    ?>
</div>
<!-- side-video-library -->

我是否一直在错误地实施课程?

感谢。

6 个答案:

答案 0 :(得分:10)

问题可能是您只允许少量连接,当您的类尝试获取新连接时,您会遇到此错误。

这不是编程问题,只是可用资源的数量。使用此类的任何其他脚本都有错误。

您必须在服务器上的mysql配置文件上配置更多连接。如果您没有此访​​问权限,请要求支持人员进行此操作,或者更改为允许更多连接的托管公司!

其他选项是在此类上实现Singleton模式,因此它重用相同的连接池,并且不会爆炸限制。

答案 1 :(得分:5)

如果您收到此max_user_connections消息,请先优化数据库表。

如何优化数据库表和查询​​:

  1. 索引mysql中的表字段
  2. 在选择查询中删除`*`并编写您需要的字段
  3. 关闭mysql_connection

答案 2 :(得分:3)

检查MySQL服务器上的MAX USER_CONNECTIONS设置以供用户使用。在PHPMyAdmin中,转到服务器页面(单击Server:&lt;&gt;),然后在子菜单中单击priviledges。编辑用户dbo343879423,MAX USER_CONNECTIONS将在右侧。默认情况下,我认为它设置为0(无限制),您的可能会受到限制,具体取决于谁设置服务器。

我不确定您的Database类是如何使用的,但如果您多次实例化该类,请考虑在数据库类中创建一个私有静态变量Database并创建一个公共静态方法getDatabase(),它实例化数据库连接,如果它为null并返回实例。

答案 3 :(得分:1)

为了它的价值,我想要包含一个情况,我遇到了由于占位符不正确而收到此消息的情况:

      $sql_str = 'SELECT prod_id FROM ' . $this->table_name["product"] . ' WHERE prod_sku =:p_sku';
    $arr[':prod_sku'] = $s_sku;

另外我做了大量的查询。我怀疑错误与大量查询相结合导致了这个问题。当我修复查询时,连接会在离开时发出。

答案 4 :(得分:1)

对于Godaddy共享主机,您无法更改MAX_USER_CONNECTION值。要找到它,请单击服务器&lt;&gt;,然后单击菜单栏中的变量。我的设定为200。

答案 5 :(得分:0)

使用命令行检查登录到mysql的连接数并运行此命令

SHOW VARIABLES LIKE 'max_user_connections

步骤1:在/etc/my.cnf中查看设置

max_user_connections = <set number that you want>

max_user_connections =

您也可以从mysql命令行设置

SET GLOBAL max_user_connections = 0;

重新启动apache服务器