How to reset MySQL root password on Ubuntu

Here are the steps to reset your MySQL root password on a Ubuntu server.

Step 1: Edit the file at /etc/mysql/my.cnf and add the following two lines to the end of the file.

[mysqld]
skip-grant-table

Step 2: Restart the MySQL server. The server will now skip over the password authentication.

sudo systemctl restart mysql.service

Step 3: Log in to MySQL server. Press enter when prompted for password. You should be able to login without password now.

mysql -u root -p
Enter password:

Step 4: Update the password of the root user.

update mysql.user set authentication_string = CONCAT('*', UPPER(SHA1(UNHEX(SHA1('yournewpasswordhere'))))) where user = 'root' and host = 'localhost';

Step 5: Exit MySQL, remove or comment away the two lines in /etc/mysql/mycnf, and restart the MySQL server.

sudo systemctl restart mysql.service

You should now be able to login to the MySQL server using your new password.

Leave a comment