How to Change or Reset MySQL root Password via Command Line
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.
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 -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.
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;
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.
Tip: The above instructions works for changing or resetting password for any MySQL user. You just need to replace user root with another.