修改 MySQL 密码

My GitHub Home Page

错误提示: ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

这是因为 root 账号在首次登录时,原初始密码必须修改。本文探讨下如何修改密码,同样适用于忘记密码。

修改密码

在已知密码的前提下,先登录:

mysql -u root -p mysql

根据提示输入 root 账号的密码,然后通过下面方式之一修改密码。

方式一

update user set authentication_string=password('654321') where user='root';

方式二

set password=password('654321');

方式三

alter user root@localhost identified by '654321';

如果不想登录,也是可以修改密码的。

方式四

以管理员身份运行命令提示符,进入 MySQL 的 bin 目录,执行命令:

-- oldpassword: i/iyviZ2Cf2k
-- newpassword: 654321
mysqladmin -u root -pi/iyviZ2Cf2k pasword 654321

重设密码

要是忘记 root 密码,按照以下方式重设密码。

Step 1:关闭服务

以管理员身份运行命令提示符,执行命令:

net stop MySQL

Step 2:跳过授权

进入 MySQL 的 bin 目录,执行命令:

mysqld --skip-grant-tables

Step 3:连接

跳过授权的命令提示符下,无法继续操作,此时,需要运行一个新的命令提示符窗口,执行命令:

mysql -u root -p mysql

此时,无需输入密码。

Step 4:修改密码

使用上述修改密码的方式 1 ~ 3 修改密码,例如:

update user set authentication_string=password('root') where user='root';

退出连接:

quit;

Step 5:重启服务

以管理员身份运行命令提示符,执行命令:

net start MySQL