How to Reset MySQL or MariaDB Root Password in Linux

Summary: If you are setting up a MySQL or MariaDB database for the first time, you can run mysql_secure_installation directly to implement basic security settings. One of them is to set the password for the database root account - you must keep it private and only use it when absolutely necessary.

If you are setting up a MySQL or MariaDB database for the first time, you can run mysql_secure_installation directly to implement basic security settings.

One of them is to set the password for the database root account - you must keep it private and only use it when absolutely necessary. This article will come in handy if you forget your password or need to reset it (for example, when the DBA changes or gets laid off!). We'll explain how to reset or recover the root password for MySQL or MariaDB in Linux.

Suggested Read: Change the root password for MySQL or MariaDB.

While we'll be using MariaDB in this article, these instructions also apply to MySQL.

Recover MySQL or MariaDB root
password Before starting, stop the database service and check the service status, we should see the environment variables set earlier:

------------- SystemD ------ -------
# systemctl stop mariadb
------------- SysVinit -------------
# /etc/init.d/mysqld stop
Next , start the service with the --skip-grant-tables option:

------------- 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 allows you to connect to the database without a root password (you may need to switch to another terminal):

# mysql -u root
Next, follow the steps listed 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 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 puts the previous changes into effect, allowing you to connect to the database with the new password.

The original release time is: 2017-03-17

This article is from the Yunqi community partner "Linux China"

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326680690&siteId=291194637