一、修改 MySQL 配置文件:
[root@mysql ~]# vim /etc/my.cnf
skip-grant-tables #设置免密登录
二、重启 MySQL:
[root@mysql ~]# systemctl restart mysqld
三、修改密码:
1、直接登录 MySQL数据库:
[root@mysql ~]# mysql
2、切换到 MySQL 系统库 mysql:
mysql> use mysql;
3、设置密码:
mysql> update user set authentication_string=password('新密码') where user='root';
四、注释掉免密登录:
[root@mysql ~]# vim /etc/my.cnf
#skip-grant-tables
五、重启 MySQL 然后登录:
[root@mysql ~]# systemctl restart mysqld
[root@mysql ~]# mysql -uroot -p'新密码'