忘記 MySQL Root 密碼時,透過跳過權限表啟動(--skip-grant-tables)即可免密登入並重置。以下為 Linux/Windows 完整步驟。
systemctl stop mysqld 或 service mysql stopmysqld_safe --skip-grant-tables --skip-networking &
--skip-networking 防止外部連入,安全第一。
mysql -u rootFLUSH PRIVILEGES; -- 必須先執行,才能修改 grant 表
ALTER USER 'root'@'localhost' IDENTIFIED BY '新強密碼';
-- 或 MySQL 5.7 以下
UPDATE mysql.user SET authentication_string=PASSWORD('新密碼') WHERE User='root' AND Host='localhost';
FLUSH PRIVILEGES;
systemctl restart mysqldmysql -u root -pnet stop mysql(或服務管理器)cd "C:\Program Files\MySQL\MySQL Server 8.0\bin"
mysqld --console --skip-grant-tables --skip-networking
保持視窗開啟(前台執行)。
mysql -u rootnet start mysqlauthentication_string,不再用 password。caching_sha2_password,舊版驅動可能需指定 mysql_native_password:ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密碼';FLUSH PRIVILEGES; 在 --skip-grant-tables 下必須執行,否則修改不生效。--skip-grant-tables 並重啟。localhost 登入,遠程管理用專用帳號。標準化流程,讓密碼遺失不再是恐慌源頭。