If you are using MySQL database, and have forgotten its root user’s password or want to reset it for some reason. For this, you have to use 3 commands of mysql such as sudo systemctl set-environment MYSQLD_OPTS=”–skip-networking –skip-grant-tables”, flush privileges;, mysql> ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘(YOUR_NEW_PASSWORD)’;, and you can easily reset the password of your root user on Ubuntu.
Here are some steps to reset MySQL root user password on Ubuntu 22.04 or 20.04 systems:
Step 1: Stop MySQL Server
To reset the MySQL root user password, you first need to shut down the MySQL server, for this you can use the systemctl command:
sudo systemctl stop mysql.service
Step 2: Skip Grant Tables and Networking
To access MySQL server without valid password, you need to skip grant tables and networking validation, for this you can use this command:
sudo systemctl set-environment MYSQLD_OPTS="--skip-networking --skip-grant-tables"
The MySQL environment has been configured to use it without a password. Now, can log in to MySQL server without password.
Step 3: Start MySQL Server
Now the MySQL server has to be started so that the password can be reset, for this you can use the command:
sudo systemctl start mysql.service
Step 4: Log in as a MySQL Root User
To access MySQL Server and its service as root user without password, Type this command on terminal and you can log in to MySQL Server:
sudo mysql -u root
Step 5: Reset MySQL Root User Password
To select a MySQL database and flush privileges on terminal window, you use the “use mysql” and “flush privileges;” command:
flush privileges; use mysql;
To set a new password for the MySQL root user on, you need to use the MySQL alter command, type this command on a ubuntu terminal window:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '(YOUR_NEW_PASSWORD)';
Here, your_new_password in command, change it to your new password. And then exit the mysql shell by using this exit command:
Step 6: Stop all MySQL processes and restart the MySQL server
Once the root password is set, the changes made can be applied. For this, stop or stop all the MySQL server processes and restart the server, you can do this by:
To stop mysql proccesses:
sudo killall -u mysql
To restart mysql server:
sudo systemctl restart mysql.service
Step 7: Log in to MySQL With New Password
With the steps you’ve followed so far, the new password for your MySQL root user has been set. Now, you can login into MySQL server as root user with the new password, you can use:
sudo mysql -u root -p
Here is the video guide on how to reset MySQL root user password on ubuntu:
That’s it; you have a detailed tutorial explaining everything step by step, using which you can reset the MySQL root user password ubuntu 22.04 easily.