MySQL connection not working: 2002 No such file or directory
Asked Answered
M

23

127

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.

Margarine answered 4/11, 2009 at 21:16 Comment(3)
please paste the output of /etc/init.d/mysql start if you're on a debian base distro. if failed, check /etc/my.cnf file for the correct mysql socket file path.Bobby
solved same problem on OS X with XAMPP using "127.0.0.1" in place of "localhost".Capture
Related: I've got `PDOException: SQLSTATE(2002) No such file or directoryKamal
S
106

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
Sirree answered 4/11, 2009 at 21:28 Comment(12)
if you're not sure , try /var/lib/mysql/mysql.sock as the new path to the socket.Excavate
WordPress works now! I actually remember going through a tutorial saying to do this, but read somewhere that you don't need to on Snow Leopard or something like that.Margarine
Hmm. Maybe they meant Leopard or Tiger. In any case, have fun with WordPress!Sirree
Note you may need to set mysqli.default_socket = /path/to/mysql.sock if connecting with mysqliTillage
Solved my issue. Thanks! Other issue was I thought my php.ini file was in /etc/php.ini but it was actually /usr/local/lib/php.ini (On Mac OS X with PHP 5.4 installed from source.)Cropper
The default socket location for a stock MySQL install is /tmp/mysql.sock.Anglian
sometime it will be at /var/run/mysqld/mysqld.sock (Ubuntu)Devon
"The hostname localhost has a special meaning. It is bound to the use of Unix domain sockets. It is not possible to open a TCP/IP connection using the hostname localhost you must use 127.0.0.1 instead." - php.net/manual/en/mysqli.quickstart.connections.php. So basically something is not working with connecting to the Unix domain sockets that allow localhost to work and TCP/IP is working so changing it to the TCP/IP address 127.0.0.1 will work.Zemstvo
I had to add 'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock' to config/database.phpBunyip
In Ubuntu, maybe it's here: /opt/lampp/var/mysql/mysql.sock. But it's not working for me.Adolescence
You Saves me Time to solving this type f**ky error.Common
Do docker containers also contain a mysql.sock file?Heretical
I
207

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.

Improbity answered 5/8, 2011 at 16:39 Comment(10)
you just saved me some precious time.Academic
I checked my hosts file and found a line with code127.0.0.1 localhostcode. Which makes me curious to why connecting through localhost did not work. If someone could enlighten me I would be happy.Maineetloire
This is because the mysql.default_socket in php.ini is wrong. Run "php -i | grep mysql.default_socket"Corregidor
This worked for me, I'm on el Capitan w/ mysql 5.7 installed via homebrew. I was trying a WP install and when I changed "localhost" to "127.0.0.1" it was able to connect.Administrate
"The hostname localhost has a special meaning. It is bound to the use of Unix domain sockets. It is not possible to open a TCP/IP connection using the hostname localhost you must use 127.0.0.1 instead." - php.net/manual/en/mysqli.quickstart.connections.php. So basically something is not working with connecting to the Unix domain sockets that allow localhost to work and TCP/IP is working so changing it to the TCP/IP address 127.0.0.1 will work.Zemstvo
After hours of trying to fix it... Worked like a charm! Thank you... But how do I fix my hosts (on Mac) now ?Barta
Or it means your on windows, where localhost isn't guaranteed to be a resolvable hostname...Fab
WoW! you saved me, bro!Circumvent
where to make this change, please name the file too?Fiona
This should be one of the first things to try when you got such error.Michale
S
106

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
Sirree answered 4/11, 2009 at 21:28 Comment(12)
if you're not sure , try /var/lib/mysql/mysql.sock as the new path to the socket.Excavate
WordPress works now! I actually remember going through a tutorial saying to do this, but read somewhere that you don't need to on Snow Leopard or something like that.Margarine
Hmm. Maybe they meant Leopard or Tiger. In any case, have fun with WordPress!Sirree
Note you may need to set mysqli.default_socket = /path/to/mysql.sock if connecting with mysqliTillage
Solved my issue. Thanks! Other issue was I thought my php.ini file was in /etc/php.ini but it was actually /usr/local/lib/php.ini (On Mac OS X with PHP 5.4 installed from source.)Cropper
The default socket location for a stock MySQL install is /tmp/mysql.sock.Anglian
sometime it will be at /var/run/mysqld/mysqld.sock (Ubuntu)Devon
"The hostname localhost has a special meaning. It is bound to the use of Unix domain sockets. It is not possible to open a TCP/IP connection using the hostname localhost you must use 127.0.0.1 instead." - php.net/manual/en/mysqli.quickstart.connections.php. So basically something is not working with connecting to the Unix domain sockets that allow localhost to work and TCP/IP is working so changing it to the TCP/IP address 127.0.0.1 will work.Zemstvo
I had to add 'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock' to config/database.phpBunyip
In Ubuntu, maybe it's here: /opt/lampp/var/mysql/mysql.sock. But it's not working for me.Adolescence
You Saves me Time to solving this type f**ky error.Common
Do docker containers also contain a mysql.sock file?Heretical
G
42

