I recently had to work on a machine with an existing MySQL database running. Unfortunately, the person who installed MySQL had forgotten the root password.
If this happens to you, the first thing I will say is, ‘Don’t Panic!’. If you have root access to the server, then you are on your way, if not, then you will have to do this with someone who has root access.
Step 1. Shut down MySQL. This differs from one OS to another. The example here is on Ubuntu 12.04.
Step 2. Start up MySQL in safe mode
mysqld_safe --skip-grant-tables &
Step 3. Log in to mysql as root without the -p option
mysql -u root
Step 4. Update the root password and flush privileges
mysql> update user set password=PASSWORD("new password") where User='root';
mysql> flush privileges;
Step 5. Stop and Start MySQL
Login with your new password.