MySQL connection not working: 2002 No such file or directory

Viewed 376432

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.

22 Answers

Restarting the mysql server might help. In my case, restarting the server saved a lot of time.

service mysql restart

P.S.- use sudo service mysql restart for non-root user.

I encountered this problem too, then i modified 'localhost' to '127.0.0.1',it works.

May be I am late to answer this, but what solved my problem was to install the mysql-server

sudo apt-get install mysql-server

after spending more than 5 hours I found this solution which helped me to proceed. I hope this would help someone if the top answers won't help them

Digital Ocean MySql 2002-no-such-file-or-directory

Add this end of file /etc/mysql/my.cnf

[mysqld]
innodb_force_recovery = 1

Restart MySql

service mysql restart

First check MySQL server is running or not. if running then check socket path by login to MySQL through command line.

mysql -uUSER -pPASSWORD

then

show variables like 'socket';

You'll find path of mysql socket which you can use further in connection string like below:

$conn = mysqli_connect('localhost', 'USER', 'PASSWORD', 'path of socket file');

If MySQL is not running. Then Please share error logs which you are getting to troubleshoot further.

Im using PHP-FPM or multiple php version in my server. On my case i update mysqli value since there is not mysql default socket parameter :

mysqli.default_socket

to :

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

thanks to @Alec Gorge

I had the same problem. My socket was eventually found in /tmp/mysql.sock. Then I added that path to php.ini. I found the socket there from checking the page "Server Status" in MySQL Workbench. If your socket isn't in /tmp/mysql.sock then maybe MySQL Workbench could tell you where it is? (Granted you use MySQL Workbench...)

enable and start mariadb service

sudo systemctl enable mariadb.service

sudo systemctl start mariadb.service

Related