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');
  2. Add under the [mysqld] stanza on the file /etc/my.cnf:

  3. Restart the mysqld service:

    service mysqld restart
  4. Remove the init-file line from /etc/my.cnf

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

    rm /var/lib/mysql/mysql-init

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


Edit this page

Luis Cacho
Luis Cacho
Senior Container Infrastructure Consultant

Senior Container Infrastructure Consultant at Red Hat | DevOps Practitioner | Kubernetes Enthusiast | Ansible Ninja | Data Science Noob