- 以系統管理員/root 身分登入作業系統
- 停止 MySQL 服務
# Windows
net stop mysql
# Linux
systemctl stop mysqld
- 跳過授權表啟動 mysqld
# Windows (命令提示字元 以管理員執行)
cd C:\mysql\bin
mysqld --skip-grant-tables --console
# Linux (背景執行)
mysqld_safe --skip-grant-tables --skip-networking &
# --skip-networking 防止遠端連線進入無密碼狀態
- 開新終端機,免密碼登入並修改密碼
mysql -u root
-- 在 mysql> 提示符下
FLUSH PRIVILEGES; -- 必須先執行,載入授權表以便修改
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewStrongPassword123!';
-- 或舊版
UPDATE mysql.user SET authentication_string=PASSWORD('NewPass') WHERE User='root';
FLUSH PRIVILEGES;
EXIT;
- 正常重啟 MySQL
# Windows
net stop mysql (關閉 --skip-grant-tables 的進程)
net start mysql
# Linux
pkill mysqld
systemctl start mysqld
- 驗證新密碼
mysql -u root -p'NewStrongPassword123!'