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

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

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


当前回答

确保您的mysql服务正在运行

service mysqld start

然后,试试下面的方法:

(如果你没有设置mysql密码)

mysql -u root

如果您已经设置了密码

mysql -u root -p

其他回答

您正在连接到“localhost”还是“127.0.0.1”?我注意到,当您连接到“localhost”时使用套接字连接器,但当您连接到“127.0.0.1”时使用TCP/IP连接器。如果套接字连接器未启用/正在工作,则可以尝试使用“127.0.0.1”。

在我的情况下,我导入了一个新的数据库,在那之后我无法再次连接。最后我意识到这是一个空间问题。

你可以删除最后一个数据库,然后扩展硬盘或者像我做的那样,恢复虚拟机的快照。

以防有人觉得这有用

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

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

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