How to recover MySQL root password in Linux?

Credentials for the article go here: http://www.howtoforge.com/reset-forgotten-mysql-root-password

Sometimes, we need to take over MySQL server, and this article explains how one can change root password for MySQL server.

If you logon locally or by using remote ssh connection, first this to do is to stop MySQL server service by typing the following command:

sudo service mysql stop

Second step is to run MySQL in safe mode without asking password. In order to do that, one can use a command as follows:

mysqld_safe --skip-grant-tables 

If it starts, you can see the message from the system. In my case, I needed to start another ssh session in order to continue.

Now we can connect with mysql client by the following command:

mysql --user=root mysql

As soon as we connected, hereby is a list of commands which help one to change the password:

update user set Password=PASSWORD('new-password') where user='root';
flush privileges;
exit;

After that one needs to kill mysqld process and restart MySQL in normal mode.