“PDO exception: php_network_getaddresses: getaddrinfo failed” after changing DNS resolvers, with DB running on AWS

非 Y 不嫁゛ 提交于 2019-12-22 04:56:11

问题


In our Laravel 5.2 app, we have a DB setup running on AWS, using multiple read servers (and multiple DB connections).

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:

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

The code at Connector.php:55 is simply the PDO constructor:

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 .env config for the AWS (with sesnsitive parts changed):

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.

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.

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?


回答1:


This is a bug in libc, according to:

https://bugs.launchpad.net/ubuntu/+source/glibc/+bug/1674733

php_network_getaddresses: getaddrinfo failed: Name or service not known ubuntu




回答2:


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:

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:

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:

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 $3}'

If you're on Ubuntu 16.X, you should see output similar to the following:

# sudo dpkg -l | grep "GNU C Library" | awk '{print $3}'
2.23-0ubuntu7
2.23-0ubuntu7
2.23-0ubuntu7

If you're on Ubuntu 14.X, you should see output similar to the following:

# sudo dpkg -l | grep "GNU C Library" | awk '{print $3}'
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.




回答3:


I also ran into this problem after rebuilding my instance and I tried to restore a database snapshot. And restarting the App Servers solved the issue for me. Worked like a charm! :)



来源:https://stackoverflow.com/questions/42925765/pdo-exception-php-network-getaddresses-getaddrinfo-failed-after-changing-dns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!