How to Reset MySQL Password on Ubuntu 22.04

In this blog, we will learn how to reset MySQL Root Password in Ubuntu/Linux. There might be a case you have forgotten your MySQL Database Password. In these steps, we will reset root password using ubuntu command line. This process of resetting root password is without login into mysql database.

Video Guide for Reset or Change the MySQL root Forgotten password on Ubuntu

Resetting the MySQL root password on Ubuntu involves a slightly different process compared to How to Reset MySQL Root Password on Windows. Here’s how you can do it:

Step 1: Stop MySQL Service: First, you need to stop the MySQL service to perform the password reset. You can do this using the following command:

sudo /etc/init.d/mysql stop

Step 2: Start MySQL in Safe Mode: Start MySQL in safe mode, which doesn’t require a password:

sudo mkdir /var/run/mysqld
sudo chown mysql /var/run/mysqld
sudo mysqld_safe --skip-grant-tables&

Step 3: Access MySQL Prompt: Access the MySQL prompt without a password:

sudo mysql --user=root mysql

Once you’re in the MySQL prompt, you will be connected to the mysql database:

Step 4: Update Root Password: Update the root password with the following SQL command. We will set password to null.

UPDATE mysql.user SET authentication_string=null WHERE User='root';

Step 5: Flush Privileges: Flush privileges to apply the changes:

FLUSH PRIVILEGES;

Step 6: Set MySQL Root Password: Once setting password to null, now we will reset password and set to desired value.

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Password@123';

Step 7: Flush Privileges: Flush privileges to apply the changes:

FLUSH PRIVILEGES;

Step 8: Exit MySQL Prompt: Exit the MySQL prompt:

exit;

Step 9: Stop MySQL Safe Mode: Stop the MySQL safe mode process:

sudo killall -KILL mysql mysqld_safe mysqld

Step 10: Start MySQL Service: Start the MySQL service again:

sudo /etc/init.d/mysql start

Step 11: Verify: Try logging in with the new password to verify that it works:

mysql -u root -p

These steps should allow you to reset the MySQL root password on Ubuntu. Make sure to replace Password@123 with your desired strong password.

Check our blog on How to Connect MySQL in Visual Studio Code.