Stop the MySQL service

sudo /etc/init.d/mysql stop

Start MySQL without a password

sudo mysqld_safe –skip-grant-tables &

Connect to MySQL

mysql -uroot

Set a new MySQL root password

use mysql;
update user set authentication_string=PASSWORD("newpassword") where User='root';
flush privileges;
quit

Stop and start the MySQL service

sudo /etc/init.d/mysql stop
sudo /etc/init.d/mysql start

Log in to the database

mysql -u root -p

Warning

mysqld_safe Directory ‘/var/run/mysqld’ for UNIX socket file don’t exists

mkdir -p /var/run/mysqld
chown mysql:mysql /var/run/mysqld

Method 2

sudo service mysql start
cd /var/run
sudo cp -rp ./mysqld ./mysqld.bak
sudo service mysql stop
sudo mv ./mysqld.bak ./mysqld
sudo mysqld_safe --skip-grant-tables --skip-networking &
mysql -u root
FLUSH PRIVILEGES;

SET PASSWORD FOR root@'localhost' = PASSWORD('my_new_password');