sudo /etc/init.d/mysql stop
Next we need to start MySQL in safe mode – that is to say, we will start MySQL but skip the user privileges table. Again, note that you will need to have sudo access for these commands so you don’t need to worry about any user being able to reset the MySQL root password:
sudo mysqld_safe --skip-grant-tables &
Note: The ampersand (&) at the end of the command is required.
All we need to do now is to log into MySQL and set the password.
mysql -u root
Note: No password is required at this stage as when we started MySQL we skipped the user privileges table.
Next, instruct MySQL which database to use:
Enter the new password for the root user as follows:
update user set password=PASSWORD("mynewpassword") where User='root';
and finally, flush the privileges:
Now the password has been reset, we need to restart MySQL by logging out:
and simply stopping and starting MySQL.
On Ubuntu and Debian:
sudo /etc/init.d/mysql stop ... sudo /etc/init.d/mysql start
On CentOS and Fedora and RHEL:
sudo /etc/init.d/mysqld stop ... sudo /etc/init.d/mysql start
NOTE: The whole content is taken from http://www.rackspace.com/knowledge_center/article/mysql-resetting-a-lost-mysql-root-password
Test the new password by logging in:
mysql -u root -p