If you are installing MySQL or MariaDB for the first time, you can run the mysql_secure_installation script to perform basic security settings. One of the settings is about the root password of the database. This password must be kept secret and only used when necessary. If you need to modify it (for example, when the database administrator changes, or gets fired!), you can do so via this script as well.
Please execute the script mysql_secure_installation to implement these security settings.
Change the root password of MySQL or MariaDB
You know the root password, but want to reset it, for this 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 in a stopped state, 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 in to 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 your new password.
Modify MysQL/MariaDB Root password
summary
In this article, we explain how to change the root password of MariaDB / MySQL - maybe you know the method we are talking about, or maybe you don't.
As usual, if you have any questions or feedback, please feel free to use the comment box below to leave your valuable comments or suggestions. We look forward to your messages.
The above is the detailed content of Easily change the Root password of MySQL/MariaDB under Linux. For more information, please follow other related articles on the PHP Chinese website!