How To Reset MySQL Root Password on Debian/Ubuntu & CentOS

DigitalOcean Referral Badge
Start your VPS now with FREE $100 credit.

On this tutorial we will guide you how to reset MySQL root password on Fedora/RHEL/CentOS and Debian/Ubuntu server.

This guide should work well on MySQL 5.5, MySQL 5.6, MySQL 5.7 and later versions.

In certain conditions it may require us to reset the MySQL root password, for example we want a stronger MySQL root password or maybe you forgot your MySQL database server root password.

The main purpose clearly to strengthen our MySQL server database system, make it more secure.

Reset MySQL Root Password

To reset MySQL root password on your server, you can follow this tutorial. This is very easy to do, as long as you have root privileges on your Linux server.

Just follow the guide below to change your MySQL root password.

1. Start MySQL in safe mode

First of all, start your MySQL database server in safe mode. Just run the following simple command

$ mysqld_safe --skip-grant-tables &

2. Login to database

After MySQL safe mode is active, you can now login to the MySQL database server without a root password via the SSH terminal. This is very useful if you currently forget your MySQL root password.

$ mysql -u root mysql

3. Reset root password

Now let’s reset your MySQL root password.

READ:  How To Set Correct Permissions (CHMOD) on Linux with Single Command

For MySQL 5.5 and MySQL 5.6

$ update user set password=PASSWORD("newrootpassword") where User='root';

For MySQL 5.7+

$ mysql> use mysql;
$ mysql> show tables;
$ mysql> describe user;
$ update user set authentication_string=password('newrootpassword') where user='root';

Note: Please replace newrootpassword with the actual new root password for your MySQL database server.

4. Flush privileges

Run the flush privileges command and exit from the MySQL database server.

$ quit

5. Exit from safe mode

Now we need to exit from MySQL safe mode, so we need to shutdown the MySQL database server.

$ mysqladmin -u root -p shutdown

6. Start in normal mode

The next step is we need to start your MySQL database server in normal mode as before.

* For Debian/Ubuntu server

$ service mysql start or /etc/init.d/mysql start

For Fedora/RHEL/CentOS

$ service mysqld start or /etc/init.d/mysqld start

As for Debian 8+, Ubuntu 14.04+ and CentOS 7 up to the latest version, you can use the systemctl command to start/stop/restart your database server. For example:

On Debian 8/9/10 and Ubuntu 14.04/16.04/18.04/19.04 LTS

$ systemctl restart mysql

On CentOS 7+

$ systemctl restart mysqld.service

Leave a Comment