MySQL 官方文檔提供的 --skip-grant-tables 重置密碼流程在 Linux 有效,但在 Windows 下常因服務管理差異導致失效。本文記錄一次真實踩坑經歷與正確操作步驟。
mysqld --skip-grant-tables 啟動,無報錯但 3306 端口未監聽mysql -u root 連接失敗UPDATE user SET password='';(缺少 PASSWORD() 函數)導致無法登錄net stop mysql
mysqld --skip-grant-tables --console
# 新開一個 CMD 窗口
mysql -u root
# 在 MySQL 客戶端內
USE mysql;
UPDATE user SET authentication_string=PASSWORD('newpass') WHERE User='root';
FLUSH PRIVILEGES;
EXIT;
# 關閉 --console 窗口 (Ctrl+C)
net start mysql
C:\mysql-init.txt 內容:ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpass';
net stop mysqlmysqld --init-file=C:\\mysql-init.txt --consolepassword → authentication_stringWindows 下重置 root 密碼核心在於:以 --console 方式啟動 --skip-grant-tables,確保服務真正運行在前台並可見日誌。切勿直接修改 password 列而不使用 PASSWORD() 函數。