MySQL connection not working: 2002 No such file or directory


I'm trying to set up WordPress. I have Apache and MySQL running, and the accounts and database are all set up. I tried to make a simple connection:

    $conn = mysql_connect('localhost', 'USER', 'PASSWORD');
    if(!$conn) {
        echo 'Error: ' . mysql_errno() . ' - ' . mysql_error();

And I always get this:

Error: 2002 - No such file or directory

What file or directory could it be talking about?

I'm on a OS X Snow Leopard, using the built-in Apache. I installed MySQL using the x86_64 dmg.

UPDATE: I found that the socket is at /tmp/mysql.sock, so In php.ini, I replaced all occurrences of the wrong path with that.

12/21/2018 9:24:40 PM

Accepted Answer

If you use Linux: the path to the mysql.sock file is wrong. This is usually because you are using (LAMPP) XAMPP and it isn't in /tmp/mysql.sock

Open the php.ini file and find this line:


And make it

mysql.default_socket = /path/to/mysql.sock
11/4/2009 9:28:41 PM

I had a similar problem and was able to solve it by addressing my mysql with instead of localhost.

This probably means I've got something wrong in my hosts setup, but this quick fix get's me going for right now.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow