当我尝试连接mysql时,我得到以下错误:

无法通过套接字/var/lib/ MySQL / MySQL连接到本地MySQL服务器。袜子”(2)

有解决这个错误的方法吗?背后的原因可能是什么?


当前回答

try

echo 0 > /selinux/enforce

其他回答

MySQL服务器没有运行,或者那不是它的套接字文件的位置(检查my.cnf)。

我的问题是,我成功安装mysql,它工作得很好。

但是有一天,同样的错误发生了。

无法通过套接字/var/lib/ MySQL / MySQL连接到本地MySQL服务器。袜子”(2)

没有mysql。Sock文件存在。

这个解决方案解决了我的问题,mysql又开始运行了:

以root用户登录:

sudo su -

Run:

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

以root身份测试:

mysql -u root -p

Mysql现在应该启动并运行了。

我希望这也能帮助到其他人。

重现此错误的一种方法:如果您打算连接到外部服务器,但却连接到不存在的本地服务器:

eric@dev ~ $ mysql -u dev -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through 
socket '/var/lib/mysql/mysql.sock' (2)
eric@dev ~ $

所以你必须像这样指定主机:

eric@dev ~ $ mysql --host=yourdb.yourserver.com -u dev -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 235
Server version: 5.6.19 MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+-------------------------+
| Database                |
+-------------------------+
| information_schema      |
| mysql                   |
| performance_schema      |
+-------------------------+
3 rows in set (0.00 sec)

mysql> exit
Bye
eric@dev ~ $

请确保你已经正确安装了MySQL服务器,我遇到过这个错误很多次,我认为从套接字调试很复杂,我的意思是重新安装它可能更容易。

如果你使用的是CentOS 7,下面是正确的安装方法:

首先,添加mysql社区源代码 Yum install http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

然后你可以通过yum install mysql-community-server安装它

用systemctl启动它:systemctl Start mysqld

首先输入“service mysqld start”并登录