1.先將MySQL停止、
命令:systemctl stop mysqld #停掉MySQL
命令:systemctl status mysqld #查看狀態
2.然后跳過授權表啟動MySQL服務程序
這一步主要利用mysqld的 --skip-grant-tables選項
修改my.cnf配置,添加 skip_grant_tables=1啟動設置:
打開/etc/my.cnf配置文件
添加skip_grant_tables 一行,然后保存退出

3.然后啟動MySQL並查看狀態
命令:systemctl start mysqld

4.使用mysql命令連接到MySQL服務,重設root的密碼
由於前一步啟動的MySQL服務跳過了授權表,所以可以root從本機直接登錄
在命令行內直接輸入mysql即可

5.進入 mysql> 環境后,通過修改mysql庫中user表的相關記錄,重設root用戶從本機登錄的密碼:
命令:update mysql.user set authentication_string=password('root') where user='root' and host='localhost' #重新設置密碼
命令:FLUSH PRIVILEGES; #刷新授權列表
命令: exit # 退出mysql
6.重新以正常方式啟動MySQL服務程序,驗證新密碼
如果前面是修改/etc/my.cnf配置的方法來跳過授權表,則重置root密碼后,應去除相應的設置以恢復正常:

設置密碼命令:
update mysql.user set authentication_string=password('root') where user='root' and host='localhost' #重新設置密碼
alter user 'root'@'localhost' identified by '123456';
set password for root@localhost=password('1234567');
grant all ON *.* TO root@localhost identified by '123qqq...A';
