LLI Wiki — My Digital Knowledge Base

Reset the MySQL root password on CentOS

Reset the MySQL root password by starting the server without its grant tables, then restart it normally.

The root password of a MySQL server can be reset by starting the server with its grant tables skipped, setting a new password, and restarting the server normally. The steps below use systemd, as on CentOS.

Find the temporary password

If MySQL was installed recently, the temporary root password is written to the error log:

grep 'temporary password' /var/log/mysqld.log

Reset the password

  1. Stop the server:

    systemctl stop mysqld
    
  2. Set the environment option that skips the grant tables:

    systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
    
  3. Start the server with this option:

    systemctl start mysqld
    
  4. Connect as root, without a password:

    mysql -u root
    
  5. Change the password of the root user:

    UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPassword')
    WHERE User = 'root' AND Host = 'localhost';
    FLUSH PRIVILEGES;
    QUIT;
    
  6. Stop the server:

    systemctl stop mysqld
    
  7. Remove the environment option, so that the server starts normally:

    systemctl unset-environment MYSQLD_OPTS
    
  8. Start the server:

    systemctl start mysqld
    
  9. Check the new password:

    mysql -u root -p
    

Notes

Mastodon LLIMastodon SocialFosstodon