Search code examples
phpmysqlconnection

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:

<?php
    $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.


Solution

  • 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:

    mysql.default_socket
    

    And make it

    mysql.default_socket = /path/to/mysql.sock