How to Change MySQL User Password

Summary: in this tutorial, you will learn how to change MySQL user password using the ALTER USER statement.

How To Change MySQL User Password

Before changing the password for a MySQL user account, it’s essential to consider the following important questions:

  • Which user account do you want to change the password for?
  • What application is using the user account whose password is being changed? If you change the password without changing the connection string of the application using the user account, the application may no longer be able to connect to the MySQL server.

Once you have these questions answered, you can start proceeding with changing the MySQL user’s password.

To change the password for a MySQL user, you use the ALTER USER ... IDENTIFIED BY statement:

ALTER USER username
IDENTIFIED BY new_password;

In this statement:

  • First, specify the username after the ALTER USER keywords.
  • Second, provide the new password after the IDENTIFIED BY keywords.

To execute the statement, you need to have an account with has global CREATE USER privilege or the UPDATE privilege for the mysql system database.

Changing MySQL User Password Example

First, open the Command Prompt on Windows or Terminal on Unix-like systems and log into the MySQL server:

mysql -u root -p

Second, create a new user called dolphin for demonstration:

CREATE USER dolphin@localhost
IDENTIFIED BY 'abcd1234';Code language: CSS (css)

Third, change the password of the dolphin user using the ALTER TABLE … IDENTIFIED BY statement:

ALTER USER dolphin@localhost
IDENTIFIED BY 'qDvOD3@L10'; -- new passwordCode language: CSS (css)

Summary

  • Use the ALTER USER ... IDENTIFIED BY statement to change the password of a MySQL user
Was this tutorial helpful?