涵蓋已知密碼的 3 種標準修改方式,與完全忘記密碼時的 skip-grant-tables 緊急重置流程。
mysqladmin -u root -p'old_pass' password 'new_pass'
# 或交互式
mysqladmin -u root -p password 'new_pass'
mysql -u root -p
UPDATE mysql.user SET authentication_string=PASSWORD('new_pass') WHERE User='root';
FLUSH PRIVILEGES;
MySQL 5.7+ 欄位為 authentication_string,5.6 為 password。
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_pass');
-- 5.7.6+ 也支援
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_pass';
service mysqld stop / net stop MySQLmysqld_safe --skip-grant-tables --skip-networking &
# Windows: mysqld --console --skip-grant-tables --skip-networking
mysql -u rootUSE mysql;
UPDATE user SET authentication_string=PASSWORD('new_pass') WHERE User='root';
FLUSH PRIVILEGES;
service mysqld restart (移除 --skip-grant-tables)root@%,僅允許 root@localhost 或 127.0.0.1。validate_password 插件強制策略。