ERROR 2002 (HY000): 无法通过 socket '/var/run/mysqld/mysqld.sock' 连接到本地 MySQL 服务器 (2)

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

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

mysqlphpmyadminlampforgot-passwordmysql-error-2002

提问by sushmit sarmah

I installed LAMPon Ubuntu 12.04 LTS(Precise Pangolin) and then set root password on phpMyAdmin. I forgot the password and now I am unable to login. When I try to change password through terminal I get:

我在Ubuntu 12.04 LTS(精确穿山甲)上安装了LAMP,然后在phpMyAdmin上设置了 root 密码。我忘记了密码,现在无法登录。当我尝试通过终端更改密码时,我得到:

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

ERROR 2002 (HY000): 无法通过 socket '/var/run/mysqld/mysqld.sock' 连接到本地 MySQL 服务器 (2)

How can I fix this? I am unable to open LAMP, uninstall it or reinstall it.

我怎样才能解决这个问题?我无法打开 LAMP,将其卸载或重新安装。

回答by towry

I once had this problem and solved it by installing mysql-server, so make sure that you have installed the mysql-server, not the mysql-clientor something else.

我曾经遇到过这个问题并通过安装解决了它mysql-server,所以请确保您已经安装了mysql-server,而不是mysql-client或其他东西。

That error means the file /var/run/mysqld/mysqld.sockdoesn't exists, if you didn't install mysql-server, then the file would not exist. So in that case, install it with

该错误意味着该文件/var/run/mysqld/mysqld.sock不存在,如果您没有安装mysql-server,则该文件将不存在。所以在这种情况下,安装它

sudo apt-get install mysql-server

But if the mysql-serveris already installed and is running, then you need to check the config files.

但是如果mysql-server已经安装并且正在运行,那么你需要检查配置文件。

The config files are:

配置文件是:

/etc/my.cnf
/etc/mysql/my.cnf
/var/lib/mysql/my.cnf

In /etc/my.cnf, the socket file config may be /tmp/mysql.sockand in /etc/mysql/my.cnfthe socket file config may be /var/run/mysqld/mysqld.sock. So, remove or rename /etc/mysql/my.cnf, let mysql use /etc/my.cnf, then the problem may solved.

在 中/etc/my.cnf,套接字文件配置可能是,/tmp/mysql.sock而在/etc/mysql/my.cnf套接字文件配置中可能是/var/run/mysqld/mysqld.sock. 所以,删除或者重命名/etc/mysql/my.cnf,让mysql使用/etc/my.cnf,那么问题就可以解决了。

回答by Yannick

Try this:

尝试这个:

mysql -h 127.0.0.1 -P 3306 -u root -p <database>

