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
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 &
This allows you to connect to the database without the 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 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
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!