问题描述
使用以下代码,我可以连接到mysql:
mysql_connect(localhost,username,);
With the following code I can connect to mysql:mysql_connect("localhost","username","");
但是如果我把 localhost
改成 127.0.0.1
,我得到以下错误:
But if I change localhost
to 127.0.0.1
I get the following error:
无法连接到'127.0.0.1'(13)上的MySQL伺服器
Can't connect to MySQL server on '127.0.0.1' (13)
它与127.0.0.1一起使用?
Why doesn't it work with 127.0.0.1?
推荐答案
localhost
使用UNIX套接字而不是TCP / IP。 127.0.0.1
没有得到特殊处理。
localhost
is special cased and uses UNIX sockets instead of TCP/IP. 127.0.0.1
doesn't get that special handling.
请参阅:
如果使用TCP / IP时它不工作,那么数据库可能不在网络上侦听。这通常是一件好事,因为它增强了安全性(不是听127.0.0.1暴露任何问题,但监听所有接口给更多的机会攻击)。
If it doesn't work when you use TCP/IP then the database probably isn't listening on the network. This is generally a good thing as it enhances security (not that listening on 127.0.0.1 exposes any problems, but listening on all interfaces gives more opportunity for attacks).
如果您确实想要允许通过网络进行连接,然后查看。
If you really want to allow connections via the network, then see skip-networking.
这篇关于无法使用127.0.0.1连接到mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!