Can't connect to local MySQL server through so

2018-12-31 17:49发布

I am getting the following error when I try to connect to mysql:

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

Is there a solution for this error? What might be the reason behind it?

30条回答
牵手、夕阳
2楼-- · 2018-12-31 18:21

Just edit /etc/my.cnf Add following lines to my.cnf

[mysqld]

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

[client]

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

Restart mysql and connect again

mysql -u user -p password database -h host;

查看更多
呛了眼睛熬了心
3楼-- · 2018-12-31 18:21

Please check whether another mysql service is running.

查看更多
余欢
4楼-- · 2018-12-31 18:21

Try first 2, 3 solutions. Error is stil popup & If you can not find /var/lib/mysql/mysql.sock

find /var/ -name mysql.sock

Check the space available in /var/

df

If the directory is full remove some unusefull files/directories

rm /var/cache/*

Probably your issue will sorted now.

查看更多
余生请多指教
5楼-- · 2018-12-31 18:23

Are you connecting to "localhost" or "127.0.0.1" ? I noticed that when you connect to "localhost" the socket connector is used, but when you connect to "127.0.0.1" the TCP/IP connector is used. You could try using "127.0.0.1" if the socket connector is not enabled/working.

查看更多
只若初见
6楼-- · 2018-12-31 18:24

The MySQL server is not running, or that is not the location of its socket file (check my.cnf).

查看更多
心情的温度
7楼-- · 2018-12-31 18:24

Here's what worked for me:

ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
service mysqld restart
查看更多
登录 后发表回答