Php 数据库错误:[mysqli.mysqli]:用户已经有超过“max\u User\u connections”个活动连接
我有一个每天只有大约100人的网站,但我作为用户登录时收到以下错误消息:Php 数据库错误:[mysqli.mysqli]:用户已经有超过“max\u User\u connections”个活动连接,php,mysql,database,mysqli,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:
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
我刷新了几次页面,现在一切正常,但由于我没有那么多用户,我怀疑代码中有错误,我应该在哪里查找它
Thx
编辑:这是模型文件:
<?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;
}
}
?>
查看处理数据库连接的代码。你在用游泳池吗?您使用的是PHP数据库抽象框架吗 您正在处理每个数据库访问中的连接吗?如果是这样的话,您正在寻找没有显式释放/关闭数据库连接的代码。
如果你是这样做的,我建议你看一篇这样的文章:在你的网站上打开数据库连接的每次点击都使用相同的数据库用户名和密码。您的数据库设置限制了每个用户的连接数,您的连接数超过了最大值 查看此MySQL手册页面: 您的模型类不是很好,因为它似乎在每个查询上打开和关闭数据库连接。这是一种非常糟糕的资源使用,因为打开和关闭连接非常昂贵。我会在您的模型对象上编写一个析构函数,调用$db->close,并将getConnection更改为打开一次连接,然后每次都返回它。这意味着将模型类转换为非静态用法,但在数据库上这样做要容易得多
无论如何,在你的类进行连接和断开连接的过程中,MySQL可能有连接正在备份,并且在你达到最大用户限制之前,它们的清除速度不够快。这应该可以解决你的问题,但我没有测试它。区别在于:如果第一次调用getConnection,则建立并存储一个连接。其余时间,将使用已建立的连接 我删除了closeConnection中的操作,因为这会使第一个更改无效。尽管从execSQL中删除closeConnection调用会更好 据我所知,通常情况下,只要mysqli不支持持久性,脚本终止时数据库连接就会自动关闭。但最好在所有数据库工作完成后手动调用正在工作的closeConnection
<?php
class Model {
private $created;
private $modified;
private static $db = false;
static function getConnection()
{
/*
Connect to the database and return a
connection or null on failure
*/
if (self::$db === false) {
self::$db = new mysqli (DB_HOST, DB_USER, DB_PASS, DB_NAME);
}
if(!self::$db) {
echo mysql_error();
throw new Exception('Could not connect to database', EX_NO_DATABASE_CONNECTION);
}
return self::$db;
}
static function closeConnection()
{
// self::$db->close();
}
// moar functions (...)
}
?>
而且。。。我建议使用现有的数据库访问抽象层,以避免将来出现此类问题。这里有两个问题;一个加剧了另一个 @zombat发现了一个更大的问题:您不需要为每个查询连接和断开连接。虽然MySQL有一个快速的安装和拆卸周期,但它会浪费其他资源。在代码的设置步骤中打开连接一次,然后在每次查询中重复使用连接,直到页面结束,这样做更有意义。我建议对mysqli对象使用实例变量 如果您有一个结构,其中有多个数据库,而哪个数据库取决于对象,那么您需要增强数据库处理程序,以跟踪它打开了哪些数据库连接,从而只打开它需要的数据库连接。但这是一个更高级的话题,大多数人不需要去做 这里播放的其他资源是MySQL连接。如果mysqli正在创建持久连接,它实际上不会关闭与MySQL的连接,事实上,它还应该重新使用连接,这样你就不会有这个问题,但我离题了。MySQL对超时连接的默认值是几个小时,所以您可能会遇到这个限制。如果您在ShowProcessList中看到数百个休眠线程,那么这就是正在发生的情况。要更改的参数是wait\u timeout。max_连接也可能太低
但是我建议您首先修复数据库处理程序。谢谢,那么我基本上应该像处理$db一样吗?创建另一个函数:$results->close?老实说,我不确定。我删除了我的评论,因为这有点超出我的深度。但是,如果您查看mysqli的文档,您会注意到它强烈地暗示查询结果集是到数据库的缓冲连接。虽然$db->close似乎会结束连接,但query文档表明它正在使用数据库连接检索结果。谢谢,我会调查的。