--skip-grant-tables 正常啟動在 C 槻建立 mysql-init.txt,內容:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewStrongPass123!';
FLUSH PRIVILEGES;
注意:MySQL 5.7 用 UPDATE mysql.user SET authentication_string=PASSWORD('newpass') WHERE user='root';,8.0+ 必須用 ALTER USER。
net stop mysql
# 或 服務管理器 (services.msc) 停止
進入 MySQL 安裝目錄 bin 目錄(如 C:\Program Files\MySQL\MySQL Server 8.0\bin):
mysqld --init-file=C:\\mysql-init.txt --console
--console 讓輸出顯示在當前視窗,方便觀察C:/mysql-init.txt看到 ready for connections 表示初始化完成,密碼已重置。
Ctrl+C 停止當前 mysqld
net start mysql
mysql -u root -p
# 輸入新密碼登入成功
| 現象 | 原因 | 解決 |
|---|---|---|
| 報錯 "Can't find file" | init-file 路徑錯誤 | 使用絕對路徑,確認副檔名非 .txt.txt |
| 密碼不生效 | MySQL 版本語法不匹配 | 5.7 用 UPDATE authentication_string,8.0 用 ALTER USER |
| 服務啟動失敗 | 殘留進程佔用埠 | 任務管理器結束所有 mysqld.exe 再試 |
| 權限不足 | 非管理員執行 CMD | 以管理員身分執行命令提示字元 |
mysql-init.txt