首先编辑my.cnf
vim /etc/my.cnf
在[mysqld]下面添加skip-grant-tables
然后重启mysql
systemctl restart mysqld
直接输入mysql按回车就能进入mysql
[root@abc ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.40 MySQL Community Server (GPL)
更新密码,我这里设置为123456
mysql> update mysql.user set authentication_string=password('123456') where user='root' and host='localhost';
刷新一下权限
mysql> flush privileges;
然后exit退出mysql
mysql> exit
再取消skip-grant-tables
vim /etc/my.cnf
在[mysqld]下面删除skip-grant-tables
重启mysql
systemctl restart mysqld