mysql_connect 无法在我的 Windows 机器上使用 localhost

发布于 2024-09-29 16:58:13 字数 1310 浏览 0 评论 0原文

这不适用于我在 WAMP / Windows 7 上安装的 PHP 5.3.3:

mysql_connect('localhost', 'root', '');

最大执行时间之后,出现此错误:

警告:mysql_connect() [function.mysql-connect]: [2002] 连接尝试失败,因为连接方没有(尝试通过 tcp://localhost:3306 连接)

警告:mysql_connect() [function.mysql-connect]:连接尝试失败,因为连接方在一段时间后没有正确响应,或者由于连接的主机未能响应而建立的连接失败。

这有效:

mysql_connect('127.0.0.1', 'root', '');

我想知道它是否与此有关,来自 PHP 文档

每当您指定“localhost”或“localhost:port”作为服务器时,MySQL 客户端库都会覆盖它并尝试连接到本地套接字(Windows 上的命名管道)。如果要使用 TCP/IP,请使用“127.0.0.1”而不是“localhost”。如果 MySQL 客户端库尝试连接到错误的本地套接字,您应该在 PHP 配置中设置正确的路径,并将服务器字段留空。

这是来自我的 php.ini:

; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
; http://php.net/mysql.default-socket
mysql.default_socket =

这是一个错误,还是发生了什么?


更新:我正在使用MySQL 5.1.36,否则我可以连接到它。忘了提及,但这适用于 PHP 5.3.0 和 5.2.11。当然,我可以将 localhost 更改为 127.0.0.1,但我想知道为什么我不能使用 localhost


更新 2:向主机文件添加(取消注释)127.0.0.1 localhost 使其正常工作。但为什么这是必要的呢?

This does not work with my PHP 5.3.3 installation on WAMP / Windows 7:

mysql_connect('localhost', 'root', '');

After Maximum execution time, this error is presented:

Warning: mysql_connect() [function.mysql-connect]: [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://localhost:3306)

Warning: mysql_connect() [function.mysql-connect]: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

This works:

mysql_connect('127.0.0.1', 'root', '');

I'm wondering if it has something to do with this, from the PHP docs:

Whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket (named pipe on Windows). If you want to use TCP/IP, use "127.0.0.1" instead of "localhost". If the MySQL client library tries to connect to the wrong local socket, you should set the correct path as in your PHP configuration and leave the server field blank.

This is from my php.ini:

; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
; http://php.net/mysql.default-socket
mysql.default_socket =

Is this a bug, or what is going on?


Update: I'm using MySQL 5.1.36 something, and I can connect to it otherwise. Forgot to mention, but this works with PHP 5.3.0 and 5.2.11. Ofcourse I can just change localhost to 127.0.0.1, but I want to know why I can't use localhost.


Update 2: Adding (uncommenting) 127.0.0.1 localhost to the hosts file made it work. But why is this necessary?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青丝拂面 2024-10-06 16:58:13

尝试将 127.0.0.1 localhost 添加到 C:\Windows\System32\drivers\etc\hosts 吗?

Try adding 127.0.0.1 localhost to C:\Windows\System32\drivers\etc\hosts?

千年*琉璃梦 2024-10-06 16:58:13

您是否只是在通过 PHP 连接时遇到问题,或者无法从命令行建立与 MySQL 的连接?如果根本无法连接,请验证服务是否正在运行并正在侦听端口 3306。

您还可以尝试将 localhost 更改为: 127.0.0.1:3306

$cfg['Servers'][$i]['host'] = '本地主机';

$cfg['服务器'][$i]['主机'] = '127.0.0.1';

Are you just having trouble connecting through PHP or can you not establish a connection to MySQL from the command line as well? If you cannot connect at all, verify the service is running and listening on port 3306.

You can also try to change localhost to: 127.0.0.1:3306

Or

$cfg['Servers'][$i]['host'] = 'localhost';

to

$cfg['Servers'][$i]['host'] = '127.0.0.1';

寄居者 2024-10-06 16:58:13

提交了 PHP 一个错误。问题出在 MySQL 上,并且与它有关 未正确连接到域的所有可能的 IP 地址

解决方案?如果不是绝对必要,请勿使用主机名连接到数据库。这也可能对性能产生积极的性能影响< /a>.

Filed a bug with PHP. The problem is with MySQL, and has to do with it not correctly connecting to all possible IP addresses for a domain.

Solution? Don't use host names to connect to databases, if it isn't absolutely necessary. This might also have a positive impact on performance.

递刀给你 2024-10-06 16:58:13

这实际上是 php_mysql 驱动程序中的一个错误。请参阅 http://bugs.php.net/bug.php?id=48082

This is actually a bug in the php_mysql driver. See http://bugs.php.net/bug.php?id=48082

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文