MySQL working with 127.0.0.1 but not working with

2020-02-10 16:11发布

问题:

I have a strange problem in php MySQL:

php connects with 127.0.0.1 but not with localhost.

How to solve this issue?

回答1:

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.



回答2:

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



回答3:

For macs, here's the solution:

Connect to MySQL using localhost instead of 127.0.0.1 on a MAC. For a long while now I’ve been connecting to MySQL on my development platform with 127.0.0.1 because for some reason localhost didn’t work. Turns out it’s because 127.0.0.1 uses TCP/IP and localhost uses sockets. The php.ini file points to the wrong place for the mysql.sock so all you have to do is change it, restart apache and voila!

Open php.ini: /private/etc/php.ini
Find the following line: mysql.default_socket = /var/mysql/mysql.sock
Replace with: mysql.default_socket = /tmp/mysql.sock
Restart apache: apachectl restart

Note: Depending on your setup, you may also have to update these lines in your php.ini:

mysqli.default_socket = /tmp/mysql.sock
pdo_mysql.default_socket = /tmp/mysql.sock

Note: If you don’t have a php.ini file, you need to copy the provided default called php.ini.default

sudo cp /private/etc/php.ini.default /private/etc/php.ini

via http://madproject.com/general/connect-to-mysql-using-localhost-instead-of-127-0-0-1-on-a-mac/



回答4:

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



回答5:

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



回答6:

Try:

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



回答7:

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



回答8:

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.



回答9:

If you not on windows also test if mysql is connecting via socket, locally it normally does.

Force tcp with the mysql client

mysql -usomeone -p --protocol=TCP

vs. socket (default on linux)

mysql -usomeone -p 
mysql -usomeone -p --protocol=socket

The latter didn't work for my someone@127.0.0.1