遺忘 MySQL root 密碼時,可透過跳過權限表機制重置。操作期間資料庫無密碼保護,請先封鎖對外連線或拔網路線。
/etc/my.cnf,在 [mysqld] 區段加入:skip-grant-tables/etc/init.d/mysqld restart 或 systemctl restart mysqldmysql -u root
USE mysql;
UPDATE user SET Password = PASSWORD('new-password') WHERE User = 'root';
FLUSH PRIVILEGES;
QUIT;skip-grant-tables,儲存。/etc/init.d/mysqld restartmysql -u root -p'new-password'Password 欄位改為 authentication_string:UPDATE user SET authentication_string = PASSWORD('new-password') WHERE User = 'root';ALTER USER 'root'@'localhost' IDENTIFIED BY 'new-password'; (需正常權限模式下執行)。skip-grant-tables 並重啟。mysql_secure_installation 強化安全。此法適用於多數 MySQL/MariaDB/Percona Server 版本。