My (SQL) WorkLog: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

Tuesday, July 28, 2009

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

Platform: Linux/Unix

If you come across below errors when trying to connect MySQL then there might be couple of reasons behind it.




-bash-3.2$ mysql --user=root --password
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

One of the reason could be that MySQL server isn't running. So how do you check whether or not MySQL is running? try running below shell command and see what it reports


-bash-3.2$ ps -e|grep mysqld
30878 ? 00:00:00 mysqld_safe
30939 ? 00:58:06 mysqld
-bash-3.2$



If you see at least 2 process are running/listed from the above command then it is confirmed that MySQL is running.

What if you don't see any process(as shown below)? then the MySQL services is not at all running then try to start it and watch for errors(you can check error log for same) and resolve them before making an attempt to start.
-bash-3.2$ ps -e|grep mysqld
-bash-3.2$

Starting MySQL(easiest way :-))

/etc/init.d/mysql start
If there are no errors, re-enter the process status(ps) command once again & if the service is now running, then try connecting to MySQL and If still you are receiving the error then another reason(wasn't first one) could be if the location of the Unix socket has been changed or is different from the default location.

It might be that the location of the socket file has been changed so first one has to check the MyQL config file (/etc/my.cnf) and locate the the value for the socket parameter(you may see two socket details - for client and mysqld groups).
-bash-3.2$ more /etc/my.cnf |grep socket
socket=/var/lib/mysql/mysql.sock
-bash-3.2$

It could be that the value for the --socket option is set at the command-line when MySQL is started is different than what the client mysql is expecting in that case you have to either change the socket file path in the config file or you will have to supply the socket parameter with its path for the the client
-bash-3.2$ mysql --user=root --password --socket=/var/lib/mysql/mysql.sock
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 421830
Server version: 5.0.45 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

12 comments:

Anonymous said...

no... Again getting the same error. !!??

Anonymous said...

i have tried all above steps but finally got nothing still same error

Umesh Shastry said...

Could you post the exact error?

Unknown said...

Greetings - I'm in the same boat - still getting the error!!

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

Jai said...

Yes this worked well for me...

Thank you very much but it could be better you must run mysqld_safe before you reset pwd...

Anonymous said...

Me too!!

# mysql -u root -p --socket=/var/lib/mysql/mysql.sock
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

Anonymous said...

check your error log

tail -f /usr/local/mysql-5.5.12-osx10.5-x86/data/myhome101.err

on mac os it was port already in use3306, I changed it to 3305 in /etc/my.cnf file
This worked for me!!

Tarzaan said...

Hi..I too getting the same error, but my case is strange.
root@subash-desktop:/etc# find / -type f -name "*.sock" -print
this is not giving anything. That means I don't have socket file, and in my my.cnf file, I got the line,

root@subash-desktop:/etc# more /etc/my.cnf |grep socket
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.
socket = /var/run/mysqld/mysqld.sock
socket = /var/run/mysqld/mysqld.sock
socket = /var/run/mysqld/mysqld.sock


I don't know what I will do. From where I will find the socket. And in /etc/init.d/ I don't have any file mysql.

Please HELP !!!

Avadhesh Kumar said...

Hi Umesh,

Please look into on priority as i am posting all results
bash-3.00$ mysql -u<> -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

3566 ? 00:00:00 mysqld_safe
3612 ? 01:07:37 mysqld
Still getting same error but socket file donoe not exist in the directory

SAI KRISHNA said...

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
[root@dhcppc1 ~]# service mysqld start
MySQL Daemon failed to start.
Starting mysqld: [FAILED]
[root@dhcppc1 ~]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
[root@dhcppc1 ~]#

SAI KRISHNA said...

Hi Friend,
Could you please help me out ..i am getting problem with mysql ..i tried above procedure ..but not resolved...

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
[root@dhcppc1 ~]# service mysqld start
MySQL Daemon failed to start.
Starting mysqld: [FAILED]
[root@dhcppc1 ~]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
[root@dhcppc1 ~]#

Anonymous said...

Thanks, excellent solution for me :)

Post a Comment