MySQL 错误:#1045 - 用户 'root'@'localhost' 访问被拒绝(使用密码:YES)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41818827/
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
MySQL error: #1045 - Access denied for user 'root'@'localhost' (using password: YES)
提问by SandyS
I have tried every fix that I have found, and I cannot change my root MySQL password. At the shell, if I type in mysql -u root
, I get
我已经尝试了我找到的所有修复程序,但我无法更改我的 MySQL 根密码。在 shell 中,如果我输入mysql -u root
,我会得到
Access denied for user 'root'@'localhost' (using password: NO)
用户 'root'@'localhost' 访问被拒绝(使用密码:NO)
When I go to PHP MyAdmin, I get this:
当我去 PHP MyAdmin 时,我得到这个:
#1045 - Access denied for user 'root'@'localhost' (using password: YES)
#1045 - 用户 'root'@'localhost' 访问被拒绝(使用密码:YES)
This all started when I was forced to enter a new password for MySQL. I have NO idea how to fix this and I am desperate.
这一切都始于我被迫为 MySQL 输入新密码时。我不知道如何解决这个问题,我很绝望。
Thank you.
谢谢你。
回答by Steve Johnson
GENERIC MYSQL INFO
通用 MYSQL 信息
To start with, read the mysql manual: https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
首先,阅读 mysql 手册:https: //dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
The steps will show you how to shutdown the service and start it with an overriding command that doesn't require passwords, then you reset the password. From the manual:
这些步骤将向您展示如何关闭服务并使用不需要密码的覆盖命令启动它,然后您重置密码。从手册:
Stop the MySQL server, then restart it with the --skip-grant-tables
option. This enables anyone to connect without a password and with all privileges, and disables account-management statements such as ALTER USER
and SET PASSWORD
. Because this is insecure, you might want to use --skip-grant-tables
in conjunction with --skip-networking
to prevent remote clients from connecting.
停止 MySQL 服务器,然后使用--skip-grant-tables
选项重新启动它。这使任何人都可以在没有密码和所有权限的情况下进行连接,并禁用帐户管理语句,例如ALTER USER
和SET PASSWORD
。由于这是不安全的,您可能希望与--skip-grant-tables
结合使用--skip-networking
以防止远程客户端连接。
Connect to the MySQL server using the mysql client; no password is necessary because the server was started with --skip-grant-tables
:
使用mysql客户端连接MySQL服务器;不需要密码,因为服务器是通过以下方式启动的--skip-grant-tables
:
shell> mysql
shell> mysql
In the mysql client, tell the server to reload the grant tables so that account-management statements work:
在 mysql 客户端中,告诉服务器重新加载授权表,以便帐户管理语句起作用:
mysql> FLUSH PRIVILEGES;
mysql> FLUSH PRIVILEGES;
Then change the 'root'@'localhost'
account password. Replace the password with the password that you want to use. To change the password for a root account with a different host name part, modify the instructions to use that host name.
然后更改'root'@'localhost'
帐户密码。将密码替换为您要使用的密码。要更改具有不同主机名部分的 root 帐户的密码,请修改使用该主机名的说明。
MySQL 5.7.6 and later:
MySQL 5.7.6 及更高版本:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
MySQL 5.7.5 and earlier:
MySQL 5.7.5 及更早版本:
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');
Or directly on the user table:
或者直接在用户表上:
UPDATE mysql.user SET password=PASSWORD('mynewpassword') WHERE user='root';
UPDATE mysql.user SET password=PASSWORD('mynewpassword') WHERE user='root';
XAMPP SPECIFIC
XAMPP 特定
Stop the MySQL service. Open a command window. Change to the XAMPP MySQL directory:
停止 MySQL 服务。打开命令窗口。切换到 XAMPP MySQL 目录:
> cd \xampp\mysql\bin\
> cd \xampp\mysql\bin\
Run the service without security (note you are running mysqld, not mysql):
在没有安全性的情况下运行服务(请注意,您正在运行 mysqld,而不是 mysql):
> mysqld.exe --skip-grant-tables
> mysqld.exe --skip-grant-tables
The MySQL service will be running in this window, so open another command window and switch to the XAMPP MySQL directory:
MySQL 服务将在此窗口中运行,因此打开另一个命令窗口并切换到 XAMPP MySQL 目录:
> cd \xampp\mysql\bin\
> cd \xampp\mysql\bin\
Run the MySQL client:
运行 MySQL 客户端:
> mysql
> mysql
Update the password:
更新密码:
mysql> UPDATE mysql.user SET password=PASSWORD('mynewpassword') WHERE user='root';
mysql> UPDATE mysql.user SET password=PASSWORD('mynewpassword') WHERE user='root';
Exit MySQL:
退出 MySQL:
mysql> \q
mysql> \q
Use task manager to cancel the mysqld.exe that is still running. Restart the mysql service.
使用任务管理器取消仍在运行的mysqld.exe。重启mysql服务。
回答by Netaji
my problem was solved in below way.
我的问题是通过以下方式解决的。
When I was entered below command then I will get an error. sudo mysql -u root -p
当我在命令下面输入时,我会收到一个错误。 须藤mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES).
To resolve this follow below steps. Enter command,
要解决此问题,请按照以下步骤操作。输入命令,
sudo mysqld_safe --skip-grant-tables &
Then the below error occurred.
然后出现了下面的错误。
2018-12-17T06:43:33.106111Z mysqld_safe Logging to syslog.
2018-12-17T06:43:33.108971Z mysqld_safe Logging to '/var/log/mysql/error.log'.
/usr/bin/mysqld_safe: 152: /usr/bin/mysqld_safe: cannot create /var/log/mysql/error.log: Permission denied
2018-12-17T06:43:33.111388Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
/usr/bin/mysqld_safe: 152: /usr/bin/mysqld_safe: cannot create /var/log/mysql/error.log: Permission denied
To resolve this error, then follow below lines.
要解决此错误,请按照以下几行操作。
systemctl stop mysql
systemctl stop mysql
2018-12-17T06:48:42.971692Z mysqld_safe Logging to syslog.
2018-12-17T06:48:42.974451Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2018-12-17T06:48:42.976653Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld
sudo mysqld_safe --skip-grant-tables &
sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld
sudo mysqld_safe --skip-grant-tables &
2018-12-17T06:50:39.135292Z mysqld_safe Logging to syslog.
2018-12-17T06:50:39.137938Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2018-12-17T06:50:39.152966Z mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
Then connect
mysql -u root
. then MySQL prompt will appear. then after set your password.FLUSH PRIVILEGES;
然后连接
mysql -u root
。然后会出现 MySQL 提示。然后在设置密码后。同花顺特权;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
systemctl stop mysql
After that loing in below way.
mysql -u root -p
systemctl stop mysql
在下面的方式之后。
mysql -u root -p
回答by Renato Pierri
For Windows users: Before starting to recover your password, try in command line:
对于 Windows 用户:在开始恢复密码之前,请在命令行中尝试:
mysql -uroot -pyourpassword
as is, without space like shown below:
原样,没有空间,如下所示:
I have no time to explain why, but if you want to know more, please check how the 'MySQL X.X command line client' shortcut in your computer works.
我没有时间解释原因,但是如果您想了解更多信息,请检查您计算机中的“MySQL XX 命令行客户端”快捷方式是如何工作的。
In time, make sure that the mysql paths are in the PATH variable.
及时确保 mysql 路径在 PATH 变量中。
My computeris using these paths shown below. Your computer might be using other paths. C:\ProgramData\MySQL\MySQL Server 8.0 C:\Program Files\MySQL\MySQL Server 8.0\bin
我的计算机正在使用如下所示的这些路径。您的计算机可能正在使用其他路径。C:\ProgramData\MySQL\MySQL Server 8.0 C:\Program Files\MySQL\MySQL Server 8.0\bin
回答by tshirtdr1
I had this same problem recently. The newest version of PHPmyadmin does not allow you to login as root. I had additional problems because the username and password defined during setup on Ubuntu 18 by the setup script was not granted any privileges. I solved this by logging into mysql as root on the terminal. Then I added a new user from the command line. At that point I could log in as the new user in PHP my admin and the new user had the privileges I granted.
我最近遇到了同样的问题。最新版本的 PHPmyadmin 不允许您以 root 身份登录。我遇到了其他问题,因为安装脚本在 Ubuntu 18 上设置期间定义的用户名和密码没有被授予任何权限。我通过在终端上以 root 用户身份登录 mysql 解决了这个问题。然后我从命令行添加了一个新用户。那时我可以在我的管理员 PHP 中以新用户身份登录,新用户拥有我授予的权限。