Wednesday, August 28, 2019

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


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:




mysql.default_socket


And make it



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

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...