忘記 MySQL root 密碼是資料庫管理常見的困擾。本文整理經實測可行的重置流程,分別說明 Windows 與 Linux 環境下的操作步驟,助您快速恢復資料庫存取權限。
net stop mysql
bin 資料夾(預設常見於 C:\Program Files\MySQL\MySQL Server 版本\bin)。mysqld --skip-grant-tables
注意:舊版本可能為 mysqld-nt.exe,新版本統一為 mysqld.exe。
bin 目錄,直接輸入 mysql 進入無密碼模式。new_password 替換為您想設定的新密碼):
USE mysql;
UPDATE user SET authentication_string=PASSWORD('new_password') WHERE User='root';
FLUSH PRIVILEGES;
EXIT;
MySQL 5.7 以前版本欄位為 password,5.7 以後改為 authentication_string,建議兩者皆更新以防萬一。
mysqld 進程,或透過工作管理員結束 mysqld.exe。net start mysql
mysql -u root -p。killall -TERM mysqld
mysqld_safe --skip-grant-tables &
mysql。mysqld 進程:killall -TERM mysqld。systemctl start mysqld(或 service mysql start)。mysqld --skip-grant-tables 失敗,請檢查 datadir 權限或錯誤日誌(通常在 /var/log/mysql/error.log)。caching_sha2_password,若舊版客戶端連線失敗,可考慮改用 mysql_native_password:ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';FLUSH PRIVILEGES; 使權限立即生效。掌握以上流程,下次遇到密碼遺忘即可從容應對。建議平時妥善保管密碼,或使用密碼管理工具避免類似情況發生。