Question
How do I manually reset the MySQL root password with an init file?
Answer
This article walks you through manually resetting the MySQL root password if the password in the /root/.my.cnf becomes desynced.
Note: The steps below should only be used if the WHM interface can not be used to reset the password. This is best performed by a qualified system administrator, and cPanel support staff are not able to perform these steps for you.
How to change the root MySQL user password
- Log in to WHM, select Service Manager under the Service Configuration section, and disable monitoring for the MySQL service.
- Connect to the server via SSH as the root user.
-
Stop the MySQL service:
# /scripts/restartsrv_mysql --stop
-
Create a text file in
/var/lib/mysqlcalledmysql-init:# touch /var/lib/mysql/mysql-init
-
Open the file in a text editor and add the following text to the file, where MyNewPass represents the password you would like to use:
# ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
-
Update the password in
/root/.my.cnfto the password used in this statement:# [root@server]# grep -i pass /root/.my.cnf
password="MyNewPass" -
Change the ownership of the file to ensure there are no issues reading the file:
# chown -v mysql. /var/lib/mysql/mysql-init
-
Start MySQL manually using this command:
# mysqld -u mysql --init-file=/var/lib/mysql/mysql-init &
-
Now that the password has been updated shut down MySQL safely using this command:
# mysqladmin -u root shutdown
-
Remove the init file:
# /bin/rm -vi /var/lib/mysql/mysql-init
-
Restart MySQL normally:
# /scripts/restartsrv_mysql
- In the WHM interface, re-enable MySQL service monitoring in Service Manager / Service Configuration.
When using MySQL 5.7:
It may be necessary to use the following statement in place of the ALTER statement shown above:
CONFIG_TEXT: UPDATE mysql.user
SET authentication_string = PASSWORD('MyNewPass'), password_expired = 'N'
WHERE User = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES;
Warning: For security purposes, please do not utilize MyNewPass as the new password on your server. This is an example only.
Additional Resources
More information on these steps can be found in the official MySQL documentation here:
Comments
0 comments
Article is closed for comments.