This is for Mac OS X with the native installation of Apache HTTP and custom installation of MySQL.

The answer is based on @alec-gorge's excellent response, but since I had to google some specific changes to have it configured in my configuration, mostly Mac OS X-specific, I thought I'd add it here for the sake of completeness.

Enable PHP5 support for Apache HTTP

Make sure the PHP5 support is enabled in /etc/apache2/httpd.conf.

Edit the file with sudo vi /etc/apache2/httpd.conf (enter the password when asked) and uncomment (remove ; from the beginning of) the line to load the php5_module module.

LoadModule php5_module libexec/apache2/libphp5.so

Start Apache HTTP with sudo apachectl start (or restart if it's already started and needs to be restarted to re-read the configuration file).

Make sure that /var/log/apache2/error_log contains a line that tells you the php5_module is enabled - you should see PHP/5.3.15 (or similar).

[notice] Apache/2.2.22 (Unix) DAV/2 PHP/5.3.15 with Suhosin-Patch configured -- resuming normal operations

Looking up Socket file's name

When MySQL is up and running (with ./bin/mysqld_safe) there should be debug lines printed out to the console that tell you where you can find the log files. Note the hostname in the file name - localhost in my case - that may be different for your configuration.

The file that comes after Logging to is important. That's where MySQL logs its work.

130309 12:17:59 mysqld_safe Logging to '/Users/jacek/apps/mysql/data/localhost.err'.
130309 12:17:59 mysqld_safe Starting mysqld daemon with databases from /Users/jacek/apps/mysql/data

Open the localhost.err file (again, yours might be named differently), i.e. tail -1 /Users/jacek/apps/mysql/data/localhost.err to find out the socket file's name - it should be the last line.

$ tail -1 /Users/jacek/apps/mysql/data/localhost.err
Version: '5.5.27'  socket: '/tmp/mysql.sock'  port: 3306  MySQL Community Server (GPL)

Note the socket: part - that's the socket file you should use in php.ini.

There's another way (some say an easier way) to determine the location of the socket's file name by logging in to MySQL and running:

show variables like '%socket%';

Configuring PHP5 with MySQL support - /etc/php.ini

Speaking of php.ini...

In /etc directory there's /etc/php.ini.default file. Copy it to /etc/php.ini.

sudo cp /etc/php.ini.default /etc/php.ini

Open /etc/php.ini and look for mysql.default_socket.

sudo vi /etc/php.ini

The default of mysql.default_socket is /var/mysql/mysql.sock. You should change it to the value you have noted earlier - it was /tmp/mysql.sock in my case.

Replace the /etc/php.ini file to reflect the socket file's name:

mysql.default_socket = /tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock

Final verification

Restart Apache HTTP.

sudo apachectl restart 

Check the logs if there are no error related to PHP5. No errors means you're done and PHP5 with MySQL should work fine. Congrats!

Gasometry answered 9/3, 2013 at 18:58 Comment(4)
This answer helpsTaxiplane
This answer is crap. It has nothing to do with the question being asked and is basically only a m(acos)amp tutorial, which - if good - is even more wasted if put here and not at a place where people who actual search for setting up an amp stack on macos are looking at. Regarding the question being asked: this answer doesnt explain shit on why the error happens and why php tries to connect via the socket instead of the hostname instead, although that is passed to mysql_connect, which is the actual problem of the original question and not a generic misconception of installing software.Waltz
@Waltz I wrote "I thought I'd add it here for the sake of completeness." and thought it'd be enough (and given the upvotes a few people liked it). I like your idea of making the answer easier to find, but dunno how. Could you share your thoughts and help me...again? Thanks!Gasometry
@JacekLaskowski in general: write a blog post and comment a link to it to the accepted/most upvoted response with the same intention you had here ("add it for the sake of completeness"). But in this very thread I don't even see a reason to post that link. The OP said that he is using macOS but this problem has nothing at all to do with any operating system but with mySQL'sway of interpreting localhost. Nobody looking for help on setting up xamp on macos will get here by googling. Hence I used the term 'crap'. This thread has nothing to do at all with macos but with mysql only (not even php).Waltz
C
18

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.

Clepsydra answered 25/4, 2018 at 7:0 Comment(1)
It's so great when the simple solution actually works.Centre
D
17

Replacing 'localhost' to '127.0.0.1' in config file (db connection) helped!

Driveway answered 9/10, 2015 at 13:58 Comment(2)
but why it should be helped?Kingsly
"Replacing 'localhost' to '127.0.0.1'" or 127.0.0.1 to actual MySQL server address i.e. u55151.mysql.someserver.eu.Slipnoose
F
10

First, ensure MySQL is running. Command: mysqld start

If you still cannot connect then: What does your /etc/my.cnf look like? (or /etc/msyql/my.cnf)

The other 2 posts are correct in that you need to check your socket because 2002 is a socket error.

A great tutorial on setting up LAMP is: http://library.linode.com/lamp-guides/centos-5.3/index-print

Florio answered 4/11, 2009 at 21:27 Comment(2)
The installer didn't put a my.cnf file, however there is a /usr/local/mysql/mysql-test/include/default_my.cnf, should I copy that as my.cnf to /etc ?Margarine
yes, but you may find that you need to modify it some latter.Florio
R
10

Expanding on Matthias D's answer here I was able to resolve this 2002 error on both MySQL and MariaDB with exact paths using these commands:

First get the actual path to the MySQL socket:

netstat -ln | grep -o -m 1 '/.*mysql.sock'

Then get the PHP path:

php -r 'echo ini_get("mysql.default_socket") . "\n";'

Using the output of these two commands, link them up:

sudo ln -s /actualpath/mysql.sock /phppath/mysql.sock

If that returns No such file or directory you just need to create the path to the PHP mysql.sock, for example if your path was /var/mysql/mysql.sock you would run:

sudo mkdir -p /var/mysql

Then try the sudo ln command again.

Reflux answered 5/1, 2016 at 22:20 Comment(0)
T
8

Not that it helps you much, but in the recent versions (and even less recent) of MySQL, error code 2002 means “Can't connect to local MySQL server through socket [name-of-socket]”, so that might tell you a bit more.

Tipsy answered 4/11, 2009 at 21:26 Comment(1)
On OSX 10.9.x with MySQL 5.5.38 installed from source, I was getting this error. This tip was correct: it a socket issue even though mysqli_connect_errno() and mysqli_connect_error()) were saying the 2002 file not found message. I was able to resolve this by editing my /etc/php.ini and setting mysql.default_socket AND mysqli.default_socket to correct socket value (since my application was using mysqli for connections)Ass
A
6

I'd check your php.ini file and verify the mysql.default_socket is set correctly and also verify that your mysqld is correctly configured with a socket file it can access. Typical default is "/tmp/mysql.sock".

Alectryomancy answered 4/11, 2009 at 21:25 Comment(1)
Ok, It says /var/mysql/mysql.sock, but that path doesn't exist.Margarine
V
6

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

Volitive answered 3/12, 2013 at 4:4 Comment(0)
F
6

in my case I have problem with mysqli_connect.
when I want to connect
mysqli_connect('localhost', 'myuser','mypassword')
mysqli_connect_error() return me this error "No such file or directory"

this worked for me mysqli_connect('localhost:3306', 'myuser','mypassword')

Fullfaced answered 28/7, 2015 at 9:55 Comment(2)
This worked well for me; it was the change I needed to load Armchair Analysis's American football statistics package into an OS X local Mysql installation. Thanks!Cleora
glad to hear that :)Fullfaced
K
4

The error 2002 means that MySQL can't connect to local database server through the socket file (e.g. /tmp/mysql.sock).

To find out where is your socket file, run:

mysql_config --socket

then double check that your application uses the right Unix socket file or connect through the TCP/IP port instead.

Then double check if your PHP has the right MySQL socket set-up:

php -i | grep mysql.default_socket

