This only works for root@localhost.
sudo /etc/init.d/mysql stop sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking & mysql -u root FLUSH PRIVILEGES; SET PASSWORD FOR root@'localhost' = PASSWORD('password');
Refer MysqlPasswordReset for more complex cases.