How to Change MySQL User Password Linux?

MySQL is a free and open-source relational database that is easy to use, install and deploy. It is available on almost all platforms, including Windows and Linux. Using a strong, unique password can help to protect your database and the data it contains from unauthorized access. This is especially important if your database contains sensitive or confidential information.

With this in mind, this post aims to guide you about the possible methods to change the MySQL user password in Linux. The post’s content is as follows:

Let’s start adding a password.

How to Add a User Password in MySQL?

If the password is not set at the installation time, you can follow the steps provided below to add a MySQL root user password via the following steps.

Step 1: Log in to MySQL Through the root User

The primary step is to log in to the MySQL shell through the root user (by skipping the password) using the following command:

$ sudo mysql -u root --skip-password

Step 2: Add the Password

To add the Password, the syntax is as follows:

>  ALTER USER 'root'@'localhost' IDENTIFIED BY 'Your_Password_Goes_Here';

Here, your password will come in place of the phrase ‘Your_Password_Goes_Here’.

We will see a message pointed by an arrow in the image above that says, “Query OK”. After these commands, you must flush the privileges and exit the MySQL server.

> FlUsh privileges; > exit

Step 3: Test the Password

You should be able to login into MySQL as root using this command, whose output can also be seen:

$ sudo mysql -u root -p

Here, the “-u” stands for username, while “-p” stands for password.

How to Change the User Password in MySQL?

There are multiple ways to change the user password, which are explained here. Let’s look into them one by one.

Using the ALTER Command

The syntax using which the password can be updated using the ALTER command is as follows.

>  ALTER USER 'root'@'localhost' IDENTIFIED BY 'Your_Password_Goes_Here';  

In the last line, you need to replace ‘root’ with your username and ‘localhost’ with your hostname. When we execute the above command, we’ll see this message.

Using the UPDATE Command

The Update command, as the name suggests, will update the user’. To do that, we’d have to directly modify the user, which can be done using this command:

> UPDATE user SET authentication_string = PASSWORD('new-password-goes-here') WHERE USER= 'root' AND host = 'localhost';

It is not recommended to use the above two methods in one run because of the conflicts, so make sure to use only one of the above.

Note: Make sure the password is not much easy to crack. However, it must be remembered to avoid any problems while using MySQL.


In Linux, the password of MySQL users can be changed using the ALTER and UPDATE commands. To change it, you must be logged in as a root user.  While using these commands, make sure the password is complex enough to crack, and the user must create a backup. This post also provided the possible methods to change the MySQL password on Linux.