How to Change MySQL and MariaDB Root Password

This brief tutorial shows users and students an easy way to change the root user password for MySQL and MariaDB database servers. This guide will only work if you already know the current root user password and you only want to change it to something different.

If you don’t already know the root user password, then you may want to check out our other tutorials on resetting the root password for these database servers.

One thing you may already know is during MySQL installation, you may be prompted to set a root password for MySQL. This will be the current root password.

For MariaDB, users may have to run sudo  mysql_secure_installation after installing to set the root user password.

One last thing to remember is the latest versions of MySQL and MariaDB require that user run the sudo mysql command in order to login as root. So if you need to login to the database server, you must now run the commands below:

sudo mysql -u root -p

Now that you’re logged in, continue below to learn how to change the root password.

Step 1: Logging in as root

First, make sure the database server is running, then login as the root user by running the commands below

sudo mysql -u root -p

When prompted for a password, type the current root password.

Step 2: Change the root password

After logging, run the commands below to change the current root password to a new one.

USE mysql;
UPDATE user SET password=PASSWORD('new_password_here') WHERE User='root' AND Host ='localhost';
FLUSH PRIVILEGES;
exit

Replace the highlighted text new_password_here with the new password you want to use. After running the above commands, should go and test the new password by trying to login again.

Summary:

This posts shows you how to change MySQL or MariaDB root password using the command terminal. It will only work if you already know the current password. If you don’t, then you’ll want to read some of our other tutorials on resetting the root password for these servers.

Enjoy and please come back soon.

To reset your forgotten root password, read the post below

How to Reset MySQL or MariaDB Root Password