FB

How to Change User Password in MySQL

Do you want to change a user’s password on a MySQL database?

If so, then this guide is what you need.

You’ll learn:

  • a brief summary of the command
  • how to use the command to change the user’s password
  • an alternative method if you happen to be running an older version of MySQL

Let’s take a look.

Summary of Command

Here’s a brief summary of the MySQL change user password command:

ALTER USER 'username'@'localhost' IDENTIFIED BY 'new_password';

Replace the username with the user that you want to change the password for, and new_password with the new password for that user.

Let’s see this step by step.

 

Alter User to Change Password

To change a user’s password in MySQL, you need to log on to the database as a user that has privileges to do this. The root user can do this, which is the default user if you’ve got this set up on your computer. Otherwise, you can use any other user that has the privileges to do this.

Log in to your database either using the command line or an IDE such as MySQL Workbench.

Command line:

mysql -u root -p

This command will logon to MySQL as the root user. The -p parameter will make the command line prompt you for the password, which you can enter.

If you’re using MySQL Workbench (or another IDE), create a connection to the database and connect to it.

Next, run the ALTER USER command:

ALTER USER 'username'@'localhost' IDENTIFIED BY 'new_password';

In this command, you can change the username to the user you want to change the password for. The single quotes remain in this command.

The localhost is the database you’re connecting to. If you’re connecting to a server not on your computer, you can specify the remote hostname here.

The new_password is the new password of the account you’ve specified.

So, to change the password for the account called “john” to “October2021”, your command would be:

ALTER USER 'john'@'localhost' IDENTIFIED BY 'October2021';

Run this command, and the password will be changed.

 

Alter Password in Older Version with Set Password

In recent versions of MySQL, the Alter User command is the preferred way to change a user’s password. It’s even recommended by MySQL.

However, in older versions (before 5.7.5, which was released in September 2014), you’ll have to run a different command.

This command is SET PASSWORD and looks like this:

SET PASSWORD FOR 'username'@'localhost' = PASSWORD('new_password');

It takes the same inputs as the Alter User command.

For example, to change the password for the account called “john” to “October2021”, the command would be:

SET PASSWORD FOR 'john'@'localhost' = PASSWORD('October2021');

This will update the password for this account.

 

Conclusion

In summary, to change a user’s password in MySQL you should use the Alter User command. There is a command called Set Password but this is no longer the recommended way to change a password.

1 thought on “How to Change User Password in MySQL”

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Table of Contents