PHP-MySQLi连接随机失败,并显示“无法分配请求的地址". [英] PHP-MySQLi connection randomly fails with "Cannot assign requested address"

查看:218
本文介绍了PHP-MySQLi连接随机失败,并显示“无法分配请求的地址".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Since about 2 weeks I'm dealing with one of the weirdest problems in LAMP stack. Long story short randomly connection to MySQL server is failing with error message:

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

MySQL位于Rackspace Cloud托管的不同盒子"上 今天,我们将其版本降级为

The MySQL is on different "box", hosted at Rackspace Cloud Today we downgraded it's version to

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

根据数据库服务器的状态变量,数据库服务器正在忙于处理每秒平均查询数:5239.57.

The DB server is pretty busy dealing with Queries per second avg: 5327.957 according to it's status variable.

MySQL处于log-warnings = 9,但没有记录有关拒绝连接的警告. 现场和齿轮工工人脚本均因该错误而失败,比如说概率为1%. 没有服务器负载似乎并不是我们监控的因素. (CPU负载,IO负载或MySQL负载) 最大数据库连接数(max_connections)设置为200,但我们从未处理过与数据库的同时连接数超过100的情况

MySQL is in log-warnings=9 but no warring for connection refused are logged. Both site and gearman workers scripts fail with that error at let's say 1% probability. No server load DO NOT seems to be a factor as we monitor. (CPU load, IO load or MySQL load) The maximum DB connections (max_connections) are setted to 200 but we have never dealed with more than 100 simultaneous connections to the database

在有和没有防火墙软件的情况下都会发生.

It happens with and without the firewall software.

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

I suspect TCP Networking problem rather than PHP/MySQL configurationn problem.

任何人都可以给我提示找到它的方法吗?

Can anyone give me clue how to find it?

更新:

连接代码为:

$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);
    }
} 

推荐答案

MySQL:使用大量连接

频繁连接有什么危险?
它工作得很好,除了一些极端情况.如果每秒从同一盒中获得数百个连接,则可能会耗尽本地端口号.修复它的方法可能是-在Linux上减少"/proc/sys/net/ipv4/tcp_fin_timeout"(这会破坏TCP/IP标准,但您可能不在乎本地网络),增加"/proc/sys/net/ipv4/ip_local_port_range"在 client 上.其他操作系统具有类似的设置.您也可以为同一数据库主机使用更多的Web框或多个IP,以解决此问题.我真的在生产中看到了这一点.

What are dangers of frequent connects ?
It works well, with exception of some extreme cases. If you get hundreds of connects per second from the same box you may get into running out of local port numbers. The way to fix it could be - decrease "/proc/sys/net/ipv4/tcp_fin_timeout" on linux (this breaks TCP/IP standard but you might not care in your local network), increase "/proc/sys/net/ipv4/ip_local_port_range" on the client. Other OS have similar settings. You also may use more web boxes or multiple IP for your same database host to work around this problem. I've realy seen this in production.

有关此问题的一些背景:
TCP/IP连接由localip:localport remoteip:remote端口标识.在这种情况下,我们具有固定的MySQL IP和端口以及客户端IP,因此我们只能更改具有有限范围的本地端口.注意即使关闭连接后,TCP/IP堆栈也必须保留端口一定时间,这才是tcp_fin_timeout的来源.

Some background about this problem:
TCP/IP connection is identified by localip:localport remoteip:remote port. We have MySQL IP and Port as well as client IP fixed in this case so we can only vary local port which has finite range. Note even after you close connection TCP/IP stack has to keep the port reserved for some time, this is where tcp_fin_timeout comes from.

这篇关于PHP-MySQLi连接随机失败,并显示“无法分配请求的地址".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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