--skip-grant-tables 免密登入改密(通用、推薦)systemctl stop mysqld 或 net stop mysql。mysqld --skip-grant-tables --user=mysql &(Linux)mysqld --skip-grant-tables(Windows CMD)。mysql -u root。USE mysql;
UPDATE user SET authentication_string=PASSWORD('newpass') WHERE User='root';
-- MySQL 5.7+ 欄位為 authentication_string
FLUSH PRIVILEGES;
init.sql:
USE mysql;
UPDATE user SET authentication_string=PASSWORD('newpass') WHERE User='root';
FLUSH PRIVILEGES;
mysqld --init-file=/path/to/init.sql --user=mysql &。--init-file 正常重啟。user 表檔案(特殊場景)前提:擁有另一台已知密碼的同版本 MySQL(如開發機)。
mysql 資料庫下 user.frm、user.MYD、user.MYI(或 user.ibd InnoDB)。chown -R mysql:mysql /var/lib/mysql/mysql/user.*
chmod 660 /var/lib/mysql/mysql/user.*
FLUSH PRIVILEGES。DELETE FROM mysql.user WHERE User='';
DELETE FROM mysql.user WHERE Host='%';
FLUSH PRIVILEGES;
風險:版本不一致、表結構不同會導致權限系統錯亂。僅限同版本、同結構環境。
mysqld --skip-grant-tables。MySQL 5.7 起密碼欄位為 authentication_string,Password 已移除。8.0 預設驗證插件為 caching_sha2_password,若客戶端不支援需指定 mysql_native_password:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'newpass';