Linux 错误:'无法通过套接字'/var/run/mysqld/mysqld.sock'(2)'无法连接到本地MySQL服务器--缺少/var/run/mysqld/mysqld.sock

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11990708/
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-06 14:05:49  来源:igfitidea点击:

error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)' -- Missing /var/run/mysqld/mysqld.sock

mysqllinuxubuntu

提问by dot

My problem started off with me not being able to log in as root any more on my mysql install. I was attempting to run mysql without passwords turned on... but whenever I ran the command

我的问题开始于我无法再以 root 身份登录我的 mysql 安装。我试图在没有打开密码的情况下运行 mysql ......但是每当我运行命令时

# mysqld_safe --skip-grant-tables &

I would never get the prompt back. I was trying to follow these instructions to recover the password.

我永远不会得到提示。我试图按照这些说明恢复密码

The screen just looks like this:

屏幕看起来像这样:

root@jj-SFF-PC:/usr/bin# mysqld_safe --skip-grant-tables
120816 11:40:53 mysqld_safe Logging to syslog.
120816 11:40:53 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

and I don't get a prompt to start typing the SQL commands to reset the password.

并且我没有收到开始输入 SQL 命令来重置密码的提示。

When I kill it by pressing CTRL+ C, I get the following message:

当我按CTRL+杀死它时C,我收到以下消息:

error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'

Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!

If I retry the command and leave it long enough, I do get the following series of messages:

如果我重试该命令并保持足够长的时间,我会收到以下一系列消息:

root@jj-SFF-PC:/run/mysqld# 120816 13:15:02 mysqld_safe Logging to syslog.
120816 13:15:02 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
120816 13:16:42 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended

[1]+  Done                    mysqld_safe --skip-grant-tables
root@jj-SFF-PC:/run/mysqld#

But then if I try to log in as root by doing:

但是,如果我尝试通过执行以下操作以 root 身份登录:

# mysql -u root

I get the following error message:

我收到以下错误消息:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

I checked and /var/run/mysqld/mysqld.sockfile doesn't not exist. The folder does, but not the file.

我检查过,/var/run/mysqld/mysqld.sock文件不存在。文件夹有,但文件没有。

Also, I dunno if this helps or not, but I ran find / -name mysqldand it came up with:

另外,我不知道这是否有帮助,但我跑了find / -name mysqld它想出了:

/var/run/mysqld - folder
/usr/sbin/mysqld - file
/run/mysqld - folder

I'm new to Linux and MySQL, so I don't know if this is normal or not. But I'm including this info just in case it helps.

我是 Linux 和 MySQL 的新手,所以我不知道这是否正常。但我将这些信息包括在内,以防万一。

I finally decided to uninstall and reinstall mysql.

我终于决定卸载并重新安装mysql。

apt-get remove mysql-server
apt-get remove mysql-client
apt-get remove mysql-common
apt-get remove phpmyadmin

After reinstalling all packages again in the same order as above, during the phpmyadmin install, I got the same error:

按照与上述相同的顺序重新安装所有软件包后,在 phpmyadmin 安装过程中,我遇到了同样的错误:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

So I tried again to uninstall/reinstall. This time, after I uninstalled the packages, I also manually renamed all mysql files and directories to mysql.badin their respective locations.

所以我再次尝试卸载/重新安装。这一次,在我卸载软件包后,我还手动将所有 mysql 文件和目录重命名为mysql.bad它们各自的位置。

/var/lib/mysql 
/var/lib/mysql/mysql
/var/log/mysql
/usr/lib/perl5/DBD/mysql
/usr/lib/perl5/auto/DBD/mysql
/usr/lib/mysql
/usr/bin/mysql
/usr/share/mysql
/usr/share/dbconfig-common/internal/mysql
/etc/init.d/mysql
/etc/apparmor.d/abstractions/mysql
/etc/mysql

Then I tried to reinstall mysql-serverand mysql-clientagain. But I've noticed that it doesn't prompt me for a password. Isn't it supposed to ask for an admin password?

然后我尝试重新安装mysql-servermysql-client再次安装。但我注意到它没有提示我输入密码。不是应该要求输入管理员密码吗?

采纳答案by Ray

To find all socket files on your system run:

要查找系统上的所有套接字文件,请运行:

sudo find / -type s

My Mysql server system had the socket open at /var/lib/mysql/mysql.sock

