I just needed to recover a lost MySQL root password on a clients server recently and thought I’d share the method I used.

Actually it’s not a method for recovering but you can change the forgotten password to a new one.
All you need is to follow this 6 easy steps:

Step 1: Stop the mysql service

# /etc/init.d/mysql stop

Expected Output:

Stopping MySQL database server: mysqld.

Step 2: Start the MySQL server without password

# mysqld_safe --skip-grant-tables &

Expected Output:

[1] 5988
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[6025]: started

Step 3: Connect to your MySQL server using the MySQL client

# mysql -u root

Expected Output:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 4.1.15-Debian_1-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

Step 4: Setup a new MySQL password for the root user

mysql> use mysql;
mysql> update user set password=PASSWORD("YOUR-NEW-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit;

Expected Output:

Database changed

Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Bye

Step 5: Stop the MySQL Server

# /etc/init.d/mysql stop

Expected Output:

Stopping MySQL database server: mysqld
STOPPING server from pid file /var/run/mysqld/mysqld.pid
mysqld_safe[6186]: ended

[1]+  Done                    mysqld_safe --skip-grant-tables

Step 6: Start the MySQL server and test it

# /etc/init.d/mysql start
# mysql -u root -p

Expected Output:

Starting service MySQL         done

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is xx
Server version: x.x.xx XXXXX MySQL RPM

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

Now you should be all done!
I hope this helps you as much as it did for me.