PHP-MySQLi 连接随机失败,出现 "Cannot assign requested address"

标签 php mysql debian

大约 2 周以来,我一直在处理 LAMP 堆栈中最奇怪的问题之一。 长话短说随机连接到 MySQL 服务器失败并显示错误消息:

Warning:  mysqli::real_connect(): (HY000/2002): Cannot assign requested address in ..

MySQL 在不同的“盒子”上,托管在 Rackspace Cloud 今天我们将它的版本降级为

Ver 14.14 Distrib 5.1.42, for debian-linux-gnu (x86_64).

根据它的状态变量,数据库服务器非常忙于处理每秒平均查询数:5327.957。

MySQL 在 log-warnings=9 中,但没有记录连接被拒绝的警告。 站点和 gearman worker 脚本都以 1% 的概率失败并出现该错误。 没有服务器负载似乎不是我们监控的一个因素。 (CPU 负载、IO 负载或 MySQL 负载) 最大数据库连接数 (max_connections) 设置为 200,但我们从未处理过超过 100 个同时连接到数据库的情况

无论有没有防火墙软件,都会发生这种情况。

我怀疑是 TCP 网络问题,而不是 PHP/MySQL 配置问题。

谁能告诉我如何找到它?

更新:

连接代码为:

$this->_mysqli = mysqli_init(); 
$this->_mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 120); 
$this->_mysqli->real_connect($dbHost,$dbUserName, $dbPassword, $dbName); 

if (!is_null($this->_mysqli->connect_error)) {
    $ping = $this->_mysqli->ping(); 

    if(!$ping){
        $error = 'HOST: {'.$dbHost.'};MESSAGE: '. $this->_mysqli->connect_error ."\n"; 
        DataStoreException::raiseHostUnreachable($error);
    }
} 

最佳答案

我遇到了这个问题并使用持久连接模式解决了它,可以通过在 mysqli 中通过在数据库主机名前加上 'p:' 来激活它

$link = mysqli_connect('p:localhost', 'fake_user', 'my_password', 'my_db');

来自: http://php.net/manual/en/mysqli.persistconns.php :

The idea behind persistent connections is that a connection between a client process and a database can be reused by a client process, rather than being created and destroyed multiple times. This reduces the overhead of creating fresh connections every time one is required, as unused connections are cached and ready to be reused. ...

To open a persistent connection you must prepend p: to the hostname when connecting.

关于PHP-MySQLi 连接随机失败,出现 "Cannot assign requested address",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3210270/

相关文章:

mysql - 将 WHERE 与 COALESCE 一起使用非常慢

bash - 如何使用位置替换字母

c - 是什么导致调用 inb_p() 时出现段错误?

php - Cron 作业错过了 wget 上的 JSON get 请求

php - 更改链接,从 id 到 names

php - Symfony Panther 中 getInternalResponse 的 getHeader 始终返回空数组

php - MySQL SELECTS 的开销——最好按顺序使用一个或多个

php - 打印下拉菜单的可见名称而不是其值名称

linux - 无法在 Debian 8 Jesse 上安装 Code::Blocks IDE

php/MySQL - 如何将 'cart' 表连接在一起