持久连接在codeIgniter中不能与mysqli驱动程序一起使用 [英] Persistant connections not working with mysqli driver in codeIgniter

查看:180
本文介绍了持久连接在codeIgniter中不能与mysqli驱动程序一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本地开发环境中有这个db配置

I have this db-configuration on my local development environment

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = ''; //Actual username is put inside these quotes
$db['default']['password'] = '';
$db['default']['database'] = ''; //Actual name of database is put inside quotes
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = APPPATH .'cache';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

当我转移到这个生产服务器不工作,但是似乎工作的一件事是将dbdriver更改为 mysqli 而不是mysql。但是我也把db_debug设置为FALSE(所以它工作将不是正确的语句)

When I transfer to this to a production server it doesn't work so I've tried a lot of things, but one thing that seemed to work was to change the dbdriver to mysqli instead of mysql. But I also hade to put db_debug to FALSE (so it "worked" wouldn't be the correct statement)

我已经阅读了很多,在任何地方找到这个答案。 (我不满意:更改为debug = false,它会工作)

I have read about this a lot, but I haven't found an answer to this anywhere. (I'm not satisfied with: "Change to debug = false and it would work")

我想看看实际的问题是什么,所以我更改了本地服务器到mysqli驱动程序,然后我得到的错误:

I wanted to see what the actual problem was so I changed the local server to mysqli driver as well and then I got the error:

A Database Error Occurred

Unable to connect to your database server using the provided settings.

Filename: C:\Program Files\wamp\www\mellomgarden2\system\database\DB_driver.php

Line Number: 124



在一些挖掘之后,我发现db_connect()和db_pconnect()的工作方式完全相同:

After some digging I see that db_connect() and db_pconnect() are working in the exact same manner:


  1. 如果你查看system / database / drivers / mysqli / mysqli_driver.php - 看起来像 connect / code>和 pconnect()的工作方式完全相同,因为 pconnect code> connect() function。

  1. If you look in the system/database/drivers/mysqli/mysqli_driver.php - it seems like connect() and pconnect() are working exactly the same way because pconnect() is just calling connect() function.

'default'] ['pconnect'] = TRUE; 在使用mysqli驱动程序时完全无用。

so $db['default']['pconnect'] = TRUE; is totally useless when using mysqli driver.

function db_connect()
{
    if ($this->port != '')
    {
        return @mysqli_connect($this->hostname, $this->username, $this->password, $this->database, $this->port);
    }
    else
    {
        return @mysqli_connect($this->hostname, $this->username, $this->password, $this->database);
    }

}
// --------------------------------------------------------------------

/**
 * Persistent database connection
 *
 * @access  private called by the base class
 * @return  resource
 */
function db_pconnect()
{
    return $this->db_connect();
}

仔细观察上面的db_connect()和db_pconnect抑制。我删除了@的返回值,然后得到:

Taking a closer look at db_connect() and db_pconnect() above - erorrs are suppresed. I removed the @ for the return value and then got this:

严重性:警告

消息:mysqli_connect ):(08004/1040):太多连接

Message: mysqli_connect(): (08004/1040): Too many connections

文件名:mysqli / mysqli_driver.php

Filename: mysqli/mysqli_driver.php

:76

这是一个FAR更多的解释性错误

which is a FAR more explainatory error

所以我的想法是db_pconnect为mysqli驱动程序应该看像这样:

so my thought is that db_pconnect for mysqli driver should look something like this:

function db_pconnect()
    {       
                 $this->hostname = 'p:' . ltrim($this->hostname, 'p:');                                  
                 return $this->db_connect();
    }

这是CodeIgniter开发团队的全部遗漏还是我缺少一些东西? / p>

Is this a total miss from CodeIgniter development team or am I missing something?

推荐答案

参考您的其他主题我的假设,你指定使用持久连接似乎是真的,但看看CodeIgniter的源代码,我们可以得到一个不同的结论: MySQL驱动程序实际上注意到此选项开始设置。 而MySQLi驱动程序,正如你正确分析,没有。 错误报告中也提到了此问题,并已解决。

Referring to your other thread my assumption that you specified to use persistent connections seems to be true, but taking a look at the source code of CodeIgniter we can come to a different conclusion: The MySQL driver actually notices this option begin set. Whereas the MySQLi driver, as you correctly analyzed, does not. This has been also remarked in a bug report and already been fixed.

正如您可以看到整个MySQLi驱动程序类在开发分支中被重写。

As you can see the whole MySQLi driver class was rewritten in the development branch.

因此,你已经使用了持久连接,直到现在,使用它们,当切换到MySQLi,因为这个修复没有被释放...你可以尝试使用开发分支,如果这是一个选项。 (或者只替换这个单个文件...也应该工作。)

Therefore you have used persistent connections until now, but (unwillingly) stopped using them when switching to MySQLi, because this fix has not been released... you could try to use the development branch, if this is an option. (Or only replace this single file ... should work too.)

你的另一个选项,像我在其他答案中所说的,你简要估计使用的连接,并根据您的实际需要调整 max_connections 限制。

Your other option is, like I stated in my other answer, that you briefly estimate the used connections by your application and resize the max_connections limit according to your real needs.

这篇关于持久连接在codeIgniter中不能与mysqli驱动程序一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