-->
🏠 🔍
SHAREOLITE

How to solve ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'


In this post , we bring out few tips on how we solved MySQL database error - ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' . 

We observed this error with MySql DB connection when all MYSQL process was running normally. 

[root@shareolite ~]# mysql -u shareolite -pshareolite
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' 

However , when using with a hostname as below , command worked with no errors. 
[root@shareolite ~]# mysql -u shareolite -pshareolite -h 10.74.2.12

On further checks , we got to know file /tmp.mysql.sock was missing . After restarting mysql processes - this file was created again . Now we started wondering what has caused this ?

Later , we found this is because of Redhat Linux default cron.d service which has a tmpwatch utility to delete old files from /tmp folder periodically. In our case it was every 10 days.  Below is the cron.d file which was monitoring /tmp and /var/tmp directories.

[root@shareolite ~]# cat /etc/cron.daily/tmpwatch 
#! /bin/sh 
flags=-umc 
/usr/sbin/tmpwatch "$flags" -x /tmp/.X11-unix -x /tmp/.XIM-unix \ 
        -x /tmp/.font-unix -x /tmp/.ICE-unix -x /tmp/.Test-unix \ 
        -X '/tmp/hsperfdata_*' 10d /tmp 
/usr/sbin/tmpwatch "$flags" 30d /var/tmp 
for d in /var/{cache/man,catman}/{cat?,X11R6/cat?,local/cat?}; do 
    if [ -d "$d" ]; then 
        /usr/sbin/tmpwatch "$flags" -f 30d "$d" 
    fi 
done 

After you install MySQL using standard default procedures , the socket file is usually stored under /tmp/mysql.sock. This error can be permanently avoided by adding an entry in your my.cnf configuration file as shown below , pointing the socket file to a different directory other than /tmp and /var/tmp.

socket=/var/lib/mysql/mysql.sock

Hope this helps.
Comments

–>