且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MySQL错误:#1045-用户' root' @' localhost'的访问被拒绝(使用密码:是)

更新时间:2022-04-28 04:13:10

通用MYSQL信息

首先,请阅读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:

停止MySQL服务器,然后使用--skip-grant-tables选项重新启动它.这使任何人都可以不使用密码并具有所有特权进行连接,并禁用帐户管理语句,例如ALTER USERSET PASSWORD.因为这是不安全的,所以您可能希望将--skip-grant-tables--skip-networking结合使用,以防止远程客户端连接.

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客户端连接到MySQL服务器;不需要密码,因为服务器是使用--skip-grant-tables启动的:

Connect to the MySQL server using the mysql client; no password is necessary because the server was started with --skip-grant-tables:

shell> mysql

在mysql客户端中,告诉服务器重新加载授权表,以便帐户管理语句起作用:

In the mysql client, tell the server to reload the grant tables so that account-management statements work:

mysql> FLUSH PRIVILEGES;

然后更改'root'@'localhost'帐户密码.用您要使用的密码替换密码.要更改具有不同主机名部分的root帐户的密码,请修改说明以使用该主机名.

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.

MySQL 5.7.6及更高版本:

MySQL 5.7.6 and later:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

MySQL 5.7.5和更早版本:

MySQL 5.7.5 and earlier:

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');

或直接在用户表上:

UPDATE mysql.user SET password=PASSWORD('mynewpassword') WHERE user='root';

停止MySQL服务.打开命令窗口.转到XAMPP MySQL目录:

Stop the MySQL service. Open a command window. Change to the XAMPP MySQL directory:

> cd \xampp\mysql\bin\

在没有安全性的情况下运行服务(请注意,您正在运行mysqld,而不是mysql):

Run the service without security (note you are running mysqld, not mysql):

> mysqld.exe --skip-grant-tables

MySQL服务将在此窗口中运行,因此打开另一个命令窗口并切换到XAMPP MySQL目录:

The MySQL service will be running in this window, so open another command window and switch to the XAMPP MySQL directory:

> cd \xampp\mysql\bin\

运行MySQL客户端:

Run the MySQL client:

> mysql

更新密码:

mysql> UPDATE mysql.user SET password=PASSWORD('mynewpassword') WHERE user='root';

退出MySQL:

mysql> \q

使用任务管理器取消仍在运行的mysqld.exe.重新启动mysql服务.

Use task manager to cancel the mysqld.exe that is still running. Restart the mysql service.