Tutorials

How to change a MySQL user password?

Malo Paletou
· 1 min read
Send by email

MySQL is one of the most popular database management systems (DBMS). In most cases, it is installed via package managers such as Debian, Ubuntu, or RedHat and configured with default settings.

By default, you can connect as root using the process socket with the following command (from the root user):

mysql -u root

Let’s go over how to change the password for MySQL users, whether it’s a standard account or the root account.


Changing a standard MySQL user password

To modify a MySQL user’s password, use the following command:

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

Define a new standard MySQL user password

Listing MySQL users

If you want to display the list of existing users, run this command:

SELECT user, host FROM mysql.user;

List MySQL users

💡
Note: MySQL treats each user as a user/host pair. For example, root@127.0.0.1 can have a different password than root@%.

Changing the MySQL root password

To change the MySQL root password, run the following command:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_root_password');

Set a new password for root user

Once the password is changed, apply the new settings immediately with:

FLUSH PRIVILEGES;

Force privileges reload


Other Useful MySQL Commands

Delete a MySQL user

DROP USER 'username'@'localhost';

Delete a MySQL user

List your MySQL databases

SHOW DATABASES;

List your databases


Going further

Want to deepen your knowledge of MySQL management? Check out our complete guide on using mysqldump to efficiently back up and restore your MySQL databases.

At Datashelter, we are experts in data backup solutions. Feel free to contact us for a free demo or personalized support in defining the best backup strategy for your company or your end clients.