Home  >  Q&A  >  body text

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 databases are set up. I try to establish a simple connection:

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

I always get this:

Error: 2002 - No such file or Table of contents

What file or directory is it talking about?

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

Update: I discovered that the socket is located at /tmp/mysql.sock, so in php.ini I replaced all occurrences of the wrong path with that.

P粉422227023P粉422227023260 days ago379

reply all(2)I'll reply

  • P粉946336138

    P粉9463361382024-02-26 13:21:41

    I had a similar problem and was able to resolve my mysql issue by using 127.0.0.1 instead of localhost.

    This probably means something is wrong with my hosting setup, but I need this quick fix right now.

    reply
    0
  • P粉466909449

    P粉4669094492024-02-26 11:58:23

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

    Open the php.ini file and find this line:

    mysql.default_socket

    Then do it

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

    reply
    0
  • Cancelreply