整理 Windows 與 Linux 兩大平台 MySQL root 密碼重置通用方案,含 my.ini/my.cnf 配置法與命令行 --skip-grant-tables 法。
skip-grant-tables 繞過權限檢查。mysql.user 表密碼字段。FLUSH PRIVILEGES 生效並移除 skip-grant-tables 重啟。my.ini,[mysqld] 區段加入 skip-grant-tables。net stop mysql && net start mysql。mysql -u root。USE mysql;
UPDATE user SET password = PASSWORD('newpass') WHERE user = 'root' AND host = 'localhost';
FLUSH PRIVILEGES;
my.ini 中 skip-grant-tables,重啟服務。net stop mysql d:\usr\local\mysql\bin\mysqld-nt --skip-grant-tables # 視窗 1 # 視窗 2 d:\usr\local\mysql\bin\mysqladmin -u root flush-privileges password "newpass" d:\usr\local\mysql\bin\mysqladmin -u root -p shutdown # 輸入新密碼 net start mysql
killall -TERM mysqld
safe_mysqld --skip-grant-tables &
mysql -u root
UPDATE mysql.user SET password = PASSWORD('新密碼') WHERE User = 'root';
FLUSH PRIVILEGES; QUIT
service mysql restart
sudo /etc/init.d/mysql stop
mysqld --skip-grant-tables &
mysql -u root
UPDATE mysql.user SET password = PASSWORD('newpass') WHERE User = 'root';
FLUSH PRIVILEGES; QUIT
sudo /etc/init.d/mysql restart
兩平台核心邏輯一致,**關鍵在於確認密碼字段名**(5.7 用 authentication_string,舊版用 password)。