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: , , ,

55 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!

  15. Marcaum54 Says:

    In Window 7

    \system32\drivers\etc\hosts

    # Copyright (c) 1993-2009 Microsoft Corp.
    #
    # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
    #
    # This file contains the mappings of IP addresses to host names. Each
    # entry should be kept on an individual line. The IP address should
    # be placed in the first column followed by the corresponding host name.
    # The IP address and the host name should be separated by at least one
    # space.
    #
    # Additionally, comments (such as these) may be inserted on individual
    # lines or following the machine name denoted by a '#' symbol.
    #
    # For example:
    #
    # 102.54.94.97 rhino.acme.com # source server
    # 38.25.63.10 x.acme.com # x client host

    # localhost name resolution is handled within DNS itself.
    # 127.0.0.1 localhost
    # ::1 localhost

    REPLACE SOMETHING '# 127.0.0.1 localhost' BY ' 127.0.0.1 localhost'

    Discoment the row

  16. Arthur Says:

    big thanks to Marcaum54 for the Windows 7 solution..

  17. Soroush Says:

    thanks to Marcaum54. bravo!

  18. Rick Says:

    Thank you!

    Zend Framework projects suffer from this problem if you upgrade to 5.3.x on Windows.

    (In order to help Zend googlers, here's the error I got:

    Warning: PDO::__construct() [pdo.--construct]: [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://localhost:3306) in C:\Users\Rick\Zend\workspaces\ZendFramework-1.9.6\library\Zend\Db\Adapter\Pdo\Abstract.php on line 129
    )

  19. Conny Says:

    Thank you so very much, Marcaum54!!!

    I've spent 20 hours to solve this problem searcing the net and then it turned out to be such a simple solution…:-(

  20. Marcelo Says:

    Thanks to much!!!

    It works perfectly!!!

  21. Daniel Says:

    Your post helped me a lot!
    Strangely enough this problem occurred to me only on Windows 7 Premium 64bit. Vista Basic 32bit on the other hand worked fine even with IPv6 entry. Must be some setting somewhere.

  22. Gary Says:

    Wow – I reckon your post has just saved me several hours of debugging in the wrong places to find that solution. Thanks for sharing!

    I'm on WinXP SP3, running Wampserver 2, with PHP 5.3.1 selected.

    Again, to help Zend folks find this simple solution, here's the start of the error I got:

    Warning: PDO::__construct() [pdo.--construct]: [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://localhost:3306) in C:\wamp\www\project\library\Zend\Db\Adapter\Pdo\Abstract.php on line 129

  23. VisionCloud Says:

    Thank you so much, this solved hours of frustration for me.

  24. Nicholas Sherlock Says:

    Thanks so much! I can finally get back to developing!

  25. ian Says:

    Thanks a lot !

  26. redsolar Says:

    I'm working on Windows 7 with Apache, MySQL and PHP. Uncommenting the following line from the "hosts" file worked for me.

    127.0.0.1 localhost

    Thanks so much for tip! This was driving me bonkers.

  27. quark yunus Says:

    i was about to get crazy man, cool

  28. PHP Can’t connect MySQL ? Here’s the fix Says:

    [...] following info has been copied from coreygilmore.com – finding out this solution took a really long [...]

  29. Spiffe Says:

    Here's another thanks! I beat my head against this problem for several hours before coming across your blog.

  30. Kanwaljit Says:

    I cant tell what a bloody headache above error gave me, thanks a lot man.
    in my hosts file, 2 localhost entries were there:
    # localhost name resolution is handled within DNS itself.
    # 127.0.0.1 localhost
    # ::1 localhost
    and i uncommented first to get it to work, if that helps anyone :D

  31. Wayne Says:

    Me trying to solve the same problem over the past three days: no success
    Me trying to solve the same problem with your solution here: took about one minute, works great.

    Thanks so much.

  32. vaze Says:

    worked! Thanks bro!

  33. Sanjay Says:

    Thank a lot

  34. Bill Ortell Says:

    i'm in a rush, but had to say… "THANK YOU THANK YOU THANK YOU!"
    3 FULL days of beating my head against the proverbial wall, on this… not just to get it to work, but to get it to work on a PHP VC9, APACHE VC9, IMAGEMAGICK VC9 installs was DRIVING ME NUTS-O!

    sorry for the caps, but quite frankly… i'm debating whether or not to ask my wife for another child (would make 4!) ** JUST SO… ** i can name them Corey!

    Corey, sincere thanks!

  35. Masoud Zohrabi Says:

    Wooow !! Thank you so much ….

  36. Mousa Says:

    A great help! I was having the same problem with Uniserver (Uniform Server) on Win7 and for a while and kept wondering if it's because of Uniserver for a while.

    Thanks to article now I know what exactly was wrong and unlike in Vista, in Win7 I had to edit the host file to uncomment the line "# 127.0.0.1 localhost" so that it read "127.0.0.1 localhost" and bingo!

    Thanks a lot.

  37. prozac Says:

    This was driving me crazy for days.

    Thank you man

  38. Jorge Says:

    Spent 2 days "googling" and finally a simple but smart solution for Windows 7. Thanks so much. I wonder why the Microsoft's guys keep changing operating systems settings. Maybe they do not want third party software.

  39. Houston IT support Says:

    Corey this is awesome. It is for this reason that I run PHP on a linux box instead of a windows box. I will pass the word along that this fix will allow my fellow developers running PHP and MYSQL on a windows box the ability to implement IPv6 by a simple change to the hosts file is system32. Also guys if you really want a cookie cutter way to do some dev work on a windows box use XAMPP!

  40. Michael Says:

    Just wanted to say thanks, this solved a problem I was having!

  41. Php 5.3.3 Upgrade, Mysql 5 Connection Problem Solved « Zeletron Says:

    [...] my scripts' connection host parameters to "127.0.0.1". I can thank the post http://coreygilmore.com/blog/2009/11/20/fix-php-5-3-hang-on-windows/ by Corey Gilmore for that little detail. Although, it was working perfectly fine with PHP5.3.0, so [...]

  42. David Says:

    Hey Corey, thanks man! I had just upgraded from PHP5.3.0 on my Windows 7 64-bit development machine to PHP5.3.3 and PHP could no longer connect to MySQL. I replaced my few scripts containing 'localhost' with '127.0,0.1' and everything worked. There must have been some changes made to how PHP connects to MySQL for the connection to break like that.

  43. Liz Says:

    I must be the only person this hasn't worked for. I'm still getting the error message even after adjusting the hosts file. I've cleared my caches, triple and quadruple checked syntax, spelling, and variable names, I'm running out of ideas.

    I'm a PHP/MYSQL newbie and i'm going through a simple blog tutorial

    This is the line that is tripping the error:
    $db = new PDO(DB_INFO, DB_USER, DB_PASS);

    And this is the code from the include file it's pulling the information from:

  44. Spectrum Says:

    I had both ::1 and 127.0.0.1 commented but when I uncommented 127.0.0.1 and also deleted #::1 it started to work immediately, thank you very much ! =)

  45. Loz Says:

    I have spent all night trying to nut this one out – and you solve it for me in 30 seconds. Awesome, just awesome. Thanks, dude.

  46. Abdul Halim Says:

    thanks a lot, works fine

  47. Bernhard Says:

    Thanks for sharing, this saved me a lot of time!!

  48. php cannot make connection to mysql (Fix) | The Journal of Richard Craig Says:

    [...] the instructions twice, a reinstall and trying crystal healing, finally some thanks has to go to Corey Gilmore for providing the [...]

  49. Tyler Says:

    Corey,

    I've been installing/re-installing/debugging for hours trying to fix this. Thanks VERY much for saving me another 5 hours of work.

  50. Fábio Moreira Says:

    Well, it really works,
    has i've experienced, the hosts file must be uncommented if there's a line that says
    #127.0.0.1 localhost

    making it
    127.0.0.1 localhost

    and/or if there's a line that's not commented, saying:
    ::1 localhost

    comment it like that
    #::1 localhost

    and so it works great (at least for windows 7 ultimate, with apache 2.2, php 5.3 and mysql server 5.1)

  51. gabriela Says:

    GRACIAS!!! La solucion de Marcaum54 funciono prfecto!!
    fueron mi salvacion :D

  52. Radu Says:

    Hi
    Thanks, it works excellent
    Thanks for posting this

  53. youngpac Says:

    thanks you all worked like a charm

  54. Divya Says:

    Hi,

    Thank u so much for posting this,Had this problem and I Tried this and worked right away.

    http://godwinsblog.cdtech.in/2010/11/microsoft-now-supports-php.html

  55. alexbrina Says:

    Thank you so much!!! How did you find it?

Leave a Reply


© 2007-2013, 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.