以 --skip-grant-tables 啟動 MySQL,跳過權限檢查,以 root 免密登入,更新 mysql.user 表的 authentication_string(5.7+)或 Password(5.6 及舊版),再刷新權限並正常重啟。
/etc/my.cnf,在 [mysqld] 區段加入:skip-grant-tablessystemctl restart mysqldmysql -u rootUSE mysql;
UPDATE user SET authentication_string = PASSWORD('NewPass123!') WHERE User = 'root';
FLUSH PRIVILEGES;
UPDATE user SET Password = PASSWORD('NewPass123!') WHERE User = 'root';
FLUSH PRIVILEGES;skip-grant-tables,重啟服務:systemctl restart mysqldmysql -u root -p'NewPass123!'net stop mysqlbin,跳過權限啟動:mysqld --skip-grant-tables --consolemysql -u rootmysqld),正常啟動服務:net start mysql8.0 預設使用 caching_sha2_password 插件,若客戶端不支援,需同步指定:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'NewPass123!';
FLUSH PRIVILEGES;
若無法修改 my.cnf,可在 GRUB 啟動參數加入 init=/bin/bash 進入單用戶模式,掛載根分區為可寫,直接編輯 my.cnf 或用 mysqladmin 修改密碼。
mysql_secure_installation 移除匿名帳號、禁止 root 遠端登入、移除 test 資料庫。