mysqli php 随机连接错误

mysqli php random connect error

我从 php 后端作业和 php 网页日志中随机收到以下错误。拥有运行 php 后端作业和 php 网络服务器的应用服务器。两者都连接到同一个数据库服务器。使用 php mysqli 面向对象的库连接到数据库。在 my.cnf 中将最大连接数设置为 750。没看到有那么多连接。

PHP Warning: mysqli::mysqli(): (HY000/2003): Can't connect to MySQL server on '77.777.120.81' (99) in /usr/local/dev/classes/Admin.php on line 15

Failed to connect to MySQL: Can't connect to MySQL server on '77.777.120.81' (99)

更新 1:

tcp_tw_reuse 看起来更好的解决方案。原因如下:

tcp_tw_reuse vs tcp_tw_recycle : Which to use (or both)?

原回答:

mysql error (99) 表示你运行 out of the tcp ports。

启用 tcp 回收应该可以解决它。

echo 1 >/proc/sys/net/ipv4/tcp_tw_recycle 

Credits.

正如这篇 Percona Database Performance Blog 文章中所描述的那样,您的问题是您的应用程序无法打开与 MySQL 服务器的另一个连接。您 运行 超出了本地 TCP 端口。作为解决方案,我建议 调整 TCP 参数设置

  • tcp_tw_reuse(布尔值;默认值:禁用;自 Linux 2.4.19/2.6 起) 允许为新连接重用 TIME_WAIT 套接字 从协议的角度来看是安全的。它不应该被改变 没有 advice/request 技术专家。

    It is possible to force the kernel to reuse a connection hanging in TIME_WAIT state by setting /proc/sys/net/ipv4/tcp_tw_reuse to 1. What happens in practice is that you’ll keep seeing the closed connections hanging in TIME_WAIT until either they expire or a new connection is requested. In the later case, the connection will be “relived”.

  • tcp_tw_recycle(布尔值;默认值:禁用;自 Linux 2.4 起) 启用 TIME_WAIT 套接字的快速回收。启用这个 不推荐选项,因为这会导致问题 使用 NAT(网络地址转换)。

    When you enable /proc/sys/net/ipv4/tcp_tw_recycle closed connections will not show under TIME_WAIT anymore – they disappear from netstat altogether. But as soon as you open a new connection (within the 60 seconds mark) it will recycle one of those. But everyone writing about this alternative seems to advise against it’s use. Bottom line is: it’s preferable to reuse a connection than to recycle it.

  • tcp_max_tw_buckets(整数;默认值:见下文;自 Linux 2.4 起) TIME_WAIT 状态下允许的最大套接字数 系统。此限制的存在只是为了防止简单的拒绝 - 服务攻击。 NR_FILE*2 的默认值为 根据系统中的内存进行调整。如果这 超出数量,套接字关闭并发出警告 打印。

    This parameter rules how many connections can remain in TIME_WAIT state concurrently: the kernel will simply kill connections hanging in such state above that number. For example, in a scenario where the server has a TCP port range composed of only 6 ports, if /proc/sys/net/ipv4/tcp_max_tw_buckets is set to 5, then open 6 concurrent connections with MySQL and then immediately close all 6 you would find only 5 of them hanging in the TIME_WAIT state – as with tcp_tw_recycle, one of them would simply disappear from netstat output. This situation allows to immediately open a new connection without needing to wait for a minute*.

    • When it comes to connecting to database servers, many applications chose to open a new connection for a single request only, closing it right after the request is processed. Even though the connection is closed by the client (application) the local port it was using is not immediately released by the OS to be reused by another connection: it will sit in a TIME_WAIT state for (usually) 60 seconds – this value cannot be easily changed as it is hard coded in the kernel.

    However, a second connection won’t be able to open until one of the other 5 connections in TIME_WAIT expire and release the local port it was using. The secret here, then, is to find a compromise between the number of available network ports and the number of connections we allow to remain in TIME_WAIT state. The default value of this setting is 65536, which means by default the system allows all possible connections to go over the TIME_WAIT state when closed.

PS:您的问题还有更多可能的解决方案,请阅读全文以了解问题的详细描述。