Mysql – “Can’t connect to local MySQL server through socket” and deleted socket

MySQLservicesocket

It's a common error, but all the answers don't solve my problem :

When I start mysql, I have :

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

So after reading some answers, I change it in my.cnf to /var/lib/mysql/mysql.sock and also /tmp/mysql.sock but I have the same issue each time.

In desperation, I remove mysql.sock in /var/run/mysqld, but it's not created when I do mysqld start or service mysql start (after stopping each).

I'm on Ubuntu 10.04, with a LAMP server.

Thanks.

Best Answer

Check the permissions on those directories or that the file was in use.

Here's a permission scenario. Suppose that you install MySQL and are playing around as root, you manage to start the database and accidentally get it to write in the /tmp directory a named socket with root ownership. You kill the process in a violent way and cleanup doesn't happen.

Then, later, you try as a regular user to get MySQL to start, which may want to use a special mysql user the distro put on your system. Because the named socket exists, it fails. Because it doesn't have permission to zap root's cruft, that fails.

Then you reboot, which can wipe all of /tmp before the system starts. Now there's nothing there, the MySQL server comes up, no conflicts, and magically it all appears to work.

Not saying this is what happened, but most of my woes when something happened like this turned out that some directory didn't have write permission along the way or the file was already there and couldn't be deleted by a mortal account.

Glad to hear you got it fixed, though.