Read in 3 minutes
How to reset a MySQL root password
Forgotten your MySQL root password? Don’t worry, it happens to all of us.
In this tutorial we will show you how to reset a MySQL root password in case you have forgotten it. This guide should work with any modern Linux distribution such as Ubuntu 18.04 and CentOS 7.
Before continuing with the steps below, make sure you are logged into your server as a user with sudo privileges.
Identify the Server Version
Depending on the MySQL or MariaDB server version you are running on your system, you will need to use different commands to recover the root password.
You can find your server version by issuing the following command:
If you have MySQL installed in your system the output will look something like this:
mysql Ver 14.14 Distrib 5.7.22, for Linux (x86_64) using EditLine wrapper
If you have MariaDB installed in your system the output will look something like this:
mysql Ver 15.1 Distrib 10.1.33-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
Be sure to make note of which version of MySQL or MariaDB you’re running.
How do I reset MySQL or MariaDB root password ?
Follow these steps to reset your MySQL/MariaDB root password:
Stop the MySQL or the MariaDB service
To change the root password, first we need to stop the MySQL server. To do so type the following command:
sudo systemctl stop mysql
Start the MySQL or the MariaDB server without loading the grant tables
--skip-grant-tablesoption is enabled anyone can to connect to the database server without a password and with all privileges. To start the database server without loading the grant tables type:
sudo mysqld_safe --skip-grant-tables &
&at the end of the command above will cause the program to run in the background, so we can continue to use the shell.
Log into the MySQL shell
Now you can connect to the database server as the root user, without being prompted the password:
mysql -u root
Set a new root password
Run the following commands if you have MySQL 5.7.6 and later or MariaDB 10.1.20 and later:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MY_NEW_PASSWORD'; FLUSH PRIVILEGES;
ALTER USERstatement doesn’t work for you, try to modify the user table directly:
UPDATE mysql.user SET authentication_string = PASSWORD('MY_NEW_PASSWORD') WHERE User = 'root' AND Host = 'localhost'; FLUSH PRIVILEGES;
Run the following commands if you have MySQL 5.7.5 and earlier or MariaDB 10.1.20 and earlier:
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MY_NEW_PASSWORD'); FLUSH PRIVILEGES;
In both cases if everything went well, you should see the following output:
Query OK, 0 rows affected (0.00 sec)
Stop and Start the Database Server Normally
Now that we have reset the root password, we can stop the database server and start it normally.
Stop the database server using the following command, you will be prompted to enter the new root password:
mysqladmin -u root -p shutdown
Finally start the database server normally.
For MySQL, type:
sudo systemctl start mysql
For MariaDB, type:
sudo systemctl start mariadb
Verify the password
To verify that the new root password has been applied correctly type:
mysql -u root -p
You will be prompted to enter the new root password. Enter it, and you should be logged into your database server.
In this tutorial, you learned how to reset your MySQL or MariaDB root password. Make sure your new root password is strong and secure and keep it in a safe place.
If you want to learn how to manage your MySQL user accounts and databases you can now check this tutorial.
Feel free to leave a comment if you have any questions.