For Linux
This process is if you have root access on your server
First things first. Log in as root and stop the mysql daemon. Now lets start up the mysql daemon and skip the grant tables which store the passwords.
mysqld_safe --skip-grant-tables
You should see mysqld start up successfully. If not, well you have bigger issues. Now you should be able to connect to mysql without a password.
mysql --user=root mysql
update user set Password=PASSWORD('new-password') where user='root';
flush privileges;
exit;
Now kill your running mysqld, then restart it normally. Let me know if you have a problem
For windows
Stop your MySQL server completely. This can be done from Wamp(if you use it), or start “services.msc” using Run window, and stop the service there.
Open your MS-DOS command prompt using “cmd” inside the Run window. Then go to your MySQL bin folder, such as C:\MySQL\bin. Path is different if you use Wamp.
Execute the following command in the command prompt:
mysqld.exe -u root --skip-grant-tables
Leave the current MS-DOS command prompt as it is, and open a new MS-DOS command prompt window.
Go to your MySQL bin folder again.
Enter “mysql” and press enter.
You should now have the MySQL command prompt working. Type “use mysql;” so that we switch to the “mysql” database.
Execute the following command to update the password:
UPDATE user SET Password = PASSWORD('your_new_passowrd') WHERE User ='root';
However, you can now run almost any SQL command that you wish.
After you are finished close the first command prompt, and type “exit;” in the second command prompt.
You can now start the MySQL service. That’s it.
Take a look at this answer for more explanation from the discussions