Resetting forgotten root password in mysql

I forgot my root password for mysql. This is how to restore it
First, I need to stop the server:
%sudo /etc/init.d/mysql stop

then run the server in safe-mode with –skip-grant-tables to enable login without password
%sudo /usr/bin/mysqld_safe --skip-grant-tables &

Then we can login without the password and access mysql database
%mysql --user=root mysql

In MySQL client, do this:
mysql> update user set Password=PASSWORD('new-password-here') WHERE User='root';
Query OK, 2 rows affected (0.04 sec)
Rows matched: 2 Changed: 2 Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)

mysql> exit

after this type “fg” and “ctrl-C” to kill the server in safe mode. Now start the server again.
%sudo /etc/init.d/mysql start

Now you can try logging in with the new password:
%mysql --user=root --pass=new-password-here

Advertisements

One Response

  1. Hi,

    Excellent.Nice knowledge sharing. Thanks for same.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: