SQLSTATE[HY000] [2002] Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)

Posted June 25th, 2011 in Zanimivosti by Metod

When you run into this error, a few solutions apply.

First one is to check where mysql.sock or mysqld.sock actually resides and change values in php.ini appropriately.

mysqli.default_socket = /var/run/mysqld/mysqld.sock

You can also make a symbolic link to the correct location.

# ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock

For me, the above solution did not work. All I got was the change in error from (2) to (13). Turns out the solution is quite interesting. Instead of using ‘localhost’ as host for DB, use 127.0.0.1 – it magically begins to work again.