MySQL/PHP Error:[2002] Only one usage of each socket address (protocol/network address/port) is normally permitted

前端 未结 6 1951
陌清茗
陌清茗 2020-11-27 08:11

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 aft

相关标签:
6条回答
  • 2020-11-27 08:20

    FWIW, I had the same issue on a similar setup (Windows XP). The same code worked without this issue on Mac (OS X).

    I resolved the issue by using a persistent connection within the MySQLi class.

    See here for more information: http://php.net/mysqli.persistconns

    Make sure you are aware of all the gotchas / caveats associated to using a persistent connection.

    0 讨论(0)
  • 2020-11-27 08:24

    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.

    Run Currports as administrator >> Select the service/application that creating problem by running to same port >> Stop the service. To stop service -

    1. Direct from Currports application.
    2. Start>>Run>>service.msc>> (select from the list).
    3. TaskManager.

    Hope you boost me up by polling.

    0 讨论(0)
  • 2020-11-27 08:28

    The error isn't actually coming from MySQL, but from Windows itself:

    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.

    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:

    1. Increase the dynamic port range through registry editing.
    2. Reduce the time the system wants connections to spent in TIME_WAIT through registry editing.
    3. Run a bit of code to do the above registry edit without regedit.

    What a wide variety of workarounds!

    See also this question on the Visual Studio forums, which explains:

    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.

    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.

    I'm going bet that your changing of the keepalive timeout had a direct impact here. Even though reducing it theoretically frees up the socket, Windows disagrees and keeps the socket reserved.

    0 讨论(0)
  • 2020-11-27 08:29

    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.

    0 讨论(0)
  • 2020-11-27 08:34

    This is the Doldurma answer 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)

    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
    
    0 讨论(0)
  • 2020-11-27 08:44

    you can modify windows REGISTRY to fix that,

    first in regedit open this path:

    HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters

    and create 4 new DWORD as this key and values:

    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:

    reference

    0 讨论(0)
提交回复
热议问题