一、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 --conso
然后再开一个窗口,执行下面命令
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;