Fix PHP 5.3 hang on Windows

I just upgraded to PHP 5.3 on my Windows development box, and ran into an issue making any database connection with the MySQL and MySQLi drivers. Pages not making any database connections worked fine, but any page with a database connection would sit and hang for about a minute, and then throw an error.

PHP Warning: mysqli::mysqli(): [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://localhost:3306) in xxx.php on line 2
PHP Warning: mysqli::mysqli(): (HY000/2002): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
in xxx.php on line 2

PHP bug #45150 suggests that this is due to issues resolving localhost on Windows. This is partially correct, more specifically it's an issue with localhost resolving to ::1 when IPv6 is enabled.

While it feels like Windows has shoehorned in IPv6 support, the blame seems to lie with MySQL, which won't support IPv6 until version 6.0.

I'm not sure why the timeout isn't instant, the port is closed, so it could be partially a problem with PHP or Windows.

> nmap -sT -p 80,3306,3389 -6 ::1
 
Starting Nmap 5.00 ( http://nmap.org ) at 2009-11-20 15:48 Mountain Standard Time
Interesting ports on cfg64 (::1):
PORT     STATE  SERVICE
80/tcp   closed http
3306/tcp closed mysql
3389/tcp open   ms-term-serv
 
Nmap done: 1 IP address (1 host up) scanned in 3.14 seconds

The Fix

Open up %windir%\system32\drivers\etc\hosts with a text editor and comment out the line that looks like:

::1             localhost

Prefix it with a #, like so:

#::1             localhost

Save it and your PHP/MySQL connections will immediately begin working. You could also use 127.0.0.1 in your connection string instead of localhost, but I didn't want to change code in innumerable files.

 

Tags: , , ,

14 Responses to “Fix PHP 5.3 hang on Windows”

  1. Ivan Suarez Says:

    Thak's, it works great!

  2. Brian Meissen Says:

    Hello,

    I get these error messages constantly on my server. But when I go to the hosts file, it only has
    127.0.0.1 localhost – in my experience I've never seen ::1 localhost

  3. Corey Says:

    @Brian Meissen – ::1 is the IPv6 loopback address. If you're on running Windows XP you won't see it unless you've explicitly installed IPv6 support. It's enabled by default in Vista, 7 and Server 2008.

  4. Serge Kossouho Says:

    Thanks so much Corey for sharing this information.
    Best Regards

  5. Andrew Says:

    Thank you SOOO much for this. I just upgraded my server at home to a copy of Windows 7 and after re-installing all the required applications, I went to re-setup the database for the website I've been working on… and I kept getting that stupid timeout error. I finally found a semi-solution to change everything to 127.0.0.1, but I wondered what was making it do that considering that I never had that problem in the past and have always been okay with just using 'localhost.' This was a great solution. Thank you very much.

  6. sacabonos Says:

    I am using windows 7 and the hosts file was empty, everything was commented. I had to enable the line with 127.0.0.1 localhost to make it work in my case.

  7. Dan Says:

    Wonderful. Thanks for the post. Worked great.

  8. Dave Miller Says:

    Thank you so much, I've been trying to figure this out for the last hour!

  9. Karl Says:

    Thank you very much for the contribution =D. I solve my problem as say sacabonos

  10. NYC Says:

    Thanks for the useful post man.. I was going crazy over these errors..

  11. Wichtige Punkte bei der Einrichtung eines Win2k8-Servers « TheUndeadable entwickelt Says:

    [...] In der etc/hosts festlegen, dass 127.0.0.1 auf localhost liegt und nicht ::1. Ein Fehler im MySQL-Treiber kommt nicht mit IPv6 klar und sämtliche PHP-Webseiten hängen einfach beim mysql_connect. Mit MySQL 6 wird alles besser [...]

  12. mike.eire.ca » Blog Archive » PHP 5.3 database connection problems Says:

    [...] to Corey Gilmore for documenting it; I figure the more people get it online the quicker it will be for people to [...]

  13. Matt J. Says:

    Awesome – thanks for sharing – fixed it for me! :)

  14. trevor Says:

    Great. I just changed "localhost" in my connection to "127.0.0.1" and all is well with the world again. I found your post quickly too. Just copied the entire error I was getting into google. Yours was the first result. Thanks!

Leave a Reply


© 2007-2010, Corey Gilmore | Posts RSS Feed | Comments RSS Feed | Contact

 

The views expressed on these pages are mine alone and not those of any past or present employer. All information presented on this site was obtained lawfully and not through disclosure under the terms of an NDA.