sajad torkamani

Suppose you’ve lost your root user’s MySQL password on Ubuntu. You can recover and change it using the steps below.

Stop MySQL server:

 sudo systemctl stop mysql

Start the mysqld without password checking:

sudo mysqld --skip-grant-tables &

If you get any errors about the /var/run/mysqld folder not existing, create it:

sudo mkdir -v /var/run/mysqld && sudo chown mysql /var/run/mysqld

Login to MySQL as root:

mysql -u root mysql

Update password:

FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'YOURNEWPASSWORD';
FLUSH PRIVILEGES;

Kill the passwordless process:

sudo killall -9 mysqld

Login as root and enter your new password when prompted

mysql -u root -p

Links