gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 07:10:00 32 4
gpt4 key购买 nike

大约 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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com