If you’ve forgotten or need to change your root user password in MySQL, you might be wondering what steps are necessary to reset your MySQL root password. Luckily, there are couple of ways to do this.
Using the mysqladmin Command to Change root Password
If you’ve never set a root password for MySQL, the server does not require a password when connecting as root. If this is your first time setting up the root password, you can do so using the mysqladmin command from within the shell prompt using:
$ mysqladmin -u root password NEWPASSWORD
In the above instance, you would need to change “NEWPASSWORD” to whatever you would like your new password to be.
If, however, this is not your first time setting the password and you’d like to change or update the existing root password, then you would need to enter this command:
$ mysqladmin -u root -p'OLDPASSWORD' password NEWPASSWORD
For example, if your old password is “abc” and you wanted to set your new password to “123456”, you would tweak the command so that it looks like this:
$ mysqladmin -u root -p'abc' password '123456'
Changing MySQL Password (Non-Root Users)
Beyond changing the root password, you can use the mysqladmin command to change the password for non-root users as well. By replacing the “root” in the command above with the name of the user whose password you are attempting to change, you can change the password of any specific database user.
For example, if you wanted to change the password for a user named “vivek” you could do so by entering the follow command:
$ mysqladmin -u vivek -p'OLDPASSWORD' password NEWPASSWORD
Just like in the examples above, you would still need to enter the user’s old password in the space marked “OLDPASSWORD” and replace the “NEWPASSWORD” at the end of the line with whatever you desire the new password to be.
Manually Changing the root Password
As mentioned earlier, using the mysqladmin command isn’t the only way to change the root password in MySQL. For those of you who prefer a more hands on approach, you can also change the root password from within the terminal by utilizing the following series of commands.
- First, you’ll need to stop MySQL. As the root user enter the command:
root@server [~] /etc/init.d/mysqld stop
- Next, you’ll need to start MySQL server without a password. Use this command to do so:
root@server [~] mysqld_safe –skip-grant-tables &
Make sure you include the “&” at the end of this, as this will allow the command to run in the background
- Now login to MySQL using the “mysql” command:
root@server [~] mysql
- Once you have access to the mysql prompt then run the following commands:
mysql >use mysql
mysql >UPDATE user SET Password=PASSWORD(‘NEWPASSWORD’)
you will need to replace “user” with “root” if you are attempting to change the root password.
- From there, exit out of MySQL using:
- To finalize your changes, you’ll need to stop MySQL Server and start it back up again using:
root@server [~]/etc/init.d/mysqld stop
root@server [~]/etc/init.d/mysqld start
Now you can access your mysql service by using:
root@server [~]#mysql -u root -p
Like the mysqladmin method above, this method can be used to change other users’ passwords as well by replacing the “user” in the “UPDATE user SET Password=PASSWORD(‘NEWPASSWORD’)” command with the name of the user whose password you are trying to change.
And there you have it! Now you know how to set, change, and reset the root password within MySQL.