macos 从 PHP (5.3.6-8) 访问 MySQL (5.5.17) 时出现“getaddrinfo failed...”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8010927/
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
"getaddrinfo failed..." error when accessing MySQL (5.5.17) from PHP (5.3.6-8)
提问by runamokisay
I'm a total beginner, and when attempting to connect to my first real MySQL database in the manner that most tutorials use:
我是一个完全的初学者,在尝试以大多数教程使用的方式连接到我的第一个真正的 MySQL 数据库时:
$connect = mysql_connect("localhost","user","pass")
I received the following error:
我收到以下错误:
php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known
I searched the internets but people only seemed to run into this problem when trying to access the contents of a remote file using fopen() or some such thing. I found their solutions to be inapplicable here. I assume it has something to do with an improper configuration?
我搜索了互联网,但人们似乎只有在尝试使用 fopen() 或类似的东西访问远程文件的内容时才会遇到这个问题。我发现他们的解决方案在这里不适用。我认为这与配置不当有关?
For context, I'm running OS X and installed MySQL from the official package installer. I WAS using OS X's version of PHP (5.3.6) but in exasperation, I installed the package from http://php-osx.liip.ch/. It didn't help, but the error message was formatted much more nicely!
对于上下文,我正在运行 OS X 并从官方软件包安装程序安装 MySQL。我使用的是 OS X 版本的 PHP (5.3.6),但令人恼火的是,我从http://php-osx.liip.ch/安装了该软件包。它没有帮助,但错误消息的格式要好得多!
EDIT: I do have a workaround solution, but due to being a new user, I can't post it for 8 hours. Will post as soon as I can. Thanks!
编辑:我确实有一个解决方法,但由于是新用户,我无法在 8 小时内发布它。我会尽快发布。谢谢!
回答by runamokisay
I found a solution, but I don't quite know why it works. Hopefully it helps someone though:
我找到了一个解决方案,但我不太清楚它为什么有效。希望它可以帮助某人:
First I had to edit php.ini to change where it was looking for MySQL's sock file ("/var/mysql/mysql.sock") to where the OS X package installer actually put it ("/tmp/mysql.sock"). This didn't work but it was probably a good thing anyway.
首先,我必须编辑 php.ini 以将查找 MySQL 的 sock 文件(“/var/mysql/mysql.sock”)的位置更改为 OS X 软件包安装程序实际放置它的位置(“/tmp/mysql.sock”) . 这不起作用,但无论如何这可能是一件好事。
Then, instead of connecting to MySQL via "localhost", I tried connecting directly to that sock file. Like:
然后,我没有通过“localhost”连接到 MySQL,而是尝试直接连接到该 sock 文件。喜欢:
$connect = mysql_connect(":tmp/mysql.sock","username","pass")
The "localhost" is implied in the above example. It worked! I don't know why, and it seems like I shouldn't have to specify the sock file every time I want to connect, but it does work.
上面的例子中隐含了“localhost”。有效!我不知道为什么,而且似乎每次我想连接时都不必指定 sock 文件,但它确实有效。
UPDATE: After restarting everything (Apache, mysqld, the computer itself) I no longer have to specify the sock file. I can just use "localhost." I suppose this means the problem was just that line in php.ini, but it seems like this error message should be more common if that was the case, since lots of OS X folks use that MySQL package installer. Anyways, hope this helps.
更新:重新启动所有内容(Apache、mysqld、计算机本身)后,我不再需要指定 sock 文件。我可以只使用“本地主机”。我想这意味着问题只是 php.ini 中的那一行,但如果是这种情况,这个错误消息似乎应该更常见,因为很多 OS X 人使用那个 MySQL 包安装程序。无论如何,希望这会有所帮助。
回答by Sotiris K.
Use only the snippet below and refresh your post with the results. The error that you're getting seems odd and should not happen with just a mysql connect.
仅使用下面的代码段并使用结果刷新您的帖子。您遇到的错误似乎很奇怪,仅使用 mysql 连接不应该发生。
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
回答by mickyginger
I also ran into this problem. I discovered that I had a trailing slash on localhost, which presumably PHP interpreted as a socket address.
我也遇到了这个问题。我发现我在 localhost 上有一个尾部斜杠,大概是 PHP 解释为套接字地址。
$db = mysql_connect('localhost/', 'user', 'pass'); //bad
$db = mysql_connect('localhost', 'user', 'pass'); //good :)
回答by Natalia Ivanova
I had the same error when I misspelled a word 'localhost' here:
当我在这里拼错了一个单词“localhost”时,我遇到了同样的错误:
$db = mysqli_connect('localhost','root',' ', 'mydb');