InMotion Hosting Support Center

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

Category: Databases

2012-11-19 9:43 pm EST

Hits: 3,336
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();

You must login before you can ask a follow up question.

You must login before you can submit an answer.



40,371 Points
2012-11-19 11:15 pm EST
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.

You must login before you can post a comment about this answer.

Like this Question?

Support Center Login

Our Login page has moved, Click the button below to be taken to the login page.

Social Media Login

Social Login Joomla

Need more Help?


Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail:
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!