How to Reset MySQL or MariaDB Root Password in Linux

If you are setting up a MySQL or MariaDB database server for the first time, chances are you will be running mysql_secure_installation soon afterwards to implement basic security settings. One of these settings is...

Spread the love

If you are setting up a MySQL or MariaDB database server for the first time, chances are you will be running mysql_secure_installation soon afterwards to implement basic security settings.

One of these settings is the password for the database root account – which you must keep private and use only when strictly required. If you forget the password or need to reset it (for example, when a database administrator changes roles – or is laid off!).

Suggested Read: Change MySQL or MariaDB Root Password

This article will come in handy. We will explain how to reset or recover forgottent MySQL or MariaDB root password in Linux.

Although we will use a MariaDB server in this article, the instructions should work for MySQL as well.

Recover MySQL or MariaDB root Password


To begin, stop the database service and check the service status, we should see the environment variable we set previously:

------------- SystemD ------------- # systemctl stop mariadb
------------- SysVinit -------------
# /etc/init.d/mysqld stop

Next, start the service with --skip-grant-tables:

------------- SystemD ------------- # systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
# systemctl start mariadb
# systemctl status mariadb
------------- SysVinit -------------
# mysqld_safe --skip-grant-tables &
Start MySQL/MariaDB with Skip Tables

Start MySQL/MariaDB with Skip Tables

This will allow you to connect to the database server as root without a password (you may need to switch to a different terminal to do so):

# mysql -u root

From then on, follow the steps outlined below.

MariaDB [(none)]> USE mysql;
MariaDB [(none)]> UPDATE user SET password=PASSWORD('YourNewPasswordHere') WHERE User='root' AND Host = 'localhost';
MariaDB [(none)]> FLUSH PRIVILEGES;

Finally, stop the service, unset the environment variable and start the service once again:

------------- SystemD ------------- # systemctl stop mariadb
# systemctl unset-environment MYSQLD_OPTS
# systemctl start mariadb
------------- SysVinit -------------
# /etc/init.d/mysql stop
# /etc/init.d/mysql start

This will cause the previous changes to take effect, allowing you to connect to the database server using the new password.

Summary

In this article we have discussed how to reset the MariaDB / MySQL root password. As always, feel free to use the comment form below to drop us a note if you have any questions or feedback. We look forward to hearing from you!

Facebook Comments

More Stuff

Screenlets: An Amazing Tool to Add Desktop Gadgets/Widgets in Linu... Screenlets is an application software released under GNU GPL. The same name screenlets refers to the engine as well as widget that runs on it. It was ...
Google Wants China. Will Chinese Users Want Google? Google CEO Sundar Pichai was upbeat Monday when he told WIRED about internal tests of a censored search engine designed to win approval from Chinese o...
The Good and the Bad of .NET Framework Programming .NET is a software development framework and ecosystem designed and supported by Microsoft to allow for easy desktop and web application engineering....
Devs used solar-powered radios to complete first ‘off-grid’ crypto... A group of developers claim to have performed a solar-powered cryptocurrency transaction, using shortwave radios and blockchain tech. While it mig...
Spread the love

Posted by News Monkey