X

How to Reset Your MySQL or MariaDB Root Password on Ubuntu 22.04

Spread the love

In this tutorial we are going to explain how to reset MySQL or MariaDB Root password on Ubuntu 22.04.

MySQL is an open-source SQL database management system distributed and supported by Oracle Corporation. It is a relational database management system that provides fast, reliable, scalable, and easy usage. MySQL works in client/server or embedded systems.

MariaDB is also a popular open-source relational database management system made by the original MySQL developers. In this tutorial, we are going to install the MariaDB database system and will explain how to reset the root password. Since it is made by MySQL developers, the same commands are applied for both systems.

Installing MariaDB or MySQL and resetting the root password is a straightforward process that may take up to 15 minutes. Let’s get started!

Prerequisites

  • A server with Ubuntu 22.04 as OS
  • User privileges: root or non-root user with sudo privileges

Step 1. Update the System

Every fresh installation of Ubuntu 22.04 needs to be updated. That’s why we need to update the package to the latest versions available.

sudo apt-get update -y && sudo apt-get upgrade -y

Step 2. Install MariaDB database service.

To install the MariaDB database server, execute the command below.

sudo apt install mariadb-server -y

Start and enable the mariadb.service with the following commands:

sudo systemctl start mariadb && sudo systemctl enable mariadb

Check the status of the mariadb.service

sudo systemctl status mariadb

You should receive the following output:

root@host:~# sudo systemctl status mariadb 
● mariadb.service - MariaDB 10.6.12 database server 
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled) 
Active: active (running) since Thu 2023-09-07 09:28:14 CDT; 17s ago 
Docs: man:mariadbd(8) https://mariadb.com/kb/en/library/systemd/ 
Main PID: 2722 (mariadbd) 
Status: "Taking your SQL requests now..." 
Tasks: 16 (limit: 4558) 
Memory: 61.2M 
CPU: 528ms 
CGroup: /system.slice/mariadb.service └─2722 /usr/sbin/mariadbd

Step 3. Secure MariaDB database service

Next we will secure the MariaDB database service and will set a root password. To do that execute the following command:

mysql_secure_installation

You need to pass these steps with the following options:

Switch to unix_socket authentication [Y/n] Y 
Change the root password? [Y/n] Y 
New password: 
Re-enter new password: 
Password updated successfully! 
Reloading privilege tables.. 
... Success! 
Remove anonymous users? [Y/n] Y 
Disallow root login remotely? [Y/n] Y 
Remove test database and access to it? [Y/n] Y 
Reload privilege tables now? [Y/n] Y 
All done! If you've completed all of the above steps, your MariaDB installation should now be secure. 
Thanks for using MariaDB

After installation login to the MySQL console will not ask for a password and will let you without any password, even though we set it in the previous step.

To set MySQL to ask you for a password execute the following command:

ALTER USER 'root'@'localhost' IDENTIFIED VIA mysql_native_password USING PASSWORD('YourStrongPasswordHere');

Now, if you try to log in only with this command: mysql you will get this message:

root@host:~# mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

You need to use the following command and enter your password:

root@host:~# mysql -u root -p 
Enter password: 
Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 64 Server version: 10.6.12-MariaDB-0ubuntu0.22.04.1 Ubuntu 22.04 

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 
MariaDB [(none)]>

Step 4. Reset MariaDB Root password

In the previous step we set the root password for the MariaDB root user to be YourStrongPasswordHere. We know our root password but in some cases, the admins or developers manage to lose it and need to log in to the MySQL server.

So, to set a new root password we must follow the next steps:

First stop the MariaDB service with the following command:

systemctl stop mariadb

After stopping the service check the status:

systemctl status mariadb

You should get the following output:

root@host:~# systemctl status mariadb 
○ mariadb.service - MariaDB 10.6.12 database server 
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled) 
Active: inactive (dead) since Thu 2023-09-07 10:13:33 CDT; 36s ago 
Docs: man:mariadbd(8) https://mariadb.com/kb/en/library/systemd/ 
Process: 2722 ExecStart=/usr/sbin/mariadbd $MYSQLD_OPTS $_WSREP_NEW_CLUSTER $_WSREP_START_POSITION (code=exited, status=0/SUCCESS) 
Main PID: 2722 (code=exited, status=0/SUCCESS) 
Status: "MariaDB server is down" 
CPU: 1.760s

Now, when the service is stopped we need to start it without permission checking and without networking to prevent other users from connecting in the meantime:

sudo mysqld_safe --skip-grant-tables --skip-networking &

The ampersand will allow us to use the terminal. Now, execute the following command:

mysql -u root

This will connect us without a password. Flush the privileges first and then change the password with the commands below:

FLUSH PRIVILEGES; 
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('NewStrongPasswordHere'); EXIT;

Now, the password is changed from YourStrongPasswordHere TO NewStrongPasswordHere.

Next we need to kill the process id manually for the mysql service. To find it execute the following command:

ps aux | grep mysqld_safe

You will get output similar to this: root@host:~#

ps aux | grep mysqld_safe 
root 3292 0.0 0.1 14100 5596 pts/0 S 05:18 0:00 sudo mysqld_safe --skip-grant-tables --skip-networking 
root 3293 0.0 0.0 14100 880 pts/1 Ss+ 05:18 0:00 sudo mysqld_safe --skip-grant-tables --skip-networking 
root 3294 0.0 0.0 2888 1732 pts/1 S 05:18 0:00 /bin/sh /usr/bin/mysqld_safe --skip-grant-tables --skip-networking 
root 3427 0.0 0.0 9208 2288 pts/0 S+ 05:28 0:00 grep mysqld_saf

In our case, the process id is 3292. To kill this process execute the following command:

kill -9 3292

Now, we can start the MariaDB service normally:

sudo systemctl start mariadb

Now, you can use the NewStrongPasswordHere.

Congratulations! You just learned how to reset MySQL or MariaDB Root password on Ubuntu 22.04. Of course, you do not have to do this if you find any difficulties. You can contact our technical support by submitting a support ticket or live chat. We are available 24/7

If you liked this post on how to reset your MySQL or MariaDB root password on Ubuntu 22.04, please share it with your friends on the social networks or simply leave a reply below. Thanks.

admin:
Related Post