News
Faster Speeds: Bandwidth for vStack Servers in Kazakhstan Increased to 200 Mbps
VB
November 13 2022
Updated June 29 2025

How to Reset MySQL and MariaDB Root Password on Ubuntu 20.04 LTS — Step-by-Step Guide

Databases Ubuntu

What is MySQL and MariaDB

MySQL and MariaDB are two of the most widely used relational database management systems (RDBMS) for handling SQL-based databases.
They are commonly used in a variety of environments — from powering dynamic websites and web applications to supporting machine learning pipelines, data analytics platforms, and software development projects.
Thanks to their speed, reliability, and open-source nature, both systems remain a top choice for developers and system administrators worldwide.

Why password reset may be needed

The reasons are obvious - current password may be "dropped" after database service upgrade, you can accidentally forget it, your personal passwords storage could be lost etc.

How to reset root password of MySQL and MariaDB services

To set new database service root password (it is NOT "built-in" system root!) please follow rurther steps:

Login to your server as privileged user;

Stop the service;

service mysql stop # for MySQL
service mariadb stop # for MariaDB

pic1

Allow temporary "passwordless" authentication and start the service:

cat << EOF >> /etc/mysql/my.cnf
[mysqld]
skip-grant-tables
EOF
service mysql start

pic2

Authenticate into the database service and set the new password:

mysql
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewStr0ngPassword';
exit

pic3

Remove skip-grant-tables directive from configuration file and restart the service:

service mysql stop; sed -i -e '$d' /etc/mysql/my.cnf && sed -i -e '$d' /etc/mysql/my.cnf; service mysql start

pic4

NOTE: I run sed -i -e '$d' twice cause need to delete two last file rows.

Checking and tuning

To check the result just run:

mysql -uroot -p<your_new_password>

pic5

As you see, authorization is successful. To make your further life more comfortable, it is possible to save this password to the special file in your home directory. It will allow you to authenticate without typing password each time:

cat << EOF >> ~/.my.cnf

[client]
user = root
password = <your_new_password>
EOF

pic6

Conclusion

Now you know how to reset the root password for MySQL or MariaDB, why this operation might be necessary, and how to streamline daily authentication for easier access.
Whether you've lost administrative credentials or need to strengthen security policies, understanding the password reset process ensures reliable and secure database management.
Additionally, we covered ways to simplify routine access using .my.cnf or secure alternative methods.

Vote:
5 out of 5
Аverage rating : 5
Rated by: 4
1101 CT Amsterdam The Netherlands, Herikerbergweg 292
+31 20 262-58-98
700 300
ITGLOBAL.COM NL
700 300

You might also like...

We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.