Update MySQL/MariaDB database user password

Created: 2022-04-15

From time to time, it is advised to change user's passwords as a security measure.

If you have reason to believe your password has been stolen, you should change it, and make sure you change it on all of your accounts where you use the same or a similar password.
...
If you think you might have just given your password to a phishing website, change it. If your current password is weak, change it. If it will make you feel better or if you just feel like it’s time for a change, then by all means go ahead and change your password.

-- Time to rethink mandatory password changes

  1. Login to MySQL/MariaDB

Note: This command must be run as root, hence the sudo in front of it.

sudo mysql -u root -p

and press ENTER

  1. List all users
SELECT User, Host FROM mysql.user;

Sample output:

+---------------------+-----------+
| User                | Host      |
+---------------------+-----------+
| mysql_user          | localhost |
| debian-sys-maint    | localhost |
| mysql.infoschema    | localhost |
| mysql.session       | localhost |
| mysql.sys           | localhost |
| root                | localhost |
+---------------------+-----------+
9 rows in set (0.00 sec)

In our case, the user is mysql_user.

  1. Update the user password
ALTER USER 'mysql_user'@'localhost' IDENTIFIED BY 'new_mysql_password';

Make sure you change the mysql_user with a user that you have and the new_mysql_password with a strong password.

Resources: