laravel 更改 DNS 解析器后出现“PDO 异常:php_network_getaddresses:getaddrinfo 失败”,数据库在 AWS 上运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42925765/
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
"PDO exception: php_network_getaddresses: getaddrinfo failed" after changing DNS resolvers, with DB running on AWS
提问by lesssugar
In our Laravel 5.2 app, we have a DB setup running on AWS, using multiple read servers (and multiple DB connections).
在我们的 Laravel 5.2 应用程序中,我们有一个在 AWS 上运行的数据库设置,使用多个读取服务器(和多个数据库连接)。
At night, we changed the DNS resolvers for all web servers to the Google DNS (8.8.8.8 and 8.8.4.4). Then, the servers started spitting these:
晚上,我们将所有网络服务器的 DNS 解析器更改为 Google DNS(8.8.8.8 和 8.8.4.4)。然后,服务器开始吐出这些:
PDOException: Exception 'PDOException' with message 'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known' in /home/forge/studydrive.net/releases/20170320162143/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:55
PDOException: 异常 'PDOException' 带有消息 ' SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo 失败:名称或服务未知' 在 /home/forge/studydrive.net/releases/20170320162143/vendor/laravel/framework/src /Database/Connectors/Connector.php:55
The code at Connector.php:55
is simply the PDO constructor:
代码Connector.php:55
只是 PDO 构造函数:
public function createConnection($dsn, array $config, array $options)
{
$username = Arr::get($config, 'username');
$password = Arr::get($config, 'password');
try {
$pdo = new PDO($dsn, $username, $password, $options); // <== THIS ONE
} catch (Exception $e) {
$pdo = $this->tryAgainIfCausedByLostConnection(
$e, $dsn, $username, $password, $options
);
}
return $pdo;
}
The .envconfig for the AWS (with sesnsitive parts changed):
AWS的.env配置(敏感部分已更改):
DB_HOST=myapp-cluster.cluster-123abc.eu-west-2.rds.amazonaws.com
DB_HOST_WRITE=myapp-cluster.cluster-123abc.eu-west-2.rds.amazonaws.com
DB_HOST_READ=myapp-cluster.cluster-ro-123abc.eu-west-2.rds.amazonaws.com
So, it seems like PHP's PDO suddenly cannot resolve the DB_HOST
name.
所以,似乎 PHP 的 PDO 突然无法解析DB_HOST
名称。
Note, that we cannot use an IP for the DB_HOST
, as the hostname (on the AWS side) maps to 3 different database READ servers.
请注意,我们不能为 使用 IP DB_HOST
,因为主机名(在 AWS 端)映射到 3 个不同的数据库 READ 服务器。
Also, before we changed the resolvers, all was working fine.
此外,在我们更改解析器之前,一切正常。
Anyone has any idea what might be causing the PDO error, or how to resolve it?
任何人都知道什么可能导致 PDO 错误,或者如何解决它?
采纳答案by Aymen Fitati
This is a bug in libc, according to:
这是 libc 中的一个错误,根据:
https://bugs.launchpad.net/ubuntu/+source/glibc/+bug/1674733
https://bugs.launchpad.net/ubuntu/+source/glibc/+bug/1674733
php_network_getaddresses: getaddrinfo failed: Name or service not known ubuntu
php_network_getaddresses:getaddrinfo 失败:名称或服务未知 ubuntu
回答by Pablo Martinez
As of March 20th, many Ubuntu users across the web were suddenly having DNS resolution issues pop up in their applications due to a bad update pushed out by Canonical (the maintainers of Ubuntu) which breaks DNS resolution after a few hours of uptime. Here's the bug report where the issue was reported and triaged:
截至 3 月 20 日,由于 Canonical(Ubuntu 的维护者)推出的错误更新,网络上的许多 Ubuntu 用户突然在他们的应用程序中弹出 DNS 解析问题,该更新在正常运行几个小时后中断了 DNS 解析。这是报告和分类问题的错误报告:
https://bugs.launchpad.net/ubuntu/+source/eglibc/+bug/1674532
https://bugs.launchpad.net/ubuntu/+source/eglibc/+bug/1674532
This may have resulted in failures of your CMS to update/install plugins/install themes, failures to connect to external APIs, and/or error messages similar to the following:
这可能导致您的 CMS 无法更新/安装插件/安装主题,无法连接到外部 API,和/或类似于以下的错误消息:
Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: No address associated with hostname Could not resolve host: downloads.wordpress.org cURL error 6: Could not resolve host: example.com in.... Fortunately Canonical has since been able to resolve the issue, but affected systems must update to acquire the fix. To do that, you'll need DNS working, so if it's currently down please first reboot your Droplet. Once DNS resolution is working as normal, please run:
警告:file_get_contents(): php_network_getaddresses: getaddrinfo failed: No address associated with hostname解决问题,但受影响的系统必须更新以获得修复。要做到这一点,您需要 DNS 工作,所以如果它当前关闭,请首先重新启动您的 Droplet。一旦 DNS 解析正常工作,请运行:
sudo apt-get update
sudo apt-get upgrade
Then you can check your versions to ensure you've got the right ones. This can be done with:
然后您可以检查您的版本以确保您拥有正确的版本。这可以通过以下方式完成:
# sudo dpkg -l | grep "GNU C Library" | awk '{print }'
If you're on Ubuntu 16.X, you should see output similar to the following:
如果您使用的是 Ubuntu 16.X,您应该会看到类似于以下内容的输出:
# sudo dpkg -l | grep "GNU C Library" | awk '{print }'
2.23-0ubuntu7
2.23-0ubuntu7
2.23-0ubuntu7
If you're on Ubuntu 14.X, you should see output similar to the following:
如果您使用的是 Ubuntu 14.X,您应该会看到类似于以下内容的输出:
# sudo dpkg -l | grep "GNU C Library" | awk '{print }'
2.19-0ubuntu6.11
2.19-0ubuntu6.11
If you see older versions of those packages, you'll need to upgrade per the previous instructions to avoid this issue.
如果您看到这些软件包的旧版本,则需要按照之前的说明进行升级以避免出现此问题。
回答by Dexter Bengil
I also ran into this problem after rebuilding my instance and I tried to restore a database snapshot. And restarting the App Serverssolved the issue for me. Worked like a charm! :)
我在重建我的实例后也遇到了这个问题,我试图恢复一个数据库快照。和重新启动应用服务器解决了这个问题对我来说。像魅力一样工作!:)