Sabir Moglad
Sabir Moglad

Reputation: 123

Connection failed: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

I am using mysql database, I was deploying my database with some hosting company and it was working fine but they suck so I migrated to upCloud. now I am facing a problem connecting to the database and getting an error:

Connection failed: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

    $servername = 'localhost';
$username = "root";
$password = "password";
$database = 'dbname';

 try{
    $conn = new PDO("mysql:host=localhost;dbname=driver", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }

I tried changing localhost to 127.0.0.1 but no luck, I also tried manipulating single quote and double quote in strings but also it didn't work. can you advise on why I am getting this error? I am deploying this in Ubonto 16.04

Upvotes: 2

Views: 8519

Answers (1)

wiz432
wiz432

Reputation: 21

Double check your DB connection details (host, DB name, username and password).

If it still does not work, then apparently there is a bug recently discovered and reported on Ubuntu, which fixes itself if you restart your Server.

More details are listed here:

  1. Bug reported at Ubuntu: https://bugs.launchpad.net/ubuntu/+source/glibc/+bug/1674733

  2. Another StackOverflow link where this issue is reported and discussed: PDOException: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

Hope it helps :)


EDIT:

Ubuntu has apparently released a fix as well: https://www.ubuntu.com/usn/usn-3239-2/

Update your version of Ubuntu and libraries as listed at this page.

Upvotes: 2

Related Questions