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

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

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


当前回答

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

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

其他回答

当试图在SSH客户端连接mysql时遇到这个问题,发现在命令中添加套接字路径时,在套接字之间切换是必要的。

> mysql -u user -p --socket=/path/to/mysql5143.sock

我的问题是,我成功安装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现在应该启动并运行了。

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

我使用MyXQL访问Elixir中的数据库。我的实际错误是使用/tmp/mysql。当/etc/my.conf使用/var/lib/mysql/mysql.sock时。

我的解决方案是在Elixir设置中设置一个环境变量: 出口MYSQL_UNIX_PORT = / var / lib / mysql / mysql。sock,这样MyXQL就不会使用它的默认位置。

这种解决方案的原则是,您可以将MyXQL重定向到另一个位置,以避免这种类型的问题。

try

echo 0 > /selinux/enforce

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

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

以防有人觉得这有用