Mysql 8.0 + 版本 忘记数据库root密码 处理方法

系统情况:

1.MySQL Server version: 8.0.20 MySQL Community Server - GPL

2.Linux Server Version:CentOS Linux release 7.8.2003 (Core)

处理步骤:

方法1

1、停止mysql服务

systemctl stop mysqld.service

2、修改默认配置文件/etc/my.cnf

在[mysqld]中添加

skip-grant-tables = 1 (目的是跳过授权检测)

skip-networking = 1 (禁止远程访问连接mysql,此项是安全考虑,测试环境可不设置)

之后保存 重启mysql服务

systemctl restart mysqld.service

3、无密码本机通过mysql.sock登陆mysql服务器

mysql -uroot -p 回车回车

注意此处,由于前面设置了skip-grant-tables = 1,此时无法使用

alter user  'root@localhost' identified by 'password'; 

来修改root的密码

使用如下命令修改,

mysql>\u mysql

mysql>update user set authentication_string = "" where user = "root";  # (这里会将所有root用户名的密码都设置为空,如果你的机器配置了远程root可登陆请小心)

如果需要查询自己的用户及密码情况 可使用

mysql>select user,host,authentication_string from user;

来查看

退出mysql登陆,停止mysqld服务

4、继续编辑/etc/my.cnf文件 将添加的两项注销掉

重启服务之后,可以使用

mysql -uroot -p 空密码登陆数据库

然后使用

set password for 'root'@'localhost' = 'password';

或者

alter user 'root@localhost' identified by 'password';

来修改root@localhost的密码

方法2

方法1中进行到第三步之后 即无密码登陆数据库之后

使用

mysql>flush privileges;

手动将权限验证表加载入内存,

然后使用

mysql>alter user 'root'@'localhost' identified by 'password';

或者

mysql>set password for 'root'@'localhost' = 'password';

来修改密码 然后退出mysql

修改/etc/my.cnf 删除添加的skip_grant_tables以及skip_networking

保存,重启服务即可;

完成

你可能感兴趣的:(Mysql 8.0 + 版本 忘记数据库root密码 处理方法)