MySQLi and PDO result in "no such file or directory" error

  • Answered
I am trying to connect to my database using non-deprecated connection methods (e.g., using PDO or mySQLi in preference over mysql_connect).

No matter the method I use, I get a "no such file or directory" error (below). Most help on the web suggests to check the location of my mysqld.sock. I don't know how to do this without SSH...

How do I confirm the location of mySql.sock, verify that PHP is correctly configured and get mysqli or PDO to work on my shared hosting site?

Warning: mysqli_connect() [function.mysqli-connect]: [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in /path/to/my/script.php on line 16

Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2002): No such file or directory in /path/to/my/script.php on line 16
Failed to connect to MySQL: No such file or directory

Line 16 is the connection (all variables are set to correct values):
$hostname = 'localhost';
$mysqli = mysqli_connect($hostname, $username, $password, $dbname);

OR very similar error occurs from code in the try block:

dsn = 'mysql:host=localhost;port=3306;dbname=mydb'
try {
$db = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
Hello 50weasels,

I'm going to assume that based on the information above, you're making a local connection to the database (meaning it's occurring on the web server). If it's not, then you will need to make sure that you have setup the RemoteMSQL connection. Typically, any connection with the database requires a correct database name, user and password. Based on what you have given us so far, the database is name is incorrect. Databases on the shared server always start with the user name for your account. For example, if your account user name is lois5 and the main database is called loisMySQL, then the database name would be "lois5_loisMySQL" User name is also prefixed with the account user name.

Please make sure that information is correct and in place before proceeding.

If you have any further questions, please contact technical support or leave a comment at the bottom of the page.


Arnel C.