After enabling PDO - Error 2002 getaddrinfo failed: Name or service not known

19,876

First, your connection string in your example is single quoted, so variable substituion won't work. Use double quotes if you are intending to use the contents of the variables $dbhost.

Second, you should not specify both a local socket and a host/port. Pick one or the other. I am guessing from your post the socket wasn't the original intention.

Try this:

 $dbConnection = new PDO("mysql:host=$dbhost;port=3306;dbname=$dbhost", $user, $pass);

Also, your example points the dbname to the $dbhost variable. Is this a typo?

Sockets are all 0 bytes. They are just named pipes that can be read and wrote to, but they themselves have no contents. Just verify the socket exists if you want to use it.

Good luck!

Share:
19,876
tehaaron
Author by

tehaaron

Updated on June 23, 2022

Comments

  • tehaaron
    tehaaron almost 2 years

    I recently learned about PDO so I changed my php files to fit the format and now I am getting back this response:

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

    I did not change the connection variables just the format to fit PDO. I am running a Bitnami Lampstack (5.3.16-0) and did what the documentation said as far as uncommenting the line for PDO with mysql in the php.ini file. I am wondering if it has to do with my mysql.sock file which has a size of 0.

    My PDO connection looks like this (I added the unix_socket and port pieces because they were mentioned in another question relating to this error but they didn't seem to help):

    $dbConnection = new PDO('mysql:unix_socket=/opt/lampstack-5.3.16-0/mysql/tmp/mysql.sock;host=$dbhost;port=3306;dbname=$dbname;', $user, $pass);

    apache, mysql, httpd, ect are all running fine on my server and this only came up after enabling PDO.