Error 2002 (HY000) – Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)

Recently, out of the blue, I started receiving the following error message and mysql would not start properly:

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

I searched for solutions and resorted to reinstalling mysql, which worked for me. If you have similar problems, then try the following.
First, backup your databases:

sudo su
cp -r /var/lib/mysql /BackupLocation

Then remove mysql-server.

sudo apt-get remove --purge mysql-server-5.1

Reinstall mysql-server.

sudo apt-get install mysql-server-5.1

Normally doing the above should not remove your databases and their privileges. But we backup the databases just to be cautious.
Thats it. My mysql server started working again and my blog is back up now.

UPDATE 1: The error reappeared after reboot. I later found that this happened because of a full / drive (excessive logging). Upon clearing all the logs the error disappeared.

UPDATE 2: Due to requests on how I cleared the log, I am adding further details. In my case the root partition where the /var/log folder was located was about 10 Gb. This partition filled up causing the MySQL error. First thing I did was to elevate myself to root:

/var/log$ sudo su

Once you have administrative privileges, navigate to /var/log and find out which logs are huge. You can do this using the following command:

/var/log# ls -alh

In my case the following log files where huge: mail.err, mail.info, messages, syslog, etc. You can reset these logs using the following commands:

/var/log# >mail.err
/var/log# >mail.info
/var/log# >messages
/var/log# >syslog

You can do the same this for any logs that are full. You can also delete archived logs using the following command:

/var/log# rm *.gz

NOTE: Server logs may contain important information. You will lose the logs if you reset them.

In my case this freed up space and MySQL started working.Therefore, as an initial fix, I wrote a bash script to reset my logs and added it to my daily cron job list. So all my logs were reset every morning.

I wrote the following script to cleanup my /var/log files that were filling up too quickly.

rm /var/log/*.gz ;
rm /var/log/*.1 ;
rm /var/log/*.2 ;
rm /var/log/*.3 ;
>/var/log/mail.err ;
>/var/log/mail.info ;
>/var/log/mail.warn ;
>/var/log/daemon.log ;
>/var/log/kern.log ;
>/var/log/mail.log ;
>/var/log/messages ;
>/var/log/syslog ;
>/var/log/ufw.log

Save the script to some name (example: logcleanup.sh) and run it

sudo ./logcleanup.sh

However, the actual problem of logs filling up still remained. To find out how I solved the problem, follow this thread: http://ubuntuforums.org/showthread.php?t=1501194

Additionally, you could use logrotate to rotate your logs. Played with /etc/logrotate.conf and files in /etc/logrotate.d/ to modify logrotation.

Be the 1 in 200,000. Help us sustain what we do.
34 / 150 by Dec 31, 2024
Join Us (starting from just $1.67/month)

Anand

Anand is a self-learned computer enthusiast, hopeless tinkerer (if it ain't broke, fix it), a part-time blogger, and a Scientist during the day. He has been blogging since 2010 on Linux, Ubuntu, Home/Media/File Servers, Smart Home Automation, and related HOW-TOs.