You could earn up to $300 by adding new articles!

Get Started Now

Reset MySQL Root Password on Debian/Ubuntu

Published on: Tue, Oct 14, 2014 at 4:13 am EST

If you have forgotten your MySQL root password, you can reset it by following the steps in this article. The process is quite simple and works on these operating systems:

  • Ubuntu 16.04
  • Ubuntu 18.04
  • Debian 8
  • Debian 9

Determining your database

The table below will tell you which database software is available in the OS repository by default.

|    Operating System    |  MySQL/MariaDB  |
| ---------------------- | --------------- |
| Ubuntu 16.04           | MariaDB         |
| Ubuntu 18.04           | MariaDB         |
| Debian 8               | MySQL           |
| Debian 9               | MySQL           |

If you are still unsure of the database software you have installed, perform the following command:

mysql -V

If you see mysql Ver xxx Distrib x.x.x-MariaDB, for example, you are running MariaDB. Otherwise, you are running MySQL.

If you have MySQL installed, please follow the guide below to reset your password. If you have MariaDB installed, scroll to the "Resetting your MariaDB password" section.

Resetting your MySQL password

First, we must stop the mysql service and grant password-less access:

service mysql stop
mysqld_safe --skip-grant-tables &

Once the mysql service stops, we can now connect to our database server by performing the following command:

mysql -u root mysql

Now, enter the following into the SQL terminal:

UPDATE user SET password=PASSWORD('YOUR_NEW_PASSWORD') WHERE user='root';
FLUSH PRIVILEGES;

You can now enter the following into the SQL terminal:

exit

You may now restart the mysql service:

service mysql restart

Resetting your MariaDB password

Since MariaDB is (essentially) the same software, the commands are similar. Like the previous section, we need to stop the mariadb process and grant password-less access before continuing:

service mariadb stop
mysqld_safe --skip-grant-tables &

Now, we can connect to our database server:

mysql -u root mysql

Once you see the SQL terminal, enter the following:

UPDATE user SET password=PASSWORD('YOUR_NEW_PASSWORD') WHERE user='root';
FLUSH PRIVILEGES;

Exit and restart MariaDB:

exit
service mariadb restart

Conclusion

Congratulations! You've successfully reset your MySQL/MariaDB server password.

Want to contribute ?

You could earn up to $300 by adding new articles!

Get started in the SSD Cloud!