1.先關閉mysql服務器
systemctl stop mysqld
2.修改配置文件
vi /usr/local/mysql/etc/my.cnf
在 [mysqld]裏面加上
skip-grant-tables
3重新啓動mysql服務器
systemctl start mysqld
4.登錄服務器,此時是免密登錄
mysql -uroot -p
5.修改數據庫root密碼
mysql> alter user root@localhost identified by '123';
6.停止Mysql
systemctl stop mysqld
7.修改my.cnf文件
#skip-grant-tables
8.重啓Mysql
systemctl start mysqld
9.用新密碼登錄即可