killall -TERM mysqld
# 或 systemctl stop mysqld / service mysqld stop
mysqld_safe --skip-grant-tables --skip-networking &
# --skip-networking 防止遠程連線,安全第一
mysql -u root
USE mysql;
-- MySQL 5.7+ 欄位為 authentication_string
UPDATE user SET authentication_string=PASSWORD('NewStrongP@ss') WHERE User='root';
-- MySQL 5.6 或更舊
-- UPDATE user SET Password=PASSWORD('NewStrongP@ss') WHERE User='root';
FLUSH PRIVILEGES;
EXIT;
killall -TERM mysqld
systemctl start mysqld
# 現在可用新密碼登入:mysql -u root -p
| MySQL 版本 | 密碼欄位 | 推薦語法 |
|---|---|---|
| 8.0 | authentication_string | ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpass'; |
| 5.7 | authentication_string | ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpass'; 或 UPDATE |
| 5.6 / 5.5 | Password | SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass'); |
--skip-grant-tables。user.Host 包含實際連線來源 (localhost, 127.0.0.1, ::1, 具體 IP)。DELETE FROM mysql.user WHERE User='';。DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost','127.0.0.1','::1');。DROP DATABASE test; DELETE FROM mysql.db WHERE Db='test' OR Db='test\_%';。mysql_secure_installation 互動式加固。FLUSH PRIVILEGES; 是否執行;確認 plugin 欄位為 mysql_native_password 或 caching_sha2_password (8.0 預設)。net stop mysql → mysqld --skip-grant-tables --skip-networking → 新視窗 mysql -u root 更新 → net stop mysql (Ctrl+C 停止 --skip-grant-tables 實例) → net start mysql。mysqld --skip-grant-tables --skip-networking --user=mysql &,指定 --user、--basedir、--datadir。