Learn how to change the password of a registered user in MySQL from the command line.

The query to change the credentials from an user registered in MySQL is the following:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword');

However you won't be able to execute a plain query from an administrative tool like MySQL but from a terminal, in that case you will need to know how to run such query from the command line as well and we'll show you in this article how you can do this easily.

Changing user password

If you are able to change the password from any account because you have root access, the following command in your terminal should do the trick:

mysql -e "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword');"

You would only need to replace root with the username that you want to change the password and newpassword with the new password.

Changing passwords with credentials

In some cases for example, you will need to run the command without being authenticated, which means that you will surely face the following exception:

ERROR 1131 (42000) at line 1: You are using MariaDB as an anonymous user and anonymous users are not allowed to modify user settings.

For obvious reasons, you can't change the password of an account without the proper permissions, so you will need to provide them as arguments in the command to change the password from the user. For example, given a user namely root with password 12345, you can change the password of the root account with the following command:

mysql --user=root --password=12345 -e "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('6789');"

Happy coding !

Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.