简体   繁体   English

MySQL和CodeIgniter 3:连接错误过多

[英]MySQL & CodeIgniter 3 : Error too many connection

So I have a little problem here, I'm using CodeIgniter 3. Sometimes when I try to access my website. 所以我在这里有一个小问题,我正在使用CodeIgniter3。有时,当我尝试访问我的网站时。 It comes to an error too many connection. 出现太多连接错误。

This is my database.php 这是我的database.php

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'testing',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

Any correct code to solve this problem ? 任何正确的代码可以解决此问题?

Thanks for any help :) 谢谢你的帮助 :)

Off the mysql.allow_persistent because if it is on. 关闭mysql.allow_persistent因为如果打开了。 Then it create connections again and again 然后它一次又一次地创建连接

//Set mysql.allow_persistent ON to Off
mysql.allow_persistent = Off

Note : If Persistent connections is on, that do not close when the execution of your script ends 注意:如果持久连接已打开,则在脚本执行结束时不会关闭

Also Read Too many connections 另请阅读太多连接

I was recently stumped by the fact that one of my controllers was maxing out connections to the database. 最近,我感到困惑的是,我的一个控制器正在最大化与数据库的连接。 Adding in some debug logging revealed that it was creating a new connection for every single query, and holding them open, so they were rapidly exhausted. 添加一些调试日志记录表明,它正在为每个查询创建一个新的连接,并使它们保持打开状态,因此它们很快就用尽了。 None of my other controllers did this, they all used a single connection per request. 我的其他控制器都没有这样做,每个请求都使用单个连接。

After much head-scratching, what eventually solved it was adding $this->load->library('session'); 经过大量的努力之后,最终解决的方法是添加$this->load->library('session'); to the controller's constructor, after $this->load->database(); $this->load->database();之后到控制器的构造函数$this->load->database(); . All my other controllers had that, but this particular controller didn't do any session-related stuff so I hadn't bothered with it. 我所有其他控制器都具有该功能,但是这个特定的控制器没有执行任何与会话相关的工作,因此我不必理会它。 But for some reason it seems to be necessary to load it in order to use a single database connection for the lifetime of the request. 但是出于某种原因,为了在请求的生存期内使用单个数据库连接,似乎有必要对其进行加载。

This was in CodeIgniter 2.x, but much of the 3.x code is the same, so it might apply there too. 这是在CodeIgniter 2.x中的,但是3.x的大部分代码是相同的,因此它也可能适用于此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM