跳過權限表啟動 MySQL,以免密碼驗證,進入後更新 mysql.user 表的 authentication_string(或舊版 password)欄位,重啟生效。
systemctl stop mysqld 或 service mysql stop。vim /etc/my.cnf,在 [mysqld] 區段添加 skip-grant-tables。systemctl start mysqld(此時無需密碼可登入)。mysql -u root 直接進入。USE mysql;
-- MySQL 5.7+ 使用 authentication_string
UPDATE user SET authentication_string=PASSWORD('NewPass123') WHERE User='root';
-- 或 5.6 及更早版本
UPDATE user SET password=PASSWORD('NewPass123') WHERE User='root';
FLUSH PRIVILEGES;
skip-grant-tables。systemctl restart mysqld,新密碼生效。net stop mysql。my.ini(通常在安裝目錄或 C:\ProgramData\MySQL\MySQL Server x.y\),在 [mysqld] 下加 skip-grant-tables。net start mysql。mysql -u root。skip-grant-tables,重啟服務。PASSWORD() 函數已移除,需用 ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPass123'; 或直接更新 authentication_string 為 SHA256 雜湊值(較繁瑣),建議用 ALTER USER。FLUSH PRIVILEGES 並重啟。skip-grant-tables 期間任何人均可免密登入,**務必在安全網絡環境下操作,並盡快恢復配置重啟**。