How to Change or Reset MySQL root Password via Command Line

By Raju

February 4, 2017

MySQL

In this tutorial you are going to learn how to change or reset MySQL root password via command line on Linux/Ubuntu server. There are situations where you need to change password for root user or any database user.

MySQL Database Command Line Basics

  • How to Create a MySQL Database via Command Line
  • How to Select a MySQL Database via Command Line
  • How to Delete a MySQL Database via Command Line
  • How to Backup or Restore a MySQL Database via Command Line

First, you need to access to MySQL command line to execute required queries.

How to access MySQL command line or shell?

If MySQL server already installed on your server, then you can access it through shell or command line.

First, login to the server as a root and then enter below command to access MySQL shell interface.

You can use any of the below commands to access MySQL command line.

mysql
mysql -u root -p

In the above command u flag indicates root user, and -p flag for password authentication.

You have to enter password to access MySQL.

mysql>

Now, you are ready to reset default password for MySQL user.

How to Change or Reset MySQL root Password?

The MySQL database server users, databases, permissions and configuration data will be stored in default mysql database.

You need to select the mysql database through below command to set new password.

To make a database "mysql" the current database, execute this below query:

mysql> use mysql;

The above command gives you following output and shows that database has changed.

mysql> use mysql;
Database changed

Next, you need to update MySQL root user password with below query.

mysql> update user set password=PASSWORD('new_password') where User='root';

In the above query, the "new_password" represents your new password. Replace it with whatever your choice but make sure to use high strength password. 

Finally, reload the privileges of MySQL server using below command.

​

mysql> flush privileges;

Now that MySQL root password has changed to desired one, you can exit MySQL command line.

​

mysql> exit;

Tip: The above instructions works for changing or resetting password for any MySQL user. You just need to replace user root with another.

Leave a Reply

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

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Create a website in 3 simple steps

Choose a website template, add features, then customise! - Free Online Website builder.