Also (to see if it's running):

另外(查看它是否正在运行):

telnet 127.0.0.1 3306 

Probably it is just a misconfiguration in the my.cnffile, in /etc/somewhere(depending on the Linux distribution).

可能只是my.cnf文件中的错误配置,在/etc/somewhere(取决于Linux 发行版)。

回答by davejal

I am seeing all these answers, but noneoffer the option to reset the passwordand no accepted answer. The actual question being he forgothis password, so he needs to reset, not see if it's running or not (installed or not) as most of these answers imply.

我看到了所有这些答案,但没有一个提供重置密码的选项,也没有接受的答案。实际问题是他忘记了密码,因此他需要重置,而不是像大多数这些答案所暗示的那样查看它是否正在运行(已安装或未安装)。



To reset the password

重置密码

Follow these steps (can be helpful if you really forget your password and you can try it anytime, even if you're not in the situation at the moment):

请按照以下步骤操作(如果您确实忘记了密码,这会很有帮助,您可以随时尝试,即使您目前不在这种情况下):

  1. Stop mysql

    sudo /etc/init.d/mysql stop
    

    Or for other distribution versions:

    sudo /etc/init.d/mysqld stop
    
  2. Start MySQL in safe mode

    sudo mysqld_safe --skip-grant-tables &
    
  3. Log into MySQL using root

    mysql -uroot
    
  4. Select the MySQL database to use

    use mysql;
    
  5. Reset the password

    -- MySQL version < 5.7
    update user set password=PASSWORD("mynewpassword") where User='root';
    
    -- MySQL 5.7, mysql.user table "password" field -> "authentication_string"
    
    update user set authentication_string=password('mynewpassword') where user='root';
    
  6. Flush the privileges

    flush privileges;
    
  7. Restart the server

    quit
    
  8. Stop and start the server again

    Ubuntu and Debian:

    sudo /etc/init.d/mysql stop
    ...
    sudo /etc/init.d/mysql start
    

    On CentOS, Fedora, and RHEL:

    sudo /etc/init.d/mysqld stop
    ...
    sudo /etc/init.d/mysqld start
    
  9. Login with a new password

    mysql -u root -p
    
  10. Type the new password and enjoy your server again like nothing happened

  1. 停止 mysql

    sudo /etc/init.d/mysql stop
    

    或者对于其他分发版本:

    sudo /etc/init.d/mysqld stop
    
  2. 以安全模式启动 MySQL

    sudo mysqld_safe --skip-grant-tables &
    
  3. 使用 root 登录 MySQL

    mysql -uroot
    
  4. 选择要使用的 MySQL 数据库

    use mysql;
    
  5. 重置密码

    -- MySQL version < 5.7
    update user set password=PASSWORD("mynewpassword") where User='root';
    
    -- MySQL 5.7, mysql.user table "password" field -> "authentication_string"
    
    update user set authentication_string=password('mynewpassword') where user='root';
    
  6. 刷权限

    flush privileges;
    
  7. 重启服务器

    quit
    
  8. 停止并再次启动服务器

    Ubuntu 和 Debian:

    sudo /etc/init.d/mysql stop
    ...
    sudo /etc/init.d/mysql start
    

    在 CentOS、Fedora 和 RHEL 上:

    sudo /etc/init.d/mysqld stop
    ...
    sudo /etc/init.d/mysqld start
    
  9. 使用新密码登录

    mysql -u root -p
    
  10. 输入新密码并像什么也没发生一样再次享受您的服务器

This was taken from Reset a MySQL root password.

这取自Reset a MySQL root password

回答by rshahriar

I tried the following steps:

我尝试了以下步骤:

  1. Log in as super useror use sudo
  2. Open /etc/mysql/my.cnfusing gedit
  3. Find bind-address, and change its value to the database server host machine's IP address. For me, it was localhostor 127.0.0.1
  4. Save and close the file.
  5. Come back to terminal and execute sudo service mysql start
  1. 登录super user或使用sudo
  2. /etc/mysql/my.cnf使用gedit打开
  3. 查找bind-address,并将其值更改为数据库服务器主机的 IP 地址。对我来说,它是localhost127.0.0.1
  4. 保存并关闭文件。
  5. 回到终端并执行 sudo service mysql start

And it worked for me.

它对我有用。

回答by sbodanis

I fixed this problem by executing the following command:

我通过执行以下命令解决了这个问题:

mysql.server start

And if you are on a mac and used brew to install mysql, simply use:

如果您使用的是 mac 并使用 brew 安装 mysql,只需使用:

brew services start mysql

回答by dkoes

I had a similar problem. mysql wouldn't start:

我有一个类似的问题。mysql 无法启动:

sudo service mysql start
start: Job failed to start

If I disabled apparmor:

如果我禁用了 apparmor:

sudo aa-complain /etc/apparmor.d/*

the problem went away. The issue was that mysqld was trying to access /run/mysqld/mysqld.sock but the apparmor profile only gave permission to /var/run/mysqld/mysqld.sock (/var/run is symlinked to /run, so these are actually the same). Not sure why mysqld isn't using the var path since that's what's set in all the configuration files, but you can fix the problem by adding the following to /etc/apparmor.d/usr.sbin.mysqld

问题就解决了。问题是 mysqld 试图访问 /run/mysqld/mysqld.sock 但 apparmor 配置文件只授予 /var/run/mysqld/mysqld.sock 权限(/var/run 符号链接到 /run,所以这些实际上是相同)。不知道为什么 mysqld 不使用 var 路径,因为这是所有配置文件中设置的内容,但是您可以通过将以下内容添加到 /etc/apparmor.d/usr.sbin.mysqld 来解决问题

/run/mysqld/mysqld.pid rw,
/run/mysqld/mysqld.sock rw,

回答by Martin Schultz

In my case it was that the disk was full and mysqld couldn't start anymore.

就我而言,是磁盘已满,mysqld 无法再启动。

Try to restart mysql service.

尝试重启mysql服务。

service mysql restart

服务mysql重启

or

或者

service mysql stop

service mysql start

服务 mysql 停止

服务 mysql 启动

If it doesn't recognize "stop" command then it's definitely the disk space. You should make some space in the partition mysql is allocated or make the disk larger.

如果它不能识别“停止”命令,那么它肯定是磁盘空间。您应该在分区mysql中分配一些空间或使磁盘更大。

Check the disk space with

检查磁盘空间

df -h

df -h

回答by Wartari

I solved this by killing the mysqlprocess:

我通过杀死mysql进程解决了这个问题:

ps -ef | grep mysql
kill [the id]

And then I started the server again with:

然后我再次启动服务器:

sudo /etc/init.d/mysql restart

But startworks as well:

start也有效:

sudo /etc/init.d/mysql start

Then I logged in as admin, and I was done.

然后我以 身份登录admin,我就完成了。

回答by Wouter

Somehow the MySQL server process did not create the socket, or the client is looking for the socket in the wrong place.

不知何故,MySQL 服务器进程没有创建套接字,或者客户端在错误的位置寻找套接字。

My first suggestion would be to check if the MySQL server is running. Second suggestion might be, is the MySQL server running on another host? If so, add the -h <hostname>flag to your MySQL client in the terminal.

我的第一个建议是检查 MySQL 服务器是否正在运行。第二个建议可能是,MySQL 服务器是否在另一台主机上运行?如果是这样,-h <hostname>请在终端中将该标志添加到您的 MySQL 客户端。

If MySQL is indeed running, and running locally, check your my.cnffile. There should be a line like

如果 MySQL 确实在运行,并且在本地运行,请检查您的my.cnf文件。应该有一条线

socket = /var/run/mysqld/mysqld.sock

See if that matches the socket location that you mentioned in your post.

看看这是否与您在帖子中提到的套接字位置匹配。

From experience, I would say the most likely scenario is your MySQL server either is not running at all or is not running on the same host as where you run your MySQL client from the terminal.

根据经验,我认为最有可能的情况是您的 MySQL 服务器根本没有运行,或者与您从终端运行 MySQL 客户端的主机不在同一台主机上运行。

回答by Rob

I just experienced the same issue after I had to restart my production server. I am running Debian 8.1 (Jessie) on a DigitalOcean droplet.

在我不得不重新启动生产服务器后,我刚刚遇到了同样的问题。我在 DigitalOcean Droplet 上运行 Debian 8.1 (Jessie)。

This is what I did to resolve my issue:

这是我为解决我的问题所做的:

  1. Check if the file /var/run/mysqld/mysqld.sockexists. If it doesn't, manually create it by entering touch /var/run/mysqld/mysqld.sock(which is what I had to do).

  2. So the MySQL process can use this file. Change ownership of said file by entering chown mysql /var/run/mysqld/mysqld.sock.

  3. Once '2' has been done, restart the MySQL service by entering service mysql restartor /etc/init.d/mysql restart.

  1. 检查文件是否/var/run/mysqld/mysqld.sock存在。如果没有,请通过输入手动创建它touch /var/run/mysqld/mysqld.sock(这是我必须做的)。

  2. 所以MySQL进程可以使用这个文件。通过输入更改所述文件的所有权chown mysql /var/run/mysqld/mysqld.sock

  3. 完成“2”后,通过输入service mysql restart或重新启动 MySQL 服务/etc/init.d/mysql restart

After going through the above steps, my issue was solved. I rarely have this issue, and there is probably a better way, so by all means provide constructive feedback if need be :).

经过以上步骤,我的问题就解决了。我很少遇到这个问题,而且可能有更好的方法,所以如果需要,请务必提供建设性的反馈:)。