views:

278

answers:

7

I have a strange problem in php MySQL:

php connects with 127.0.0.1 but not with localhost.

How to solve this issue?

+3  A: 

localhost must be defined in your hosts file, which it is by default on Windows and most OSs.

Check the following text file and see what's up:

C:\WINDOWS\system32\drivers\etc\hosts

Please note "hosts" is the file, no extension, it's not a folder. This file should be pretty much empty, if you see entries either your AV/spam blocker added a ton of entries that point to 127.0.0.1 or nasty malware has written garbage in there, despite, newer entries are below older ones. Localhost should be the first entry in that file.

TravisO
That hosts file may have ::1 for localhost. I usually disable anything non-IP4 related to get things to work.
Dr. Zim
+1  A: 

Try:

IE-->Tools-->Options--> Connection --> LAN Settings Check Bypass Proxy for local addresses

Yada
+2  A: 

Also have a look inside the mysql database which defines the user access privileges. You can define there from which source hosts a given user is allowed to connect. You can consult http://dev.mysql.com/doc/refman/5.1/en/connection-access.html for more details

Dominik
IIRC, I've run into this very issue before. I gave the user access via "127.0.0.1" specifically, but not via "localhost".
iKnowKungFoo
+2  A: 

Try ping localhost in a command line, it may be resolving to ::1: the IP6 equivalent to 127.0.0.1

To fix it, add (or uncomment) the line:

   127.0.0.1       localhost

in C:\WINDOWS\system32\drivers\etc\hosts

Carlos Gutiérrez
+1  A: 

I remember that being a longstanding and documented feature of Mysql on Windows. Something about the way local sockets work or something.

Nathan
A: 

Is localhost resolving to the IPv6 address, as opposed to the IPv4, and MySQL is not listening on the v4 address?

The solution is make localhost resolve to the v4 address, OR make MySQL listen on the v6 address.

bramp
+1  A: 

If MySQL connects to 127.0.0.1 it does so via TCP/IP whereas if it connects to localhost it does so via a socket.
Is PHP looking in the right place for mysql.sock? Make sure php.ini and my.conf have matching locations

meouw