Home > Database > Mysql Tutorial > Detailed introduction on how to reset the root password of MySQL or MariaDB in Linux (picture)

Detailed introduction on how to reset the root password of MySQL or MariaDB in Linux (picture)

黄舟
Release: 2017-03-23 13:21:12
Original
1019 people have browsed it

This article mainly introduces how to reset the root password of MySQL or MariaDB in Linux. It has certain reference value. Interested friends can refer to it.

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

One of these is to set a password for the database root account - you must keep this 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 database administrator is replaced or laid off!). We will explain how to reset or recover the root password for MySQL or MariaDB in Linux.

Although we will use MariaDB in this article, these instructions also apply to MySQL.

Restore the root password of MySQL or MariaDB

Before starting, stop the database service and check the service status, we should be able to see To the previously set environment variables:

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

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 &
Copy after login

This allows you to connect to the database without the root password (you may need to switch to another terminal):

# mysql -u root
Copy after login

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;
Copy after login

Finally, stop the service, unset the environment variables 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
Copy after login

This will allow the previous changes to take effect, allowing you to use the new password to connect to the database.

Summarize

The above is the detailed content of Detailed introduction on how to reset the root password of MySQL or MariaDB in Linux (picture). For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template