Preloader
Web development
  • Estimated reading time: 1 Minute

How to change the password of a MySQL user 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 !

Share:
Carlos Delgado

Carlos Delgado

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.

Related articles
The Convergence of AI, Cybersecurity & Modern Web Development | OST
16 Jun, 2026
  • Estimated reading time: 4 Minutes
WordPress vs Custom Website: Which is Better for Your Business?
23 Apr, 2026
  • Estimated reading time: 5 Minutes
Guidelines To Follow In Test Case Writing
4 Mar, 2026
  • Estimated reading time: 4 Minutes
How to Build a Secure Authentication System in a Web Application
27 Feb, 2026
  • Estimated reading time: 5 Minutes
Our Sponsors

Our blog is proudly supported by industry-leading sponsors.