以前MySQL忘记密码的”三板斧“
- 停服务
- 配置文件增加skip-grant-tables
- root用户免密登录,执行
update
user
set
password
=
PASSWORD
(
新密码
)
where
user
=
'root'
;
- 执行flush privileges
- 启动服务
而现在值得注意的是:
- MySql版本直接从5.7到8.0
- password()已弃用,被md5()、sha1()取代
- 在my.ini中加入skip-grant-tables方式,在MySQL8.0中已失效,只能使用命令方式
- [mysqld]的default_authentication_plugin默认为caching_sha2_password
- default_authentication_plugin在8.0.27已Deprecated
- default_authentication_plugin取值范围:mysql_native_password、sha256_password和caching_sha2_password
- MySQL以skip-grant-tables选项下运行,使用update mysql.user set authentication_string=不管用啥('新密码') where User = 'root',密码一直会无效
- MySQL以skip-grant-tables选项下运行,不能直接执行ALTER USER修改密码语句
- 如果将authentication_string字段置空后,非skip-grant-tables选项下,root可以直接免密登录
综上所述,只能结合这两种方式进行迂回,才能修改密码:
- 以管理员运行命令行,停止服务 `net stop mysql`
- 运行`mysqld --shared-memory --skip-grant-tables --skip-networking --console`,不要关闭窗口
- 另外打开新的命令行窗口,执行`mysql -uroot -p`无密码登录
- 将root用户密码置空`update mysql.user set authentication_string='' where User = 'root';`
- 刷新`flush privileges`
- 停止 mysqld
- 启动服务 `net start mysql`
- root用户可以无密码登录,修改密码`ALTER USER 'root'@'localhost' IDENTIFIED WITH MYSQL_NATIVE_PASSWORD BY '新密码';`
- 刷新`flush privileges`,然后可以正常登录
总归觉得这种解决方法不够直接,如果大家有更好的亲测有效的解决方法,欢迎随时交流。