You are on page 1of 7

How to Reset the MySQL Root Password

Updated 
Feb 12, 2020 • 3 min read

Have you forgotten your MySQL root password? Don’t worry, it happens to all of us.

In this article, we will show you how to reset the MySQL root password from the command line.

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:

$ mysql --version

If you have MySQL installed in your system the output will look something like this:

Output
mysql Ver 14.14 Distrib 5.7.22, for Linux (x86_64) using EditLine wrapper

Or output like this for MariaDB:

Output

mysql Ver 15.1 Distrib 10.1.33-MariaDB, for debian-linux-gnu (x86_64) using

Be sure to make a note of which version of MySQL or MariaDB you’re running.

How to Reset MySQL or MariaDB Root Password


Follow these steps to reset your MySQL/MariaDB root password:

1. Stop the MySQL/MariaDB service

To change the root password first, you need to stop the MySQL server. To do so type the
following command:

$ sudo systemctl stop mysql

2. Start the MySQL/MariaDB server without loading the grant tables

Start the database server without loading the grant tables:

Copy
$ sudo mysqld_safe --skip-grant-tables &

The ampersand & at the end of the command above will cause the program to run in the
background
, so you can continue to use the shell.

When the --skip-grant-tables option is used, anyone can to connect to the database server
without a password and with all privileges granted.

3. Log in to the MySQL shell


Now you can connect to the database server as the root user:

$ mysql -u root

4. Set a new root password

Run the following commands if you run MySQL 5.7.6 and later or MariaDB 10.1.20 and later:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MY_NEW_PASSWORD';


mysql> FLUSH PRIVILEGES;

If ALTER USER statement doesn’t work for you, try to modify the user table directly:

mysql> UPDATE mysql.user SET authentication_string = PASSWORD('MY_NEW_PASSW


mysql> WHERE User = 'root' AND Host = 'localhost';
mysql> FLUSH PRIVILEGES;

Run the following commands if you have MySQL 5.7.5 and earlier or MariaDB 10.1.20 and
earlier:

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MY_NEW_PASSWORD');


mysql> FLUSH PRIVILEGES;

In both cases if all goes well, you should see the following output:

Output
Query OK, 0 rows affected (0.00 sec)

5. Stop and Start the database server normally

Now that the root password is set, stop the database server and start it normally:
$ mysqladmin -u root -p shutdown

You will be prompted to enter the new root password.

Start the database server normally:

For MySQL, type:

$ sudo systemctl start mysql

For MariaDB, type:

$ sudo systemctl start mariadb

6. 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 in to
your database server.

Conclusion
We’ve shown you how to reset your MySQL/MariaDB root password. Make sure your new root
password is strong and secure and keep it in a safe place.

The instructions in this guide should work with any modern Linux distribution such as Ubuntu
18.04, Debian 10 and CentOS 8.

Feel free to leave a comment if you have any questions.

mysql mariadb
If you like our content, please consider buying us a coffee.

Thank you for your support!

BUY ME A COFFEE

Sign up to our newsletter and get our latest tutorials and news straight
to your mailbox.

Your email...

Subscribe

We’ll never share your email address or spam you.

Related Articles

DEC 23, 2018


How to Install WordPress with Apache on Ubuntu 18.04
DEC 12, 2018
How to Install Magento 2 on Ubuntu 18.04

NOV 27, 2018


How to Install WordPress with Nginx on CentOS 7
Show comments (8)

© 2022 Linuxize.com

Privacy Policy
Terms
Contact
Advertise on Linuxize

You might also like