How do I reset mysql root password? I can’t login

It happens that you forget the password to the root to your mysql server especially in development server like on WSL system. then now you cannot access and control anything. you are officially an intruder, an outcast, a user without privilege.  LUSER.

Fret no more, as the solution is easy like a bug quashing routine. Actually easier.

Login to SSH or your local terminal. and Type the following commands. make sure you don’t forget the WHERE part and resets password on everything like some people! yours truly.

First you need to tell your mysql server to STAHP


sudo service mysql stop

Then you need a safe mysql daemon and skip grant tables to allow access without checks on privileges. Also skip networking to avoid connection from anywhere and restrict it to localhost for security reasons. The Ampersand is NEEDED


sudo mysqld_safe --skip-grant-tables --skip-networking &

What does mysqld_safe do is explained below from mysql website.

[blockquote]mysqld_safe tries to start an executable named mysqld. To override the default behavior and specify explicitly the name of the server you want to run, specify a –mysqld or –mysqld-version option to mysqld_safe. You can also use –ledir to indicate the directory where mysqld_safe should look for the server.[/blockquote]

Then you should type mysql after couple of lines printed and it seems waiting for prompt.

now in mysql console. type


use mysql;
UPDATE user SET authentication_string = PASSWORD ('YOUR NEW PASS HERE'),plugin='mysql_native_password' WHERE user='root';
FLUSH PRIVILEGES;
exit;

The reason for setting plugin to mysql_native_password is because if you haven’t set password before, left it on default. it will have auth_socket and it might return the following error, when you try to login as root:

connect to server at ‘localhost’ failed
error: ‘Plugin ‘auth_socket’ is not loaded’

now back in shell, stop then start mysql normally


sudo service mysql stop
sudo service mysql start

For non-debian systems, replace mysql with mysqld. because that’s what centos like.

Leave a Reply

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

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.