how-to-change-mysql-root-password
# How to Change MySQL Root Password: Complete Guide
Changing the MySQL root password is a common administrative task that ensures database security. This guide covers multiple methods for changing or resetting the root password in MySQL 5.7 and 8.0.
## Method 1: Change Password (When You Know Current Password)
This is the recommended method when you have access to MySQL with the current root password.
### For MySQL 5.7 and Later
**Step 1: Log in to MySQL**
```bash
mysql -u root -p
Step 2: Change the password using ALTER USER
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword';
Step 3: Flush privileges
FLUSH PRIVILEGES;
Step 4: Exit MySQL
EXIT;
For MySQL 8.0
MySQL 8.0 uses caching_sha2_password as the default authentication plugin. Use this command:
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'NewPassword';
Method 2: Reset Password (Forgotten Password Recovery)
If you've forgotten the root password, you can reset it using safe mode.
Linux/macOS Systems
Step 1: Stop MySQL service
sudo systemctl stop mysql
Step 2: Start MySQL in safe mode
sudo mysqld_safe --skip-grant-tables &
Step 3: Log in without password
mysql -u root
Step 4: Reset the password
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword';
EXIT;
Step 5: Restart MySQL normally
sudo systemctl restart mysql
Windows Systems
Step 1: Stop MySQL service
net stop MySQL80
Step 2: Start MySQL in safe mode
Open Command Prompt as Administrator and run:
mysqld --skip-grant-tables
Step 3: Open another Command Prompt and log in
mysql -u root
Step 4: Reset the password
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword';
EXIT;
Step 5: Restart MySQL service
Press Ctrl+C in the first window, then:
net start MySQL80
Legacy Method (MySQL 5.6 and Earlier)
For older MySQL versions, use the UPDATE statement:
USE mysql;
UPDATE user SET password=PASSWORD('NewPassword') WHERE User='root';
FLUSH PRIVILEGES;
Verify Password Change
Test the new password by logging in:
mysql -u root -p
Enter your new password when prompted.
Best Practices
- Use strong passwords: Combine uppercase, lowercase, numbers, and special characters
- Store credentials securely: Use password managers instead of plain text files
- Remove skip-grant-tables: Always restart MySQL normally after password recovery
- Create backup admin accounts: Have alternative admin accounts for emergency access
- Limit root access: Consider restricting root login to localhost only
Common Issues
Authentication Plugin Error
If you encounter authentication errors, specify the authentication plugin explicitly:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'NewPassword';
Permission Denied
Make sure you're running commands with appropriate system privileges (sudo on Linux, Administrator on Windows).
Conclusion
Changing the MySQL root password is straightforward when following the correct procedure for your MySQL version. Regular password updates enhance database security and prevent unauthorized access. Always document your recovery procedures and test them in a staging environment before applying to production systems.
```