Asked  1 Year ago    Answers:  5   Viewed   19 times

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?

 Answers

3

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

Saturday, May 29, 2021
 
5

"mysql:host='localhost';dbname='data';charset=utf8"

Your DSN format is wrong, it shouldn't have those quotes in there. This is the right format

 //$con=new PDO($dsn, $user, $password);
 $con=new PDO('mysql:dbname=testdb;host=127.0.0.1','root',''); 

See Manual

Thursday, April 1, 2021
 
2

If you are running SELinux on your centos EC2, try switching off SELinux and test your application again. If it's SELinux that's causing this problem, it might well be because of the policy for external connection to database. You should switch SELinux back ON and enable the policy with this command.

sudo setsebool -P httpd_can_network_connect_db=1
Friday, August 13, 2021
 
Cilvic
 
3

To install nodejs using yum (assuming you're using the default Amazon Linux)

https://nodejs.org/en/download/package-manager/#enterprise-linux-and-fedora

curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
yum -y install nodejs

Now to execute this on your instances, you need to add the required commands to a config file inside the .ebextensions dir, something like: .ebextensions/01_install_dependencies.config

File contents:

commands:
  01_download_nodejs:
    command: curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
  02_install_nodejs:
    command: yum -y install nodejs
Wednesday, September 29, 2021
 
Jamie
 
2

unless you need a dedicated instance just for your cakephp application you may be better off using a host that provides a full lamp hosting stack on a shared server.

with aws you would need to create an instance, configure that instance, eg install mysql if that is what you want, etc.

then you pay-per-hour while it is running, so for a full year it would be like 24*365*12c = $1050 approx. (you will need to check the exact rates for what you want)

Saturday, October 16, 2021
 
demaxSH
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :