Keywords: MySQL | Password Reset | Root Account | Hashing | Privilege Management
Abstract: This article provides an in-depth exploration of common issues encountered during MySQL root password reset processes, with particular focus on the critical step of password hashing. Through analysis of real user cases, it details the correct methods for password setting after using --skip-grant-tables mode, including the use of ALTER USER statements, the importance of FLUSH PRIVILEGES, and compatibility considerations across different MySQL versions. The article also offers complete operational workflows and security recommendations to help users avoid common password reset pitfalls.
Problem Background and Core Challenges
In MySQL database management practice, root account password reset is a common but error-prone operation. Many users encounter "Access denied" errors even when following standard procedures. This situation typically stems from misunderstandings about password handling mechanisms, particularly the oversight of the crucial password hashing step.
The Importance of Password Hashing
MySQL does not store plain text passwords directly but instead uses hash algorithms to encrypt passwords for security. This security mechanism requires all password setting operations to explicitly call hash functions. From user feedback cases, the most common error is using plain text passwords directly in UPDATE statements:
-- Incorrect example: Using plain text password directly
UPDATE mysql.user SET Password='MyNewPass' WHERE User='root';
-- Correct example: Using PASSWORD() function for hashing
UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
When the PASSWORD() function is omitted, MySQL stores the plain text password directly in the database, causing hash value mismatches during subsequent authentication and resulting in access denial errors. This error is easily overlooked in emergency situations but remains a primary cause of password reset failures.
Complete Password Reset Workflow
Based on best practices and official documentation, we recommend the following complete password reset procedure:
Step 1: Enter Privileged Mode
First, stop the MySQL service and restart it in skip-grant-tables mode:
# Stop MySQL service
sudo systemctl stop mysql
# Start with skipped privilege tables
sudo mysqld_safe --skip-grant-tables &
In this mode, all users can connect to the database without passwords, but account management statements are disabled. This is a necessary precondition for password reset.
Step 2: Connect to Database and Refresh Privileges
# Connect to MySQL server (no password required)
mysql -u root
# Refresh privilege tables to enable account management
FLUSH PRIVILEGES;
Step 3: Execute Password Update Operation
Depending on the MySQL version, different statements are recommended:
-- For MySQL 5.7.6 and above, use ALTER USER
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword123!';
-- For older MySQL versions, use SET PASSWORD
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('NewPassword123!');
-- Or use UPDATE statement (ensure PASSWORD function is used)
UPDATE mysql.user SET authentication_string=PASSWORD('NewPassword123!')
WHERE User='root' AND Host='localhost';
Step 4: Complete Reset Process
# Refresh privileges again to ensure changes take effect
FLUSH PRIVILEGES;
# Exit MySQL client
EXIT;
# Stop MySQL service and restart normally
sudo pkill mysqld
sudo systemctl start mysql
Version Compatibility Considerations
Different MySQL versions have important differences in password management:
MySQL 5.7.6 and Above
These versions introduced more secure authentication mechanisms and recommend using ALTER USER statements:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
This statement automatically handles password hashing and storage, avoiding the complexity of manually calling PASSWORD() function.
MySQL Before 5.7.6
Older versions require explicit use of PASSWORD() function:
UPDATE mysql.user SET Password=PASSWORD('new_password')
WHERE User='root' AND Host='localhost';
Common Errors and Troubleshooting Methods
Error 1: Password Column Does Not Exist
In some MySQL versions, you might encounter this error:
ERROR 1054 (42S22): Unknown column 'Password' in 'field list'
This occurs because newer MySQL versions renamed the password column to authentication_string. Solution:
UPDATE mysql.user SET authentication_string=PASSWORD('new_password')
WHERE User='root' AND Host='localhost';
Error 2: Privileges Not Properly Refreshed
Even if password update succeeds, changes may not take effect immediately without FLUSH PRIVILEGES:
-- Ensure privilege refresh is executed
FLUSH PRIVILEGES;
Error 3: Hostname Mismatch
Ensure operations target the correct hostname account:
-- View all root accounts
SELECT User, Host FROM mysql.user WHERE User='root';
-- Select correct host based on actual situation
UPDATE mysql.user SET Password=PASSWORD('new_password')
WHERE User='root' AND Host='%';
Security Best Practices
Using Strong Password Policies
When resetting passwords, follow strong password principles:
-- Example: Strong password with uppercase, lowercase, numbers, and special characters
ALTER USER 'root'@'localhost' IDENTIFIED BY 'My$tr0ng_P@ssw0rd!';
Timely Restoration of Security Settings
After completing password reset, always restart MySQL service normally to ensure --skip-grant-tables mode is disabled and system security protections are restored.
Audit Log Checking
After reset operations, check MySQL error logs to confirm successful operation:
# Check MySQL error logs
tail -f /var/log/mysql/error.log
Summary and Recommendations
MySQL root password reset is a process that requires careful operation. Key points include: ensuring proper password hashing, using syntax appropriate for the current MySQL version, completely executing privilege refresh procedures, and security restoration after operations. By understanding these critical aspects, users can effectively resolve various issues encountered during password reset processes and ensure secure and stable operation of database systems.
In practical operations, it's recommended to verify procedures in test environments first before executing in production environments. Additionally, regularly backup important data to prevent data loss due to operational errors. Mastering these skills is essential professional capability for database administrators.