How to Set, Change and Reset MySQL Root Passwords

In this article you will learn how to set, change , recover and reset forgotten MySQL root passwords. Many times you might have seen this error:
ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: YES). If you are facing any issues related to lost root password then follow the below steps.

How to find temporary MySQL Root Password after installation?

[root@rac1 ~]#sudo grep 'temporary password' /var/log/mysqld.log
2018-11-04T18:48:45.964263Z 1 [Note] A temporary password is generated for root@localhost: %XT7g;Lk0FZL

Setup Root Password for the First Time

After installation You will get the temporary password in the mysqld.log file.

[root@rac1 ~]#sudo grep 'temporary password' /var/log/mysqld.log

To change or update password for the first time:
mysqladmin -u root password newpass

If you will get the below error then either try the steps given below:
[root@rac1 ~]# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

[root@rac1 ~]# mysql_secure_installation
Change the root password? [Y/n]  : y
New password:  Enter a new MySQL root password
Re-enter new password:  Re-enter MySQL root password
Remove anonymous users? [Y/n] : y
Disallow root login remotely? [Y/n] : y
Remove test database and access to it? [Y/n] : y
Reload privilege tables now? [Y/n] : y

How to Update or Change MySQL Root Password?

To change the root password, login as root user and use any of the following methods:

[root@rac1 ~]# mysql -u root -p
Enter password:
mysql> use mysql
mysql> update user set authentication_string=password('NEW_PASSWORD') where user='root';
mysql> flush privileges;
mysql> quit

OR
 
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Login@123';
Query OK, 0 rows affected (0.00 sec)

How to Recover Lost MySQL Root Password?

Below are the steps to Reset lost MySQL Root Password

 STEP 1: Stop the MySQL Server
[root@rac1 ~]# service mysqld stop
Stopping mysqld: [ OK ]


STEP 2: Start mysqld server daemon process with --skip-grant-tables option so that it will not prompt for a password.
[root@rac1 ~]# mysqld_safe --skip-grant-tables &
[1] 7852
[root@rac1 ~]# 2018-12-22T08:31:31.558388Z mysqld_safe Logging to '/var/log/mysqld.log'.
2018-12-22T08:31:31.587632Z mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql


STEP 3: Connect to the MySQL Root User without password
[root@rac1 ~]# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.24 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

STEP 4: Connect to mysql database
mysql> use mysql;


STEP 5: Set the New Root Password
mysql> update user set authentication_string=password('NEW_PASSWORD_HERE') where user='root';
mysql> flush privileges;
mysql> quit


STEP 6: Stop and Start the MySQL Server
[root@rac1 ~]# service mysqld stop
2018-12-22T08:37:51.025459Z mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
Stopping mysqld: [ OK ]
[1]+ Done mysqld_safe --skip-grant-tables

[root@rac1 ~]# service mysqld start
Starting mysqld: [ OK ]


STEP 7: Login to the newly entered MySQL Root Password:
[root@rac1 ~]# mysql -u root -p
Enter password: