MySQL connection not working: 2002 No such file or directory MySQL connection not working: 2002 No such file or directory mysql mysql

MySQL connection not working: 2002 No such file or directory


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.


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


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.errVersion: '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.sockmysqli.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!