遺忘 MySQL root 密碼時,可透過啟動參數 --skip-grant-tables 跳過權限驗證,以空密碼登入並重置密碼。本文提供 Windows 環境完整圖文步驟。
C:\Program Files\MySQL\MySQL Server 5.x\bin)「服務」管理工具(services.msc)停止 MySQL 服務,或工作管理員結束 mysqld.exe 進程。
cd C:\Program Files\MySQL\MySQL Server 5.x\bin
mysqld --skip-grant-tables
mysql -u root -p
密碼直接按 Enter(空密碼)mysql> 提示符後執行:
UPDATE mysql.user SET Password=PASSWORD('新密碼') WHERE User='root';
FLUSH PRIVILEGES;
EXIT 離開--skip-grant-tables 執行個體)authentication_string,指令改為:
UPDATE mysql.user SET authentication_string=PASSWORD('新密碼') WHERE User='root';
caching_sha2_password,建議同步指定:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密碼';
localhost 連線