Follow these steps to reset MySQL root user password:
First stop mysql using below command:
1 |
sudo /etc/init.d/mysql stop |
Run the commands below to create a new mysqld directory
1 |
sudo mkdir /var/run/mysqld/ |
Set permission to mysql user access to it
1 |
sudo chown mysql /var/run/mysqld/ |
Now run the commands below to start MySQL in safe mode by bypassing the standard authentication process:
1 |
sudo mysqld_safe --skip-grant-tables & |
You will see like this
1 2 3 4 5 |
root@ip-172-31-4-179:/var/www/html# sudo mysqld_safe --skip-grant-tables & [1] 15675 root@ip-172-31-4-179:/var/www/html# 2019-01-17T12:10:38.077290Z mysqld_safe Logging to syslog. 2019-01-17T12:10:38.080433Z mysqld_safe Logging to '/var/log/mysql/error.log'. 2019-01-17T12:10:38.098434Z mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql |
You may need to press the Enter key
Now you can login into database server with the root account without typing a password
1 |
sudo mysql -u root |
Run below command select ‘mysql’ database. This database holds the settings for root user and other server setting.
1 |
use mysql; |
Finally, run below command to change the root password
1 |
update user set authentication_string=PASSWORD("New_Passwore_Here") where User='root'; |
Save the change by running the commands below
1 2 |
flush privileges; exit; |
Finally, stop MySQL safe_mode and start MySQL default service
1 2 |
sudo /etc/init.d/mysql stop sudo /etc/init.d/mysql start |
Done!
Now you can login with new password:
1 |
sudo mysql -u root -p |
and enter new password.