MySQL reset root password

How to reset a root MySQL password


This time I share with you the faster and more secure method to reset the root password of MySQL.

This method is faster because the downtime is between 1 or 2 seconds (MySQL restart time) and it is more secure because the mysqld is not started without grants on the tables.

The steps are:

  1. Create text file /var/lib/mysql/mysql-init with the sintaxis to reset the password for user root:
vim /var/lib/mysql/mysql-init
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_password');
  1. Add under the [mysqld] stanza on the file /etc/my.cnf:
  1. Restart the mysqld service:
service mysqld restart
  1. Remove the init-file line from /etc/my.cnf

  2. Remove /var/lib/mysql/mysql-init

rm /var/lib/mysql/mysql-init

And after that, you can access again to your mysql instance.


Luis Cacho
Luis Cacho
Security Systems Administrator III

Security Systems Administrator at Rackspace | Kubernetes Enthusiast | Ansible Ninja