场景:
centos7环境下忘记MySQL8.0的密码
1、免密登录
vim /etc/my.conf
文件中任意位置加上如下代码:skip-grant-tables,保存,退出;
2、登录MySQL
mysql - u root -p
提示输入密码,直接回车就行
3、重置密码
首先查看user表中root用户的信息
select host, user, authentication_string, plugin from user;
如果root用户下的authentication_string字段有内容,首先清空,执行如下命令:
use mysql
update user set authentication_string='' where user='root';
至此可以实现免密登录了,打开/etc/my.conf
配置文件,将加入的skip-grant-tables
删除
重启mysql服务
service mysqld restart
最后一步,修改root密码
alter user 'root'@'%' identified by 'Wuhanins..0'
以上注意事项:
1、%是根据你实际查询表user得到的root用户对用的host,你host是localhost就写这个,我的是“%”。所以写的是%;
2、注意密码强度
查询数据库版本:
mysql>status