and make sure that file exists.

Test the socket:

mysql --socket=/var/mysql/mysql.sock

If the Unix socket is wrong or does not exist, you may symlink it, for example:

ln -vs /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock

or correct your configuration file (e.g. php.ini).

To test the PDO connection directly from PHP, you may run:

php -r "new PDO('mysql:host=localhost;port=3306;charset=utf8;dbname=dbname', 'root', 'root');"

Check also the configuration between Apache and CLI (command-line interface), as the configuration can be differ.

It might be that the server is running, but you are trying to connect using a TCP/IP port, named pipe, or Unix socket file different from the one on which the server is listening. To correct that you need to invoke a client program (e.g. specifying --port option) to indicate the proper port number, or the proper named pipe or Unix socket file (e.g. --socket option).

See: Troubleshooting Problems Connecting to MySQL


Other utils/commands which can help to track the problem:

  • mysql --socket=$(php -r 'echo ini_get("mysql.default_socket");')
  • netstat -ln | grep mysql
  • php -r "phpinfo();" | grep mysql
  • php -i | grep mysql
  • Use XDebug with xdebug.show_exception_trace=1 in your xdebug.ini
  • On OS X try sudo dtruss -fn mysqld, on Linux debug with strace
  • Check permissions on Unix socket: stat $(mysql_config --socket) and if you've enough free space (df -h).
  • Restart your MySQL.
  • Check net.core.somaxconn.
Kamal answered 12/11, 2015 at 23:21 Comment(0)
L
4

by using 127.0.0.1 insteady of localhost solve the problem

Lighting answered 15/11, 2022 at 6:42 Comment(0)
D
3

Make sure your local server (MAMP, XAMPP, WAMP, etc..) is running.

Dynamism answered 21/4, 2016 at 8:27 Comment(1)
In addition to this, if you are using MAMP on MacOS (obviously!) you also get this error if you access your site immediately after waking your Mac from sleep. The solution, of course, is to simply wait and try again. :-)Unbalance
F
2

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

Fiona answered 14/9, 2020 at 9:45 Comment(0)
F
1

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

Fardel answered 5/5, 2020 at 6:13 Comment(1)
The right thing is to restart the MySQL serviceBod
E
1

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.

Exciting answered 14/9, 2020 at 10:6 Comment(1)
This didn't solve my problem, but it led me to the solution: Since I run DBngin, I do not have a socket, so running mysql -u... yielded socket not found. That's when I realized I needed to use 127.0.0.1 for my connection.Gland
T
0

I had a similar problem.
Basically here the problem is there are probably two instances of mysql running.
A) One running at /etc/init.d
B) Lamp being installed at /opt/lamp
Solution :
Step 1 :- Find all mysql running instances using commnad "find / | grep mysqld"
Step 2 :- Shutdown the services running at /etc/init.d using service mysql stop
Step 3 :- Restart your Lamp services using /opt/lamp/lamp restart

You should be good to go :)

Theft answered 23/6, 2014 at 17:1 Comment(0)
P
0

On a Mac, before doing all the hard work, simply check your settings in System Preferences > MySQL. More often than not, I've experienced the team running into this problem since The MySQL Server Instance is stopped.

Click the Start MySQL Server button, and magic will happen.

Parricide answered 11/7, 2017 at 18:3 Comment(0)
D
0

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

Denten answered 21/1, 2018 at 2:46 Comment(0)
Z
0

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

Zerk answered 27/1, 2018 at 1:33 Comment(0)
A
0

I've installed MySQL using installer. In fact, there was no data directory alongside 'bin' directory.

So, I manually created the 'data' directory under "C:\Program Files\MySQL\MySQL Server 8.0". And it worked (changing the root password following steps suggested on https://dev.mysql.com/doc/mysql-windows-excerpt/5.7/en/resetting-permissions-windows.html.

Avitzur answered 21/4, 2018 at 22:14 Comment(0)
S
-2

enable and start mariadb service

sudo systemctl enable mariadb.service

sudo systemctl start mariadb.service

Sleeper answered 27/6, 2019 at 17:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.