Change or recover MySQL root password

To Change a Known Password
If you know the current password, use one of these methods to reset it.  If you do not know, skip ahead to the next section.

Using mysqladmin if there is no password:

$ mysqladmin -u root password <new password>

Using mysqladmin if there is a password:
$ mysqladmin -u root -p <old password> <new password>

Using mysql shell if there is no password
mysql -u root
mysql> update user set password=PASSWORD("<new password>") where User='root';
mysql> flush privileges;
mysql> quit

Using mysql shell if there is a password
mysql -u root -p
Password: <old password>
mysql> update user set password=PASSWORD("<new password>") where User='root';
mysql> flush privileges;
mysql> quit

Recover/reset the password if you don’t know it
There is no way to recover the password if you don’t know it, but you can reset it to something new.
You must have root on the box for this.

  1. Kill the running server
  2. Create a text file with the following contents:
    UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
    FLUSH PRIVILEGES;
  3. Run the mysql_safe binary with the --init-file option pointing to your new file like this:
    mysqld_safe --init-file=/path/to/file &
  4. Delete the init file you created.
  5. Stop and start MySQL normally.

Did you find this post useful or have questions or comments?  Please let me know!

This entry was posted in How Tos, mysql and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *