MySQL working with 127.0.0.1 but not working with

2020-02-10 15:32发布

I have a strange problem in php MySQL:

php connects with 127.0.0.1 but not with localhost.

How to solve this issue?

9条回答
叛逆
2楼-- · 2020-02-10 16:16

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

查看更多
我想做一个坏孩纸
3楼-- · 2020-02-10 16:17

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.

查看更多
萌系小妹纸
4楼-- · 2020-02-10 16:25

Try:

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

查看更多
狗以群分
5楼-- · 2020-02-10 16:26

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

查看更多
看我几分像从前
6楼-- · 2020-02-10 16:27

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

查看更多
贼婆χ
7楼-- · 2020-02-10 16:32

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/

查看更多
登录 后发表回答