ITPub博客

首页 > 数据库 > MySQL > Mysql root密码忘记找回妙招

Mysql root密码忘记找回妙招

原创 MySQL 作者:wddwr7307914 时间:2021-04-17 06:45:47 0 删除 编辑



一、Mysql root用户密码忘记(8.0以下版本)

1.用命令编辑/etc/my.cnf配置文件,即:vim /etc/my.cnf 或者 vi /etc/my.cnf
2.在[mysqld]下添加skip-grant-tables,然后保存并退出
3.重启mysql服务:service mysqld restart
4.更改root用户名
重启以后,执行mysql命令进入mysql命令行
5.修改root用户密码
update mysql.user set authentication_string=password('root') where user='root';   --5.7版本
flush privileges;
6.把/etc/my.cnf中的skip-grant-tables注释掉,然后重启mysql,即:service mysqld restart
好了,下面就可以用root新的密码登录了!


二、Mysql root用户密码忘记(8.0及以上版本)

在mysql8系统下,适用如下方法(全路径)
G:\mysql-8.0.23-winx64\bin\mysqld --datadir=G:\mysql-8.0.23-winx64\data80323308 --console --skip-grant-tables --shared-memory

然后再开一个窗口,执行下面命令
cd  G:\mysql-8.0.23-winx64\bin

mysql> select user,host,authentication_string from mysql.user;

mysql> delete from mysql.user where user='root' ;
mysql> flush privileges;   --记得刷新哈,如果提示无法创建
mysql> create user root@'%' identified with mysql_native_password by 'root';
mysql> grant all on *.* to root@'%' with grant option;
mysql> flush privileges;
mysql> drop user 'root'@'localhost';
mysql> flush privileges;
mysql> create user root@'localhost' identified with mysql_native_password by 'root';
mysql> grant all on *.* to root@'localhost' with grant option;
mysql> flush privileges;


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69947868/viewspace-2768479/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: Python集合浅谈
请登录后发表评论 登录
全部评论
中国DBA联盟(ACDU)成员,Oracle PostgreSQL ACE,CSDN博客专家及B站知名UP主,全网粉丝10万+,目前服务于工业互联网,擅长主流Oracle、MySQL、PG、高斯及Greenplum备份恢复,安装迁移,性能优化、故障应急处理。

注册时间:2020-06-13

  • 博文量
    203
  • 访问量
    213028