以 --skip-grant-tables 參數啟動 MySQL,跳過權限驗證,以空密碼 root 登入修改密碼,再重啟正常模式。
# Windows
net stop mysql
# Linux
systemctl stop mysqld
# 或
service mysql stop
# Windows(於 bin 目錄下)
mysqld --skip-grant-tables
# Linux
mysqld_safe --skip-grant-tables &
mysql -u root
USE mysql;
UPDATE user SET authentication_string=PASSWORD('新密碼') WHERE User='root';
FLUSH PRIVILEGES;
QUIT;
MySQL 5.7+ 欄位為authentication_string,舊版為Password。
# Windows
net start mysql
# Linux
systemctl start mysqld
# 1. 跳過權限啟動 mysqld
mysqld --skip-grant-tables
# 2. 另開視窗執行
mysqladmin -u root flush-privileges password "newpassword"
mysqladmin -u root -p shutdown # 會提示輸入新密碼
# 3. 正常啟動 MySQL 服務
Windows: my.ini / Linux: /etc/my.cnf),在 [mysqld] 區段加入:skip-grant-tablesskip-grant-tables,重啟服務# 若 mysqld 正在運行,先終止
killall -TERM mysqld
# 安全模式啟動
mysqld_safe --skip-grant-tables &
# 登入修改
mysql -u root
UPDATE mysql.user SET authentication_string=PASSWORD('新密碼') WHERE User='root';
FLUSH PRIVILEGES;
# 結束進程並正常啟動
killall mysqld
systemctl start mysqld