How to Change MySQL Root Password in Ubuntu 22.04?

MySQL is a standard query language for managing relational databases. If you forgot the MySQL root password, don’t worry; there is a method to reset it. The MySQL root password can be changed, and the server can be accessed with sudo privileges.

In this guide, we will elaborate on changing the root password for MySQL in Ubuntu 22.04.

How to Change MySQL Root User Password on Ubuntu 22.04?

This guide will take you through every step for changing the MySQL password for a root user on Ubuntu 22.04.

Step 1: Check MySQL Version

First, Check the installed version of MySQL server in our system using the below command:

$ mysql --version

MySQL version 8.0.30 is installed in the system.

Step 2: Stop MySQL service

Now, to change the MySQL root password; first, you need to stop the MySQL service using this command:

$ sudo systemctl stop mysql.service

You can verify the MySQL server service status if it’s running or stooped by executing provided command:

$ sudo systemctl status mysql.service

Output shows the system is “inactive”, verifying the MySQL service is stopped.

Step 3: Skip Grant Tables and Networking

Grant tables and networking store the user’s information in the MySQL database in Ubuntu 22.04. To access the database without a valid password, you need to skip grants tables and networking by running the following command:

$ sudo systemctl set-environment MYSQLD_OPTS="--skip-networking --skip-grant-tables"

The MySQL environment is set up. We can log in to the MySQL server without using a password.

Step 4: Start MySQL Service

After setting the MySQL environment, run this command to start the server:

$ sudo systemctl start mysql.service

The service for MySQL has started.

Use the following command in the terminal to verify the MySQL server’s status, including whether it is active or not:

$ sudo systemctl status mysql.service

The current state of MySQL is active (running).

Step 5: Log in as Root User to MySQL

You don’t need a password to log in as the root user. To do so, use the following command:

$ sudo mysql -u root

You will be directed to the MYSQL prompt.

Step 6: Change MySQL root password

Now, the root password for MySQL can be modified. Run the following “ALTER” command in MySQL prompt for changing the root password:

> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

The new MySQL root password is set up in Ubuntu 22.04.

Step 7: Quit MySQL Prompt

To log out, type “quit” in MySQL prompt:

> quit

Step 8: Restore Database Normal Settings

In the previous steps, we changed the environment variable by skipping networking and grant tables. Now, it’s time to restore the MySQL database’s normal setting for a secure configuration. For unsetting the environment variable:

$ sudo systemctl unset-environment MYSQLD_OPTS

Next, we need to remove the modified system configuration by reverting the MySQL using this command:

$ sudo systemctl revert mysql

Step 9: Kill All MySQL Services

Now, use the “killall” command to end all MySQL server’s processes:

$ sudo killall -u mysql

MySQL is completely closed.

Step 10: Restart MySQL service

Restart the MySQL service after closing all the previous processes using:

$ sudo systemctl restart mysql.service

MySQL has been activated again.

Step 11: Log in to MySQL With New Password

Finally, log in to the MySQL server as a root user with a newly created password:

$ sudo mysql -u root -p

So, we have successfully changed the MySQL root password in Ubuntu.


MySQL root password can be changed in Ubuntu 22.04 using the “ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘new_password’” command. Before that, we have to access the MySQL database skipping “grant tables” and “networking” that store user privilege information. In this guide, we have demonstrated the essential steps to change MySQL root password in Ubuntu 22.04.