Categories: Ubuntu

How to Change MySQL Root Password in Ubuntu 22.04

Not everybody is good at remembering passwords. Do not panic in case you cannot remember the password of your MySQL account. You can still access the MySQL server and change the root password even if you have misplaced or forgotten the root password. But how is a beginner going to perform this operation?

This blog will demonstrate the method for changing the MySQL root password in Ubuntu 22.04. Let’s get started.

Changing MySQL root password in Ubuntu 22.04

For the purpose of changing the MySQL root password in Ubuntu 22.04, follow the given instructions.

Note: The given method for changing password only implies for MySQL version greater or equal to “8”.

Step 1: Check MySQL version

First thing you need to do is check the version of installed MySQL on your system:

$ mysql –version

As you can see, we have installed MySQL version “8.0.29”:

Step 2: Stop MySQL service

In the next step, utilize the below-given command for stopping the MySQL service:

$ sudo systemctl stop mysql.service

Now, the MQL service is stopped:

Step 3: Check MySQL status

After stopping the MySQL service, verify its status with the help of the provided command:

$ sudo systemctl status mysql.service

The given output indicates that MySQL is currently inactive on our system:

Step 4: Skipping Networking and Grant Tables

MYSQL server should start without networking checks and granting tables. To do so, set the value of “MYSQLD_OPTS”:

$ sudo systemctl set-environment MYSQLD_OPTS=“–skip-networking –skip-grant-tables”

Then head towards the next step.

Step 5: Start MySQL service

Utilize the following command for starting the MySQL service

$ sudo systemctl start mysql.service

MySQL service is now started:

Step 6: Check MySQL status

Again, check the status of the MySQL service to ensure that it is currently active and running on the Ubuntu 22.04 system:

$ sudo systemctl status mysql.service

Now head towards the next step.

Step 7: Log in to MySQL

Write out the provided command in terminal for loging in to MySQL account:

$ sudo mysql -u root

Step 8: Flush privileges

To change the MySQL root password, firstly, it is required to flush all privileges:

> flush privileges;

Step 9: Choose MySQL database

Then choose the MySQL database:

> USE mysql

Step 10: Change MySQL root password

After doing so, utilize the “ALTER” command and specify the new password:

> ALTER USER ‘root’@‘localhost’ IDENTIFIED BY ‘new_password’;

Step 11: Exit MySQL

Lastly, type “quit” to logout from the active MySQL shell:

> quit;

Step 12: Reverting Database to its normal settings

In order to restart MySQL database back in “normal” mode, first of all, “revert” the added changes by unsetting the value of environment variable:

$ sudo systemctl unset-environment MYSQLD_OPTS

Next, revert “MySQL” for removing the set system configuration:

$ sudo systemctl revert mysql

Step 13: Kill MySQL processes

Execute the following “killall” command to kill all MySQL processes:

$ sudo killall -u mysql

Step 14: Restart MySQL service

In the final step, restart the MySQL service so that it can take changes from the configured settings:

$ sudo systemctl restart mysql.service

Now, move to the next step.

Step 15: Log in to MySQL

Log in to MySQL by writing out the following command in Ubuntu 22.04 terminal and specify the password you have added with the “ALTER” command:

$ sudo mysql -u root -p

That’s it. You have successfully changed the password of MySQL:

We have compiled the easiest method for changing the MySQL root password in Ubuntu 22.04.

Conclusion

To change the MySQL root password in Ubuntu 22.04, firstly, stop the MySQL service and unset the value of the environment variable. After doing so, start the MySQL service, log in to the MySQL root account, flush all privileges, choose a database, and use the “ALTER” command to specify the new password. Lastly, revert the database back to the normal mode, restart the MySQL service, and log in with the new password. This blog demonstrated the method for changing the MySQL root password in Ubuntu 22.04.

Ubuntu Server Admin

Recent Posts

Predict, compare, and reduce costs with our S3 cost calculator

Previously I have written about how useful public cloud storage can be when starting a…

19 hours ago

One Thread to Poll Them All: How a Single Pipe Made WaterDrop 50% Faster

This is Part 2 of the "Karafka to Async Journey" series. Part 1 covered WaterDrop's…

22 hours ago

A year of documentation-driven development

For many software teams, documentation is written after features are built and design decisions have…

2 days ago

Announcing FIPS 140-3 for Ubuntu Core22

With the release of the FIPS 140-3 certified cryptographic modules for Ubuntu 22.04 LTS, Canonical…

3 days ago

The foundations of software: open source libraries and their maintainers

Open source libraries are repositories of code that developers can use and, depending on the…

6 days ago

From inspiration to impact: design students from Regent’s University London explore open design for their dissertation projects

Last year, we had the opportunity to speak at Regent’s UX Conference (Regent’s University London’s…

7 days ago