我的 Mysql 服务器系统在以下位置打开了套接字 /var/lib/mysql/mysql.sock

Once you find where the socket is being opened, add or edit the line to your /etc/my.cnf file with the path to the socket file:

找到打开套接字的位置后,在 /etc/my.cnf 文件中添加或编辑该行,其中包含套接字文件的路径:

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

Sometimes the system startup script that launched the command line executable specifies a flag --socket=path. This flag could override the my.cnf location, and that would result in a socket not being found where the my.cnf file indicates it should be. Then when you try to run the mysql command line client, it will read my.cnf to find the socket, but it will not find it since it deviates from where the server created one. So, Unless you care where the socket resides, just changing the my.cnf to match should work.

有时,启动命令行可执行文件的系统启动脚本会指定一个标志--socket=path。此标志可能会覆盖 my.cnf 位置,这将导致在 my.cnf 文件指示的位置找不到套接字。然后当你尝试运行 mysql 命令行客户端时,它会读取 my.cnf 来查找套接字,但它不会找到它,因为它偏离了服务器创建的位置。因此,除非您关心套接字所在的位置,否则只需将 my.cnf 更改为匹配即可。

Then, stop the mysqld process. How you do this will vary by system.

然后,停止 mysqld 进程。您如何执行此操作将因系统而异。

If you're super user in the linux system, try one of the following if you don't know the specific method your Mysql setup uses:

如果您是 linux 系统中的超级用户,如果您不知道您的 Mysql 设置使用的具体方法,请尝试以下方法之一:

  • service mysqld stop
  • /etc/init.d/mysqld stop
  • mysqladmin -u root -p shutdown
  • Some systems aren't setup to have an elegant way to stop mysql (or for some reason mysql doesn't respond) and you can force terminate mysql with either:
    • One step: pkill -9 mysqld
    • Two step (least preferred):
      • Find the process id of mysql with either pgrep mysqlor ps aux | grep mysql | grep -v grep
      • Assuming the process id is 4969terminate with kill -9 4969
  • service mysqld stop
  • /etc/init.d/mysqld stop
  • mysqladmin -u root -p shutdown
  • 某些系统没有设置为具有停止 mysql 的优雅方式(或由于某种原因 mysql 不响应),您可以使用以下任一方法强制终止 mysql:
    • 一步: pkill -9 mysqld
    • 两步(最不推荐):
      • 使用pgrep mysql或查找mysql的进程IDps aux | grep mysql | grep -v grep
      • 假设进程ID4969终止于kill -9 4969

After you do this you might want to look for a pid file in /var/run/mysqld/and delete it

执行此操作后,您可能希望在其中查找 pid 文件/var/run/mysqld/并将其删除

Make sure the permissions on your socket is such that whatever user mysqld is running as can read/write to it. An easy test is to open it up to full read/write and see if it still works:

确保您的套接字上的权限使得 mysqld 正在运行的任何用户都可以读/写它。一个简单的测试是打开它以完全读/写,看看它是否仍然有效:

chmod 777 /var/run/mysqld/mysqld.sock

If that fixes the issue, you can tailor the permissions and ownership of the socket as needed based on your security settings.

如果这解决了问题,您可以根据您的安全设置根据需要定制套接字的权限和所有权。

Also, the directory the socket resides in has to be reachable by the user running the mysqld process.

此外,运行 mysqld 进程的用户必须可以访问套接字所在的目录。

回答by Taimoor Changaiz

I faced same error and found that it was due to upgradation of packages, So after restarting my system I resolved error.

我遇到了同样的错误,发现这是由于软件包升级,所以在重新启动系统后我解决了错误。

I think due to sql libraries/ packages update that error occured, So try this if you are doing some upgrading :)

我认为由于 sql 库/包更新发生了错误,所以如果你正在做一些升级,试试这个:)

回答by Ioan Stef

The solution is way easier.

