mysql_connect 不适用于我的 Windows 机器上的 localhost
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4015253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mysql_connect doesn't work with localhost on my Windows machine
提问by Znarkus
This does not work with my PHP 5.3.3 installation on WAMP / Windows 7:
这不适用于我在 WAMP/Windows 7 上的 PHP 5.3.3 安装:
mysql_connect('localhost', 'root', '');
After Maximum execution time
, this error is presented:
之后Maximum execution time
,出现此错误:
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.
警告:mysql_connect() [function.mysql-connect]: [2002] 连接尝试失败,因为连接方没有(尝试通过 tcp://localhost:3306 连接)
警告:mysql_connect() [function.mysql-connect]:连接尝试失败,因为连接方在一段时间后没有正确响应,或者因为连接的主机没有响应而建立连接失败。
This works:
这有效:
mysql_connect('127.0.0.1', 'root', '');
I'm wondering if it has something to do with this, from the PHP docs:
我想知道它是否与此有关,来自PHP 文档:
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.
每当您指定“localhost”或“localhost:port”作为服务器时,MySQL 客户端库将覆盖它并尝试连接到本地套接字(Windows 上的命名管道)。如果要使用 TCP/IP,请使用“127.0.0.1”而不是“localhost”。如果 MySQL 客户端库尝试连接到错误的本地套接字,您应该在 PHP 配置中设置正确的路径,并将服务器字段留空。
This is from my php.ini:
这是来自我的 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
.
更新:我正在使用 MySQL 5.1.36 的东西,否则我可以连接到它。忘了提,但这适用于 PHP 5.3.0 和 5.2.11。当然我可以localhost
改为127.0.0.1
,但我想知道为什么我不能使用localhost
.
Update 2: Adding (uncommenting) 127.0.0.1 localhost
to the hosts file made it work. But why is this necessary?
更新 2:添加(取消注释)127.0.0.1 localhost
到主机文件使其工作。但为什么这是必要的?
采纳答案by Znarkus
Filed a bugwith PHP. The problem is with MySQL, and has to do with it not correctly connecting to all possible IP addresses for a domain.
提交了一个PHP错误。问题出在 MySQL 上,与它没有正确连接到域的所有可能的 IP 地址有关。
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.
回答by Wouter Dorgelo
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.
您是在通过 PHP 连接时遇到问题,还是无法从命令行建立到 MySQL 的连接?如果您根本无法连接,请验证服务正在运行并在端口 3306 上侦听。
You can also try to change localhost to: 127.0.0.1:3306
也可以尝试将localhost改为:127.0.0.1:3306
Or
或者
$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['host'] = 'localhost';
to
到
$cfg['Servers'][$i]['host'] = '127.0.0.1';
$cfg['Servers'][$i]['host'] = '127.0.0.1';
回答by MatTheCat
Try adding 127.0.0.1 localhost
to C:\Windows\System32\drivers\etc\hosts
?
尝试添加127.0.0.1 localhost
到C:\Windows\System32\drivers\etc\hosts
?
回答by joe
This is actually a bug in the php_mysql driver. See http://bugs.php.net/bug.php?id=48082
这实际上是 php_mysql 驱动程序中的一个错误。见http://bugs.php.net/bug.php?id=48082