Easily modify the Root password of MySQL/MariaDB under Linux

If you are installing MySQL or MariaDB for the first time, you can execute the mysql_secure_installation  script to achieve basic security settings. One of these settings is the root password for the database -- this password must be kept secret and used only when necessary. If you need to change it (for example, when the DBA changes -- or gets fired!).

Modify the root password of MySQL or MariaDB

You know the root password, but want to reset it, for that case, let's first make sure MariaDB is running:

------------- CentOS/RHEL 7 and Fedora 22+ ------------- 
# systemctl is-active mariadb
------------- CentOS/RHEL 6 and Fedora -------------
# /etc/init.d/mysqld status

Check MysQL Status

If there is no active keyword in the above command return, then the service is stopped, and you need to start the database service before proceeding to the next step:

------------- CentOS/RHEL 7 and Fedora 22+ ------------- 
# systemctl start mariadb
------------- CentOS/RHEL 6 and Fedora -------------
# /etc/init.d/mysqld start

Next, we will log into the database server as root:

# mysql -u root -p

In order to be compatible with different versions, we will use the following statement to update the user table of the mysql database. Note that you need to replace YourPasswordHere with the new password you choose for root.

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

To verify that the operation was successful, enter the following command to exit the current MariaDB session.

MariaDB [(none)]> exit;

Then, hit enter. You should now be able to connect to the server using the new password.

Modify MysQL/MariaDB Root password

summary

In this article, we explained how to change the MariaDB/MySQL root password - you may or may not know how to do this so far.

As always, if you have any questions or feedback, please feel free to leave your valuable comments or suggestions using the comment box below, we look forward to hearing from you.

Guess you like

Origin blog.csdn.net/yaxuan88521/article/details/131510240