忘記 Root 密碼是常見緊急情況。本文提供 Windows/Linux 通用的標準重置流程,無需重裝、無需破解,三分鐘搞定。
以 --skip-grant-tables 啟動 MySQL,跳過權限驗證 → 無密碼登入 → 更新 mysql.user 表密碼欄位 → 刷新權限 → 正常重啟。
net stop mysql
bin 目錄(如 C:\Program Files\MySQL\MySQL Server 5.7\bin)
mysqld --skip-grant-tables --console
視窗保持開啟,不要關閉。
mysql -u root
USE mysql;
-- MySQL 5.7+ 欄位為 authentication_string
UPDATE user SET authentication_string=PASSWORD('new_password') WHERE User='root';
-- MySQL 5.6 欄位為 password
-- UPDATE user SET password=PASSWORD('new_password') WHERE User='root';
FLUSH PRIVILEGES;
EXIT;
mysqld --skip-grant-tables 的 CMD 視窗(或 Ctrl+C)net start mysql
mysql -u root -p'new_password'
systemctl stop mysqld
# 或
service mysqld stop
# 若無法停止,強制殺死
killall -TERM mysqld
mysqld_safe --skip-grant-tables &
# 部分版本為
mysqld --skip-grant-tables --user=mysql &
mysql -u root
USE mysql;
UPDATE user SET authentication_string=PASSWORD('new_password') WHERE User='root';
FLUSH PRIVILEGES;
EXIT;
# 先殺死 skip-grant-tables 進程
killall -TERM mysqld
# 正常啟動
systemctl start mysqld
mysql -u root -p'new_password'
MySQL 8.0 密碼加密採用 caching_sha2_password,PASSWORD() 函數已移除。更新密碼請用:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
FLUSH PRIVILEGES;
或在 --skip-grant-tables 模式下:
UPDATE mysql.user SET authentication_string='' WHERE User='root';
FLUSH PRIVILEGES;
-- 正常重啟後再執行
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
| 現象 | 原因 | 解決 |
|---|---|---|
| 無法啟動 --skip-grant-tables | 權限不足/目錄錯誤/已有進程佔用 | 確認以 root/管理員執行、指定 --user=mysql、檢查 port 3306 佔用 |
| 登入報錯 Can't connect to local MySQL server | Socket 路徑不匹配 | 指定 --socket=/var/lib/mysql/mysql.sock 或 -S /tmp/mysql.sock |
| UPDATE 後密碼不生效 | 欄位名錯誤(5.7+ 用 authentication_string) | 查詢 DESC mysql.user 確認欄位名 |
| 重啟後仍無法登入 | 未正確殺死 skip-grant-tables 進程 | ps aux | grep mysqld 確認僅有一個正常進程 |