In this tutorial, we will show you how to reset the root password MySQL server. By default, the MySQL server will be installed with the root account and the password is blank. Have you ever forgotten the root password on one of your MySQL servers? If you have set the password for root and forget it, then you will need to reset the root password for MySQL. To reset your MySQL password just follow these instructions and we assume that you already have a small amount of knowledge of MySQL.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The reset password MySQL is quite simple. I will show you the step-by-step reset root password MySQL server.
Prerequisites
- A server running one of the following operating systems: Ubuntu or CentOS Linux.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Reset Root Password MySQL Server
Step 1. The first thing to do is stop MySQL.
Stop the MySQL server if it is currently running:
### CentOS 7 ### sudo service mysqld stop ### CentOS 9 Stream ### sudo systemctl stop mysqld
Step 2. Reset Root Password MySQL.
We need to start MySQL in safe mode with the --skip-grant-tables
option so that it will not prompt for a password.
mysqld_safe --skip-grant-tables &
After that, start the MySQL client process using this command with a root account and a blank password:
mysql -u root
Now change the password for the root account:
mysql> use mysql; mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where #User='root'; mysql> flush privileges; mysql> quit
Finally, restart the MySQL service by running the below command:
### CentOS 9 Stream ### sudo systemctl restart mysqld ### CentOS 7 ### sudo systemctl restart mysqld
Congratulations! You have successfully reset your password MySQL. Thanks for using this tutorial to reset the root password MySQL on the Linux system. For additional help or useful information, we recommend you check the official MySQL website.