Netshine Software Limited







Home arrow Developer Blog Make Text BiggerMake Text SmallerReset Text Size

Sep 25
2009

Can't connect to MySQL on 'localhost' and/or Couldn't fetch MySQLi

Posted by netshine in windows xpwindows registrywindowstcp/ipportmysqlimysqlmultiple connectionslocalhostdatabase

I was recently writing some code to migrate data from one database to another in small chunks (so that the progress could be reported to the browser using AJAX), but while developing on my Windows XP machine, I kept getting the above 2 error messages. This would occur only after a significant amount of data had already been processed successfully.

As the processing involved opening and closing connections to 2 different databases many times, it smelt like the connections were not being closed properly, but I double-checked everything and I was indeed explicitly closing all of the connections after use. Several frustrating hours later, I discovered the cause of the problem...

Apparently, Windows only allows a maximum of 5000 TCP/IP connections by default. Each connection is held open for 2 to 4 minutes (even after you tell it to close) before being released, so when you are creating lots of connections within a short time frame, you can soon run out of connections. One way round this is to use connection pooling or persistent connections, but that can be quite a daunting prospect, and may bring some disadvantages. Fortunately, there is a simpler way: 

It is possible to change the default values in the registry, and thus allow up to 65000 connections, and to reduce the wait time to 30 seconds. There are 2 registry entries that need to be created in order to do this. (Note: Modifying the windows registry can completely mess up your machine if you do not know what you are doing, so proceed at your own risk!).

To increase the number of connections allowed, open regedit (on Windows XP, click Start->Run, type 'regedit' , and press enter). Navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters. Right click on the right-hand pane, and select New->DWORD Value. Enter the name 'MaxUserPort', and press enter. Then double-click on the new entry, click on 'Decimal', then enter 65534 as the 'value data', and click on 'OK'.

To reduce the wait time between closing a connection and it being released by Windows, you need to add another value in exactly the same way (in the same place), called TcpTimedWaitDelay, with a value of 30.

After modifying the registry, you will need to reboot the machine before the new settings take effect.

For more information, see:  http://dev.mysql.com/doc/refman/5.1/en/can-not-connect-to-server.html#can-not-connect-to-server-on-windows

In my case, I was writing software that would be distributed to others, so I could not ensure that they had those 2 registry entries in place. As a workaround, I enabled the data migration process to resume from where it left off, so that if this error occurs, and the user has not added the above entries to their registry, they just have to wait a couple of minutes for the connections to be released, and then they can kick the process off again. I did find though, that when doing this, it still kept failing every couple of minutes, so it would take a while to finish the process.



Comments (0)Add Comment

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

security code
Write the displayed characters


busy
Joomla! Tutorial
Take a look at our free tutorial for Joomla! Administrator...


Login Form





Lost Password?

© 2004-2010 Netshine Software Limited   Terms and Conditions