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.
- Kill the running server
- Create a text file with the following contents:
UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
FLUSH PRIVILEGES; - Run the mysql_safe binary with the
--init-file
option pointing to your new file like this:
mysqld_safe --init-file=/path/to/file &
- Delete the init file you created.
- Stop and start MySQL normally.
Did you find this post useful or have questions or comments? Please let me know!