生產環境禁用 skip-grant-tables 後重啟屬高風險操作,需在維護窗口、備份驗證後執行。本文提供標準化重置流程。
net stop mysql (或服務名 MySQL57)。cd /d "C:\Program Files\MySQL\MySQL Server 5.7\bin"。mysqld --skip-grant-tables --console (保持窗口開啟)。mysql -uroot -p (直接回車)。authentication_string 列):USE mysql;
UPDATE user SET authentication_string=PASSWORD('NewPass123!') WHERE User='root' AND Host='localhost';
FLUSH PRIVILEGES;
--skip-grant-tables 窗口 (Ctrl+C),正常啟動服務:net start mysql。mysql -uroot -pNewPass123!。systemctl stop mysqld。mkdir -p /var/run/mysqld
chown mysql:mysql /var/run/mysqld
cat > /etc/my.cnf.d/skip-grant.cnf <<'EOF'
[mysqld]
skip-grant-tables
skip-networking
EOF
systemctl start mysqld。mysql -uroot。USE mysql;
UPDATE user SET authentication_string=PASSWORD('NewPass123!') WHERE User='root';
FLUSH PRIVILEGES;
rm /etc/my.cnf.d/skip-grant.cnf。systemctl restart mysqld && mysql -uroot -pNewPass123!。-- 8.0 使用 caching_sha2_password,需指定插件
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'NewPass123!';
-- 或
UPDATE user SET authentication_string=NULL, plugin='mysql_native_password' WHERE User='root';
FLUSH PRIVILEGES;
mysql.user 表。mysql_secure_installation。