How To Reset MySQL 8 Root Password On Ubuntu 20.04

We can easily change and reset the MySQL 8 root password on ubuntu 20.04 using the following methods. Let’s see, how to reset MySQL 8 root password.

How To Reset MySQL 8 Root Password On Ubuntu 20.04

Suppose, you have lost or forget the MySQL 8 root password and need to recover it on ubuntu 20.04. you’re trying to access the server. There are two methods to recover. 

ubuntu@ubuntu:~/Desktop$ mysql -u root -p
Enter password: 
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

Check Out: How To Install CakePHP Framework On Ubuntu 20.04

Method 1:

We can recover the root password by creating a local file and restart the MySQL service. Create a file using the below command. 

sudo touch /home/user/init-file.txt

This file should be readable by the MySQL user and Paste the below command in this file. 

ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';

Now stop the MySQL service with the below command.

ubuntu@ubuntu:~$ sudo systemctl stop mysql

Or

ubuntu@ubuntu:~$ sudo /etc/init.d/mysql stop

Check Out: How To Create IAM Users Account As Administrator In AWS Cloud

When the service is topped then Use the below command.

sudo mysqld --user=mysql --init-file=/home/ubuntu/init-file.txt --console

Start the service and log in with your new password. 

ubuntu@ubuntu:~$ sudo systemctl start mysql
ubuntu@ubuntu:~$ sudo mysql -u root -p
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.21-0ubuntu0.20.04.4 (Ubuntu)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Method 2:

Reset MySQL Password With –skip-grant-tables:

We have the second method to recover the MySQL root password with –skip-grant-tables option. Stop the MySQL service using the below commands. 

sudo systemctl stop mysql
sudo mysqld --skip-grant-tables --user=mysql &

Check Out: How To Create Swap file Without Disk Partition On Linux

Once you have executed the above two commands. you need to start the MySQL service and then try to log in to the MySQL server with the following commands. 

ubuntu@ubuntu:~/Desktop$ sudo systemctl start mysql
[1]+ Exit 1 sudo mysqld --skip-grant-tables --user=mysql
ubuntu@ubuntu:~/Desktop$ sudo mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.21-0ubuntu0.20.04.4 (Ubuntu)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Now we will have to reload the grant. That way we can change the password. 

mysql> flush privileges;

Check Out: How To Fix the Upload Max File Size Issue In PHP On WordPress

Run the below command to change the root password. 

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassowrd';
Query OK, 0 rows affected (0.08 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

Exist the MySQL console and restart the service. Finally, login with your new password. you must be able to log in. In a similar way, you can change the MySQL 8 root password on any ubuntu distro.

That’s it.

Share on:

I'm the founder of Curious Viral. I hope this blog will provide you complete information about Linux Technology & I would like to share my technical knowledge with you which I have learned during this period.

Other Posts You May Like...

Leave a comment