首先跳过权限认证方式启动mysql:
修改my.cnf 文件,默认路径/etc/my.cnf,在[mysqld]模块下增加skip-grant-tables, 如图:
1
重启服务:
service mysqld restart
登录mysql,删除mysql.user表中host名为'localhost'的记录:
[root@localhost ~]# mysql
mysql> use mysql
mysql> delete from user where host='localhost';
Query OK, 7 rows affected (0.00 sec)
mysql> INSERT INTO `user` VALUES ('localhost','root','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0,0,'mysql_native_password','','Y','2021-06-23 03:17:59',NULL,'N');
Query OK, 1 row affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
修改my.cnf 文件:
删除[mysqld]模块下skip-grant-tables
重启服务:
service mysqld restart
进入MySQL,修改root密码:
[root@localhost ~]# mysql -uroot
mysql> use mysql
mysql> set password for 'root'@'localhost'=password('newpasswd');