ID #1045

I get Error no. 2002. Can't connect to local MySQL server through socket ...

This can occur when connecting using HTTP-tunneling to a MySQL server running on Unix/Linux platforms.

MySQL writes this about this issue.

How to cope with this would depend on which webserver and which php version is used. But here is a workaround that has worked with Apache:

if the directory /var/lib/mysql doesn't exist than create it and chown to user mysql:
"mkdir /var/lib/mysql; chown mysql /var/lib/mysql"

Then edit the /etc/my.cnf file and specify
[mysqld]
socket=/var/lib/mysql/mysql.socket
[client]
socket=/var/lib/mysql/mysql.socket


and restart the mysql server ("etc/init.d/mysql restart")

That is enough for MysSQL. However the chances are that php was compiled with a different default mysql socket location (e.g. /tmp/mysql.sock). In which case you have to edit the php.ini file and find the variable "mysql.default_socket". Set this to the above value

mysql.default_socket = /var/lib/mysql/mysql.socket

and restart Apache to re-read the php.ini file


If you don't have access to the configurations files and system command-line then you must ask your Sys Admin/support to help with this!

Tags: -

Related entries:

You can comment this FAQ

Comment of Martijn Hooimeijer:
Hi. The proposed solution may be fine if you have full control over the Mysql database. But I don't so i looked for another solution.

There is a very easy solution that worked for me: do not use localhost fot the "MySQL host address" but the actual url (e.g. database1.server.com).

I got this idea after reading the MySQL manual : "A Unix socket file is used if you don't specify a hostname or if you specify the special hostname localhost." So if you do not use 'localhost' it will not use the socket file but connect through TCP/IP, connecting through a port number. (The above error occurs when PHP tries to use the socket file to set up a connection.) It will not be as fast as using the socket file, but its good enough for me.

Hope this helps,

Martijn

Added at: 2038-01-19 04:14

Comment of Peter Laursen:
Thanks for your input. Actually I wrote something similar here: http://www.webyog.com/faq/16_66_en.html. I must be honest to say that when I put this it the FAQ i knew very little about *nix. In the meantime I got my own *nix running and that has clarified something in my head. Actually this http://www.webyog.com/faq/16_67_en.html is related too! To summarize: 1) you can use 'localhost' if the MySQL server runs on localhost and if the socket file is available (and correctly adressed by PHP). 2) You can use the name server identity of the MySQL server if you know it! 3) You can do as I originally wrote if the server configuration is under your control. Actually, I might consider rewriting these 3 FAQ items for better structure and clarity. Thanks again!
Added at: 2038-01-19 04:14