Linux 无法通过 socket '/var/lib/mysql/mysql.sock' 连接到本地 MySQL 服务器 (2)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21103197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:50:52  来源:igfitidea点击:

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

mysqllinuxsocketsubuntu

提问by eclipsis

I just manually installed a new plugin on my WordPress site, created a table in MySQL for the plugin's entries, and when I try to open the page that uses the plugin, I receive this error:

我刚刚在我的 WordPress 站点上手动安装了一个新插件,在 MySQL 中为插件条目创建了一个表,当我尝试打开使用该插件的页面时,我收到此错误:

DataTables warning (table id = 'to-do_list'): An error occurred while 
connecting to the database 'wordpress_clouse'. The error reported by the 
server was: SQLSTATE[HY000] [2002] Can't connect to local MySQL server 
through socket '/var/lib/mysql/mysql.sock' (2)

This is the MySQL info listed in my config file (which I believe is correct):

这是我的配置文件中列出的 MySQL 信息(我认为是正确的):

"type" => "Mysql",
"user" => "root",
"pass" => "******",
"host" => "localhost",
"port" => "",
"db"   => "******"

This is the main contents of the /etc/my.cnf file:

这是/etc/my.cnf文件的主要内容:

# The following options will be passed to all MySQL clients
[client]
#port       = 3306
socket      = /var/run/mysqld/mysqld.sock

# The MySQL server
[mysqld]
#port       = 3306
socket      = /var/run/mysqld/mysqld.sock
log_error   = mysqld.err

skip-networking

I've looked at answers to similar errors but none of the solutions worked for me. Any help would be appreciated.

我查看了类似错误的答案,但没有一个解决方案对我有用。任何帮助,将不胜感激。

采纳答案by eclipsis

Here's what helped me solve this problem, which was that the actual file directory on my server was /var/lib/mysql/mysql.sock, not /var/lib/mysqld/mysqld.sock:

这是帮助我解决这个问题的原因,即我服务器上的实际文件目录是/var/lib/mysql/mysql.sock,而不是 /var/lib/mysqld/mysqld.sock :

  1. Open your my.cnffile
  2. Add the following to your [client]section (if you don't have a [client]section, then add it). It should look like this:
  1. 打开你的my.cnf文件
  2. 将以下内容添加到您的[client]部分(如果您没有[client]部分,则添加它)。它应该是这样的:

[client]

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

[客户]

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

The error should be gone. Many thanks to @alvits for pointing me in the right direction.

错误应该消失了。非常感谢@alvits 为我指明了正确的方向。

回答by Sai Pardhu

Encountered the same error and found it was because I haven't started the service yet.

遇到同样的错误,发现是因为我还没有启动服务。

Executed the command systemctl start mariadband then connected to the MariaDB server using the command mysqland it worked.

执行该命令systemctl start mariadb,然后使用该命令连接到 MariaDB 服务器mysql并且它工作正常。

Hope it helps

希望能帮助到你