mysql_connect to localhost on Windows 7 and Windows Vista times out
I’ve been developing PHP websites against mysql on Windows Server 2003 and Windows Server 2008 R2 and finally decided I needed to start doing my testing and debugging on a client machine instead. My development machine runs Windows 7 so it is running the same version of IIS as my Windows Server 2008 R2 machine.
As I was getting everything setup, I installed phpMyAdmin and immediately ran into a problem. It would time out trying to connect to the database on localhost. Of course, I didn’t know that at the time as phpMyAdmin just displays a blank page. I found this article by Karina Myers that described a workaround – specify 127.0.0.1 for the host instead of local host. Specifically, change this:
$cfg[‘Servers’][$i][‘host’] = ‘localhost’;
to this:
$cfg[‘Servers’][$i][‘host’] = ‘127.0.0.1’;
Then I proceeded to install an osCommerce website and it had trouble as well. Fortunately I had Xdebug installed so I got some useful error messages. I should have guessed this before; the problem had to do with the mysql_connect() function trying to connect to localhost.
Fortunately for me, others have run into this before and the solution was posted to the documentation page for mysql_connect. Here is what you do:
- Edit the
%SystemDrive%\System32\drivers\etc\hostsfile. - Make sure the following line is not present or is commented out:
::1 localhost - Make sure the following line is present and is not commented out:
127.0.0.1 localhost
Doing this solved the problem completely for me.
Update 12/29/2009
I discovered tonight that this tip also fixes a problem I was having with the Beyond TV web admin on Windows Vista. When attempting to connect from the local machine by right-clicking on the Beyond TV tray icon and selecting Open Web Admin the browser would display an error page. After making the above changes to the hosts file, all was right with the world again.
Resources
- PHP Manual – mysql_connect
- HowTo Fix: PhpMyAdmin Displays Blank Page After Logging In by Karina Myers




January 31st, 2010 at 8:55 am
thanx for your post
we ran into the same problem on windows 7 / 64 – doing the change in hosts-file did the trick
kind regards
tom
February 22nd, 2010 at 7:13 am
I have the same problem and have taken all the steps listed here to no avail.
I have PHP version 5.2.12 and MySQL version 5.1.44.
Please help is needed urgently cause it’s driving me crazy.
April 4th, 2010 at 2:45 am
I had this exact issue. Changing the hosts file fixed it for me as well. Thanks.
April 7th, 2010 at 8:06 am
Thanks.
In case it saves anyone time, this is the bit that works so far for me on Windows 7 64-bit (after nearly three days trying):
I just uncommented the line
127.0.0.1 localhost
in my hosts file, which was completely commented to start with. Make sure you delete the initial tab on the line as well.
My file is at Windows/System32/drivers/etc/hosts
I did not need to change the phpMyAdmin config file.
(Now sitting down to work out why a PHP session that happily gets passed to Ajax on XP doesn’t on W7!)
May 10th, 2010 at 6:36 pm
Thank you David Potter. And for Ian, really thank you to you because what you said really works for me. Thanks again.
June 17th, 2010 at 5:30 am
Thank You!
I was puzzled by this for more than an hour.. :/
(adding ‘127.0.0.1 localhost’ helped)
July 14th, 2010 at 9:47 am
[...] [...]