解决方案要容易得多。

  1. First, you have to locate(in Terminal with "sudo find / -type s") where your mysql.sockfile is located. In my case it was in /opt/lampp/var/mysql/mysql.sock
  2. Fire up Terminal and issue sudo Nautilus
    This starts your Files manager with super user privileges
  3. From Nautilus navigate to where your mysql.sockfile is located
  4. Right click on the file and select Make Link
  5. Rename the Link File to mysqld.sockthen Right click on the file and Cutit
  6. Go to /var/runand create a folder called mysqldand enter it
  7. Now right click and Pastethe Link File
  8. Voila! You will now have a mysqld.sockfile at /var/run/mysqld/mysqld.sock:)
  1. 首先,您必须找到(在终端中使用“sudo find / -type s”)您的mysql.sock文件所在的位置。就我而言,它在/opt/lampp/var/mysql/mysql.sock
  2. 启动终端并发出 sudo Nautilus
    这会以超级用户权限启动您的文件管理器
  3. 从 Nautilus 导航到您的mysql.sock文件所在的位置
  4. 右键单击文件并选择制作链接
  5. 将链接文件重命名为mysqld.sock然后右键单击该文件并剪切
  6. 转到/var/run并创建一个名为的文件夹mysqld并输入它
  7. 现在右键单击并粘贴链接文件
  8. 瞧!您现在将拥有一个mysqld.sock文件/var/run/mysqld/mysqld.sock:)

回答by spencer

you can find mysqld.sockin /var/run/mysqldif you have already installed mysql-serverby sudo apt-get install mysql-server

你可以找到mysqld.sock/var/run/mysqld,如果你已经安装了mysql-serversudo apt-get install mysql-server

回答by coolcool1994

Okay just copy and paste these codes: This should be done in the terminal, inside a server, when your mysql database is not properly installed, and when you are getting this error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'.

好的,只需复制并粘贴这些代码:这应该在服务器内的终端中完成,当您的 mysql 数据库未正确安装时,并且当您收到此错误时:“无法通过套接字连接到本地 MySQL 服务器”/ var/run/mysqld/mysqld.sock'(2)'。

Stop MySql

停止 MySql

sudo /etc/init.d/mysqld stop

Restart it or start it

重新启动它或启动它

sudo /etc/init.d/mysqld restart or sudo /etc/init.d/mysqld start

Make a link like this and give it to the system

制作一个这样的链接并提供给系统

ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock

Run a secure installation which guides all the process you need to do to configure mysql

运行安全安装,指导您配置 mysql 所需的所有过程

/usr/bin/mysql_secure_installation

回答by user2780494

locate the my.cnf file with the mysql config mine was under /etc/mysql/my.cnf (change the location where it says socket accordingly)

使用 mysql 配置找到 my.cnf 文件,我在 /etc/mysql/my.cnf 下(相应地更改它说 socket 的位置)

port            = 3306
socket          = /tmp/mysql.sock

Here is entries for some specific programs The following values assume you have at least 32M ram

以下是一些特定程序的条目 以下值假设您至少有 32M ram

This was formally known as [safe_mysqld]. Both versions are currently parsed.

这被正式称为 [safe_mysqld]。目前已解析两个版本。

[mysqld_safe]
socket          = /tmp/mysql.sock
nice            = 0

回答by amindri

In My case two mysqld processes were running.. killed the optional processs by using pkill -9 mysqld

在我的情况下,两个 mysqld 进程正在运行.. 使用 pkill -9 mysqld 杀死了可选进程

回答by user3002884

The answer of the user load step worked for me. Sometimes is need edit the file in /etc/mysql/my.cnfadd line to client

用户加载步骤的答案对我有用。有时需要在/etc/mysql/my.cnf客户端的添加行中编辑文件

[client]
password = your_mysql_root_password
port  = 3306
host  = 127.0.0.1
socket  = /var/lib/mysql/mysql.sock

回答by user3296580

Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

错误: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Sometimes it is due to when mysql server goes down , in live server i think you should have to restart server and also in localhost you need to restart mysql server .

有时是由于 mysql 服务器出现故障,在实时服务器中,我认为您应该重新启动服务器,并且在 localhost 中您需要重新启动 mysql 服务器。

回答by knb

If you have a lot of databases and tables on your system, and if you have innodb_file_per_tableset in my.cnf, then your mysql server might have run out of opened objects / files (or rather the descriptors for these objects) Set a new max number with

如果您的系统上有很多数据库和表,并且您innodb_file_per_table在 my.cnf 中进行了设置,那么您的 mysql 服务器可能已经用完了打开的对象/文件(或者更确切地说是这些对象的描述符)设置一个新的最大数量和

open-files-limit = 2048

and restart mysql. This approach might help when the socket is not created at all, but really this might not not be the real problem, there is an underlying problem.

并重新启动mysql。当套接字根本没有创建时,这种方法可能会有所帮助,但实际上这可能不是真正的问题,存在潜在问题。