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: IPv6, MySQL, PHP, Windows



November 23rd, 2009 at 11:11 am
Thak's, it works great!
November 24th, 2009 at 8:27 am
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
November 24th, 2009 at 8:53 am
@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.
November 24th, 2009 at 10:48 am
Thanks so much Corey for sharing this information.
Best Regards
November 28th, 2009 at 6:06 pm
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.
November 30th, 2009 at 3:25 am
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.
December 7th, 2009 at 12:57 pm
Wonderful. Thanks for the post. Worked great.
December 10th, 2009 at 3:52 pm
Thank you so much, I've been trying to figure this out for the last hour!
December 12th, 2009 at 5:36 pm
Thank you very much for the contribution =D. I solve my problem as say sacabonos
December 19th, 2009 at 8:42 pm
Thanks for the useful post man.. I was going crazy over these errors..
December 26th, 2009 at 5:03 pm
[...] 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 [...]
January 3rd, 2010 at 11:42 am
[...] to Corey Gilmore for documenting it; I figure the more people get it online the quicker it will be for people to [...]
January 14th, 2010 at 1:21 am
Awesome – thanks for sharing – fixed it for me! :)
January 30th, 2010 at 10:05 am
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!
February 23rd, 2010 at 11:22 pm
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
February 24th, 2010 at 7:31 pm
big thanks to Marcaum54 for the Windows 7 solution..
March 2nd, 2010 at 2:42 am
thanks to Marcaum54. bravo!
March 17th, 2010 at 10:33 am
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
)
April 2nd, 2010 at 5:05 am
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…:-(
April 2nd, 2010 at 11:20 pm
Thanks to much!!!
It works perfectly!!!
April 9th, 2010 at 2:34 am
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.
April 11th, 2010 at 7:18 pm
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
April 30th, 2010 at 4:36 pm
Thank you so much, this solved hours of frustration for me.
May 14th, 2010 at 11:56 pm
Thanks so much! I can finally get back to developing!
May 22nd, 2010 at 7:52 am
Thanks a lot !
May 22nd, 2010 at 4:02 pm
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.
May 29th, 2010 at 5:19 pm
i was about to get crazy man, cool
May 31st, 2010 at 3:53 am
[...] following info has been copied from coreygilmore.com – finding out this solution took a really long [...]
June 8th, 2010 at 7:31 pm
Here's another thanks! I beat my head against this problem for several hours before coming across your blog.
June 25th, 2010 at 10:53 am
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
June 30th, 2010 at 8:45 pm
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.
July 19th, 2010 at 4:09 am
worked! Thanks bro!
August 7th, 2010 at 7:32 am
Thank a lot
August 10th, 2010 at 2:57 pm
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!
August 12th, 2010 at 7:12 am
Wooow !! Thank you so much ….
August 16th, 2010 at 2:15 am
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.
August 20th, 2010 at 11:29 am
This was driving me crazy for days.
Thank you man
August 20th, 2010 at 7:20 pm
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.
August 22nd, 2010 at 12:11 pm
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!
August 28th, 2010 at 3:23 pm
Just wanted to say thanks, this solved a problem I was having!
August 31st, 2010 at 6:33 pm
[...] 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 [...]
August 31st, 2010 at 6:42 pm
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.
September 7th, 2010 at 9:31 pm
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:
September 10th, 2010 at 1:42 am
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 ! =)
September 17th, 2010 at 6:12 pm
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.
September 27th, 2010 at 2:36 am
thanks a lot, works fine
October 23rd, 2010 at 1:55 pm
Thanks for sharing, this saved me a lot of time!!
October 28th, 2010 at 3:25 pm
[...] the instructions twice, a reinstall and trying crystal healing, finally some thanks has to go to Corey Gilmore for providing the [...]
November 6th, 2010 at 10:27 pm
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.
November 9th, 2010 at 10:15 am
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)
November 22nd, 2010 at 2:16 pm
GRACIAS!!! La solucion de Marcaum54 funciono prfecto!!
fueron mi salvacion :D
November 25th, 2010 at 2:23 am
Hi
Thanks, it works excellent
Thanks for posting this
November 25th, 2010 at 2:26 pm
thanks you all worked like a charm
December 18th, 2010 at 12:32 am
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
January 15th, 2011 at 8:33 pm
Thank you so much!!! How did you find it?