Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 584
mk12 Avatar asked Nov 04 '09 21:11

mk12


People also ask

Can't connect to No such file or directory MySQL?

The error 2002 means that MySQL can't connect to local database server through the socket file (e.g. /tmp/mysql. sock ). then double check that your application uses the right Unix socket file or connect through the TCP/IP port instead. and make sure that file exists.

Why MySQL database is not connecting?

normally means that there is no MySQL server running on the system or that you are using an incorrect Unix socket file name or TCP/IP port number when trying to connect to the server. You should also check that the TCP/IP port you are using has not been blocked by a firewall or port blocking service.

How do I fix the lost connection to MySQL server during query?

The error above commonly happens when you run a long or complex MySQL query that runs for more than a few seconds. To fix the error, you may need to change the timeout-related global settings in your MySQL database server.


2 Answers

I had a similar problem and was able to solve it by addressing my mysql with 127.0.0.1 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.

like image 199
bryan kennedy Avatar answered Nov 12 '22 05:11

bryan kennedy


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 
like image 37
Alec Gorge Avatar answered Nov 12 '22 06:11

Alec Gorge