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

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

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


当前回答

我不得不从my.cnf禁用explicit_defaults_for_timestamp。

其他回答

请确保你已经正确安装了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

我做了以下更改

在[mysqld]和my.cnf的[client]中都提到了socket的任何路径,并重新启动mysql

[mysqld] 插座= / var / lib / mysql / mysql。袜子

(客户端) 套接字= / var / lib / mysql / mysql.sock

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

先尝试2、3种解决方案。如果您无法找到/var/lib/mysql/mysql.sock .sock,则仍然弹出错误提示

find /var/ -name mysql.sock

检查/var/中的可用空间

df

如果目录已满,请删除一些无用的文件/目录

rm /var/cache/*

也许你的问题现在可以解决了。

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

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 ~ $