How to Reset the MySQL Root Password in Ubuntu
-
Reset the Forgotten MySQL
root
Password in Ubuntu -
Fix Error in Setting a MySQL
root
Password Withmysql_secure_installation
This tutorial considers two scenarios where we need to reset the MySQL root
password in Ubuntu. First, when we forget the root
password and want to reset it; second, when we cannot set a root
password with mysql_secure_installation
.
The instructions given in this tutorial have been tested using the MySQL Version 8.0.29 and Ubuntu 20.04.4. Let’s see the solution for both scenarios below.
superuser
or some other way to access with the root
privileges.Reset the Forgotten MySQL root
Password in Ubuntu
Keeping a strong password for a database is right, but remembering it can be difficult. What if you forget the MySQL root
password?
Do not panic! Many of us face this issue, but there is a way to update it.
-
Check your MySQL version by using the following command
$ mysql --version
-
Shut down the MySQL server.
In this way, we can easily restart it in safe mode to reset the
root
password. You will have to enter the system’sroot
password, which is Ubuntu in our case. -
To start the MySQL server without table grants, we use the following command to update the
systemd
configuration file to pass extra command line parameters to the MySQL server upon startup.sudo systemctl edit mysql
As soon as you press Enter, it will open a file for you using the
nano
editor. We will use it to edit theservice overrides
of MySQL.In this way, we can update MySQL’s default service parameters.
Add the following to the empty file that is just opened in the
nano
editor.[Service] ExecStart= ExecStart=/usr/sbin/mysqld --skip-grant-tables --skip-networking
Once you paste the content given above in the file, press Ctrl+X to exit the file, Y to save the updates that we just made, and Enter to confirm the file’s name.
Here,
ExecStart
clears all the default values first and then provides thesystemd
with a new startup command which includes different parameters. These parameters disable loading the networking and grant tables. -
To apply the updates, execute the following command to reload the
systemd
configuration.sudo systemctl daemon-reload
-
Start the MySQL server using the following command; it will not produce any output, but you can check the MySQL server’s status.
sudo systemctl start mysql
This time networking and grant tables are not enabled.
-
We connect to the database as a
root
user.sudo mysql -u root
-
Now, we can change the password, but before that, use the following command to tell the database server to reload the grant tables.
mysql> FLUSH PRIVILEGES;
-
Execute the statement below to reset the MySQL
root
user’s password. Do not forget to replace themy_new_password
with a strong password that you can easily remember.ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'my_new_password';
Here, MySQL lets us use custom authentication. So, the statement given above ensures that MySQL will now use its default authentication to authenticate the MySQL
root
user via a new password.Bravo! the MySQL
root
user’s password is now changed. Typeexit
to quit the MySQL shell. -
Revert the MySQL server to normal settings by executing the following command.
sudo systemctl revert mysql
We will see similar to the following output, which means the above statement has been executed successfully.
Removed /etc/systemd/system/mysql.service.d/override.conf. Removed /etc/systemd/system/mysql.service.d.
-
To apply the changes, reload the
systemd
configuration.sudo systemctl daemon-reload
-
Restart the MySQL Server as follows.
sudo systemctl restart mysql
-
Now, execute the following statement to log in as MySQL
root
user with the new password.mysql -u root -p
You will be asked the enter the MySQL
root
user’s password. Enter the new password and enjoy access to your database server.
Fix Error in Setting a MySQL root
Password With mysql_secure_installation
Users who install the MySQL server for the first time may have met the following error that is generated while setting a MySQL root
password with mysql_secure_installation
.
Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters.
Don’t worry! Here is the solution for that.
-
Kill the process of
mysql_secure_installation
or close the terminal. -
Use the following statement to log in to the MySQL server.
sudo mysql
You will be prompted to enter the system’s
root
password. Once you enter the password, you will be taken to the MySQL console. -
Execute the following
ALTER
statement. Don’t forget to replacemy_new_password
with your password.mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'my_new_password';
Further, type
exit
to quit the MySQL shell. -
Use the statement below to log in with the new password.
mysql -u root -p
Finally, you can log in to the MySQL server as a
root
user.