MySql5.7忘记root密码怎么办?

1、修改 /etc/my.cnf 文件,在 [mysqld] 下面新增一行

[mysqld]

... ...

skip-grant-tables

2、重启Mysql服务

systemctl   restart  mysqld

3、使用命令行方式登录Mysql,不用输入密码,直接回车

mysql    -uroot    -p

4、查看下 mysql 版本

mysql> status;
--------------

mysql  Ver 14.14 Distrib 5.7.29, for Linux (x86_64) using  EditLine wrapper

Connection id:		9
Current database:	mysql
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		5.7.29 MySQL Community Server (GPL)
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	utf8
Db     characterset:	latin1
Client characterset:	utf8
Conn.  characterset:	utf8
UNIX socket:		/var/lib/mysql/mysql.sock
Uptime:			1 hour 15 min 32 sec

Threads: 1  Questions: 89  Slow queries: 0  Opens: 157  Flush tables: 1  Open tables: 150  Queries per second avg: 0.019
--------------

5、进入mysql数据库修改密码

mysql> use mysql;
mysql>  update user set authentication_string=password('新密码') WHERE user="root" AND Host='localhost'; 
 修改root密码

6、刷新权限表

mysql> flush privileges;
 exit;   //退出命令行模式

7、删除或注释/etc/my.cnf文件中新添加的 skip-grant-tables 这一行

8、重启Mysql服务

[root@bogon ~]# systemctl   restart  mysqld

9、现在可以用新密码登陆了。

你可能感兴趣的:(MySql5.7忘记root密码怎么办?)