无法使用127.0.0.1连接到mysql [英] Cannot connect to mysql with 127.0.0.1

查看:452
本文介绍了无法使用127.0.0.1连接到mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码,我可以连接到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.

请参阅文档


在Unix上,MySQL程序特别处理主机名localhost,其方式可能与其他基于网络的程序相比有所不同。对于到localhost的连接,MySQL程序尝试使用Unix套接字文件连接到本地服务器。即使指定了--port或-P选项来指定端口号,也会发生这种情况。要确保客户端与本地服务器建立TCP / IP连接,请使用--host或-h指定主机名值127.0.0.1或本地服务器的IP地址或名称。您还可以通过使用--protocol = TCP选项明确指定连接协议(即使对于localhost)。

On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option.

如果使用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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