How to Change MySQL Password Safely: Step-by-Step Guide and Best Practices

1. Introduction

MySQL is one of the most widely used open-source relational database management systems. To maintain database security, it is essential to update passwords regularly. In particular, accounts with administrative privileges are frequent targets of cyberattacks, making routine password changes crucial. In this article, we will explain step-by-step how to securely change MySQL passwords and outline best practices for strengthening overall database security.

2. Why You Need to Change MySQL Passwords

2.1 Password Changes as a Security Measure

Cyberattacks have become increasingly advanced and sophisticated, raising the risks of unauthorized database access and data breaches. By changing your MySQL password regularly, you can reduce these risks. Administrative accounts, in particular, are prime targets for attackers, so setting strong passwords and updating them frequently is strongly recommended.

2.2 Recommended Timing for Password Updates

It is recommended to change your password at least once every six months. However, for system administrators or users managing sensitive data, shorter intervals are preferable. Additionally, you must immediately change passwords when an employee leaves the organization or when there are signs of a possible password leak.

3. Things to Check Before Changing Your Password

3.1 Verifying Required Privileges

To change a password, the user must have the proper privileges. In MySQL, the root account or accounts with administrative privileges can change other users’ passwords. Regular users can also change their own passwords if the correct permissions are granted. Always verify privileges before proceeding.

3.2 Checking Your MySQL Version

The command used to change a password depends on the MySQL version. For example, MySQL 8.0 and later recommend using the ALTER USER command, while older versions often use SET PASSWORD. Check your version with the following command:

mysql --version

Some commands may not be available depending on the version, so make sure to use the correct one.

4. Methods to Change MySQL Passwords

4.1 Using the ALTER USER Command

In MySQL 8.0 and later, you can change a user’s password with the ALTER USER command. This method is the most recommended—safe and straightforward. Example:

ALTER USER 'username'@'localhost' IDENTIFIED BY 'new_password';

This updates the specified user’s password. Afterward, verify that the user can log in with the new password.

4.2 Using the SET PASSWORD Command

In MySQL 5.7 and earlier, the SET PASSWORD command is commonly used. Example:

SET PASSWORD FOR 'username'@'localhost' = PASSWORD('new_password');

Although effective for older versions, SET PASSWORD is not recommended in MySQL 8.0 and above. Use ALTER USER instead if available.

4.3 Changing Passwords via the mysqladmin Tool

For administrators familiar with the command line, mysqladmin is a convenient option. Example:

mysqladmin -u username -p password 'new_password'

You will be prompted to enter the current password. This method is also useful in local environments such as XAMPP or WAMP.

5. Best Practices for Password Management

5.1 Setting Strong Passwords

Strong passwords drastically reduce the likelihood of unauthorized access. A recommended password should be at least 12 characters long and include uppercase and lowercase letters, numbers, and special characters. For example: “P@ssw0rd!23”. Using password generators to create random strings is also a good practice.

5.2 Setting Password Expiration

MySQL allows you to enforce password expiration to ensure regular updates. For instance, you can require users to change their password every 90 days with:

ALTER USER 'username'@'localhost' PASSWORD EXPIRE INTERVAL 90 DAY;

This strengthens security by preventing the use of outdated passwords.

6. Common Issues and Solutions When Changing Passwords

6.1 Dealing with Permission Errors

If you encounter a “permission error,” it may mean the user lacks sufficient privileges. Try again as root or with an administrator account. You may also check the my.cnf configuration file to confirm permissions.

6.2 Resolving Version Mismatch Errors

Newer commands may cause errors in older MySQL versions. For example, running ALTER USER in MySQL 5.7 may fail. In such cases, use SET PASSWORD or consider upgrading MySQL to the latest version.

7. Conclusion

Changing MySQL passwords is a fundamental step in maintaining database security. Use one of the three methods discussed—ALTER USER, SET PASSWORD, or mysqladmin—depending on your version and needs. Always implement best practices such as setting strong passwords and enforcing expiration policies. Lastly, be prepared to handle common errors to ensure a smooth process.