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:25

Please ensure you have installed MySQL server correctly, I met this error many times and I think it's complicated to debug from the socket, I mean it might be easier to reinstall it.

If you are using CentOS 7, here is the correct way to install it:

First of all, add the mysql community source
yum install http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

Then you can install it by yum install mysql-community-server

Start it with systemctl: systemctl start mysqld

查看更多
余欢
3楼-- · 2018-12-31 18:26

If your mysql was previously working and has stopped suddenly just "reboot" the server.

Was facing this issue on my CentOS VPS.->

Was constantly getting

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

Tried all techniques, finally restarting the server fixed the issues ->

shutdown -r now

Hope this helps !!

查看更多
不再属于我。
4楼-- · 2018-12-31 18:27

It worked for me with the following changes

Whatever path for socket is mentioned in [mysqld] and same in [client] in my.cnf and restart mysql

[mysqld] socket=/var/lib/mysql/mysql.sock

[client] socket=/var/lib/mysql/mysql.sock

查看更多
墨雨无痕
5楼-- · 2018-12-31 18:28

Check if your mysqld service is running or not, if not run, start the service.

If your problem isn't solved, look for /etc/my.cnf and modify as following, where you see a line starting with socket. Take a backup of that file before doing this update.

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

Change to

socket=/opt/lampp/var/mysql/mysql.sock -u root
查看更多
谁念西风独自凉
6楼-- · 2018-12-31 18:29

First enter "service mysqld start" and login

查看更多
听够珍惜
7楼-- · 2018-12-31 18:29

My problem was that I installed mysql successfully and it worked fine.

But one day, the same error occurred.

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

And no mysql.sock file existed.

This sollution solved my problem and mysql was up and running again:

Log in as root:

sudo su -

Run:

systemctl stop mysqld.service
systemctl start mysqld.service
systemctl enable mysqld.service

Test as root:

mysql -u root -p

mysql should now be up and running.

I hope this can help someone else as well.

查看更多
登录 后发表回答