忘記 MySQL 的 root 密碼時,可透過跳過權限驗證的方式重設,主要牽涉到權限相關設定。操作步驟如下:
1、先確認 MySQL 的工作狀態:mysql status。若 MySQL 正在 running,先將其關閉:
service mysql stop
2、進入 MySQL 設定檔:
vim /etc/my.cnf
在 [mysqld] 段落中加上一句:
skip-grant-tables
3、啟動 MySQL:
service mysql start
4、登入並修改 root 密碼:
/usr/bin/mysql
mysql> use mysql; mysql> update user set Password = password ( 'new-password' ) where user = 'root'; mysql> exit
5、把 MySQL 登入設定改回來,編輯 /etc/my.cnf,刪除先前加入的 skip-grant-tables 那一行。
6、重啟 MySQL 服務:
/root/lnmp restart