忘記 MySQL root 密碼是常見運維場景,核心原理是啟動跳過權限表模式 (--skip-grant-tables),免密登入後修改 mysql.user 表密碼欄位。
net stop mysqlbin 資料夾 (如 C:\Program Files\MySQL\MySQL Server 8.0\bin)mysqld --skip-grant-tables --consolemysql -u rootUSE mysql;
-- MySQL 5.7+ 使用 authentication_string 欄位
UPDATE user SET authentication_string=PASSWORD('新密碼') WHERE User='root';
-- MySQL 8.0+ 建議使用 ALTER USER
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密碼';
FLUSH PRIVILEGES;
EXIT;
mysqld 進程 (工作管理員或 taskkill /f /im mysqld.exe)net start mysqlsystemctl stop mysqld 或 /etc/init.d/mysqld stopmysqld_safe --skip-grant-tables --skip-networking &mysql -u rootsystemctl restart mysqldpassword 欄位改為 authentication_stringcaching_sha2_passwordALTER USER 語法而非直接 UPDATE 系統表SET GLOBAL validate_password.policy=LOW;--skip-grant-tables 期間任何人可免密存取資料庫,**務必加上 --skip-networking 限制本機存取**,完成後立即重啟正常模式。