MySQL/PHP 错误:[2002] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10317974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL/PHP Error:[2002] Only one usage of each socket address (protocol/network address/port) is normally permitted
提问by Shane
I couldn't find a solution to this already on stackoverflow, most other related topics had to do with Apache not starting first and getting this error. My issue is that after apache is running some of my users that are connected to our php/mysql website will receive this error:
我已经在 stackoverflow 上找不到解决方案,大多数其他相关主题都与 Apache 没有首先启动并出现此错误有关。我的问题是,在 apache 运行后,我的一些连接到我们的 php/mysql 网站的用户将收到此错误:
PHP Warning: mysql_connect() [function.mysql-connect]:
[2002] Only one usage of each socket address (protocol/network address/port)
is normally permitted.
This seems to be totally random and when I monitor my worker threads for Apache, there are typically lots of idle workers available to accept new connections/requests.
这似乎完全是随机的,当我监视 Apache 的工作线程时,通常有很多空闲的工作人员可以接受新的连接/请求。
My Site is running on Windows XP SP3, Xampp 1.7.7, Quad Core, 4gigs of RAM, 1TB HD, Specs for php/mysql:
我的网站在 Windows XP SP3、Xampp 1.7.7、四核、4gigs 内存、1TB 硬盘、php/mysql 规格上运行:
Apache/2.2.21 (Win32) mod_ssl/2.2.21 OpenSSL/1.0.0e PHP/5.3.8 mod_perl/2.0.4 Perl/v5.10.1
Apache/2.2.21 (Win32) mod_ssl/2.2.21 OpenSSL/1.0.0e PHP/5.3.8 mod_perl/2.0.4 Perl/v5.10.1
Any help on what I should change in any of my configurations to make this go away would be greatly appreciated. I've looked on google and even on Xampp forums but most people experience this issue prior to launching Apache, but Apache is running fine for me when users are experiencing this error.
任何有关我应该在我的任何配置中更改以使其消失的任何帮助将不胜感激。我在谷歌甚至 Xampp 论坛上都看过,但大多数人在启动 Apache 之前都会遇到这个问题,但是当用户遇到这个错误时,Apache 对我来说运行良好。
回答by DolDurma
you can modify windows REGISTRYto fix that,
你可以修改窗口REGISTRY来解决这个问题,
first in regedit open this path:
首先在regedit中打开这个路径:
HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters
HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters
and create 4 new DWORDas this key and values:
并创建 4 个新的DWORD键和值:
TcpTimedWaitDelay
REG_DWORD: 0000001e (hex)
MaxUserPort
REG_DWORD: 0000fffe (hex)
TcpNumConnections
REG_DWORD: 00fffffe (hex)
TcpMaxDataRetransmissions
REG_DWORD: 00000005 (hex)
like with this screen shot:
就像这个屏幕截图:
回答by Charles
The error isn't actually coming from MySQL, but from Windows itself:
该错误实际上不是来自 MySQL,而是来自 Windows 本身:
When a connection is closed, on the side that is closing the connection the 5 tuple { Protocol, Local IP, Local Port, Remote IP, Remote Port} goes into a TIME_WAIT state for 240 seconds by default.
In this case, the protocol is fixed - TCP
The local IP, remote IP and remote PORT are also typically fixed. So the variable is the local port.
What happens is that when you don't bind a port in the range 1024-5000 is used. So roughly you have 4000 ports. If you use all of them in 4 minutes - meaning roughly you make 16 web service calls per second for 4 minutes you will exhaust all the ports. That is the cause of this exception.
当连接关闭时,在关闭连接的一侧,5 元组 { Protocol, Local IP, Local Port, Remote IP, Remote Port} 默认进入 TIME_WAIT 状态 240 秒。
在这种情况下,协议是固定的 - TCP
本地 IP、远程 IP 和远程端口通常也是固定的。所以变量是本地端口。
发生的情况是,当您不绑定端口时,会使用 1024-5000 范围内的端口。所以大约有 4000 个端口。如果您在 4 分钟内使用所有这些 - 这意味着大约每秒进行 16 次 Web 服务调用,持续 4 分钟,您将耗尽所有端口。这就是这个异常的原因。
In other words, you've run out of ports in the dynamic range. That probably shouldn't be happening. How many concurrent users are you dealing with here?
换句话说,您已经用完了动态范围内的端口。那可能不应该发生。您在这里处理多少并发用户?
The linked blog has workarounds:
链接的博客有解决方法:
- Increase the dynamic port range through registry editing.
- Reduce the time the system wants connections to spent in
TIME_WAITthrough registry editing. - Run a bit of code to do the above registry edit without regedit.
- 通过注册表编辑增加动态端口范围。
TIME_WAIT通过注册表编辑减少系统希望连接花费的时间。- 运行一些代码来在没有 regedit 的情况下进行上述注册表编辑。
What a wide variety of workarounds!
各种各样的解决方法!
See also this question on the Visual Studio forums, which explains:
另请参阅Visual Studio 论坛上的此问题,其中解释了:
The port will be locked for another minute or two to catch all packets which might have been sent before the application was terminated but haven't arrived yet. In Winsock API you can set socket option SO_REUSEADDR to resolve this (also this option can be set in .NET Socket class), but TcpListener is too high-level and doesn't let you set this option.
端口将再锁定一两分钟,以捕获在应用程序终止之前可能已发送但尚未到达的所有数据包。在 Winsock API 中,您可以设置套接字选项 SO_REUSEADDR 来解决此问题(也可以在 .NET Socket 类中设置此选项),但 TcpListener 太高级,不允许您设置此选项。
It's very likely that the underlying code to connect to MySQL or the code that handles connections in Apache isn't trying to use SO_REUSEADDR.
连接到 MySQL 的底层代码或在 Apache 中处理连接的代码很可能没有尝试使用SO_REUSEADDR.
I'm going bet that your changing of the keepalive timeout had a direct impact here. Even though reducing it theoreticallyfrees up the socket, Windows disagrees and keeps the socket reserved.
我敢打赌,您对 keepalive 超时的更改在这里会产生直接影响。尽管理论上减少它可以释放套接字,但 Windows 不同意并保留套接字。
回答by thomas Hymanson
Happens to me when developing my Laravel projects. For developmental purposes, it's no biggie especially when you are using windows system. But for production, I simply run on Linux and problem solved. Also, change the MySQL connection from "localhost" to "127.0.0.1" if possible.
在开发我的 Laravel 项目时发生在我身上。出于开发目的,这没什么大不了的,尤其是当您使用 Windows 系统时。但是对于生产,我只是在 Linux 上运行并解决了问题。此外,如果可能,将 MySQL 连接从“localhost”更改为“127.0.0.1”。
回答by Ares9323
This is the Doldurmaanswer but for lazy people, you just have to place this inside a .reg file and than click it (reboot was not required in my case)
这是Doldurma 的答案,但对于懒惰的人,您只需将其放在 .reg 文件中,然后单击它(在我的情况下不需要重新启动)
File content:
文件内容:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters]
"TcpTimedWaitDelay"=dword:0000001e
"MaxUserPort"=dword:0000fffe
"TcpNumConnections"=dword:00fffffe
"TcpMaxDataRetransmissions"=dword:00000005
回答by imsinu9
There is a simple Work around on this.
有一个简单的解决方法。
Download CurrPorts http://www.nirsoft.net/utils/cports.html- a standalone freeware tool that lists was programs and services are using which ports. Sure, you can use netstat, the Windows Administrative Tools, and Task Manager to figure this out yourself, but CurrPorts does that all for you.
下载 CurrPorts http://www.nirsoft.net/utils/cports.html- 一个独立的免费软件工具,列出了程序和服务正在使用哪些端口。当然,您可以使用 netstat、Windows 管理工具和任务管理器自己解决这个问题,但 CurrPorts 会为您完成这一切。
Run Currports as administrator >> Select the service/application that creating problem by running to same port >> Stop the service. To stop service -
以管理员身份运行 Currports >> 选择通过运行到相同端口而产生问题的服务/应用程序 >> 停止服务。停止服务 -
- Direct from Currports application.
- Start>>Run>>service.msc>> (select from the list).
- TaskManager.
- 直接来自 Currports 应用程序。
- 开始>>运行>>service.msc>>(从列表中选择)。
- 任务管理器。
Hope you boost me up by polling.
希望你通过投票来鼓舞我。
回答by paperclip
FWIW, I had the same issue on a similar setup (Windows XP). The same code worked without this issue on Mac (OS X).
FWIW,我在类似的设置(Windows XP)上遇到了同样的问题。相同的代码在 Mac (OS X) 上没有这个问题。
I resolved the issue by using a persistent connection within the MySQLi class.
我通过在 MySQLi 类中使用持久连接解决了这个问题。
See here for more information: http://php.net/mysqli.persistconns
有关更多信息,请参见此处:http: //php.net/mysqli.persistconns
Make sure you are aware of all the gotchas / caveats associated to using a persistent connection.
确保您了解与使用持久连接相关的所有问题/注意事项。


