Knowing the MySQL root password of your server can come in handy in several cases such as accessing the database server console to manually create databases, create users and associate them with the databases, grant privileges to database users on specific databases, etc.
If you are unsure of the MySQL root user password, then the below steps will guide you on how to reset the root password safely. Resetting/changing the database server root password occasionally is also an important security factor, which helps protect against unauthorized intrusion attempts.
The below steps can be followed to reset the root password for MySQL
How to Reset MySQL Root Password in Windows
Step 1: Stop the MySQL server
1. Begin by checking if you’re logged in as an administrator.
2. Press Win+R (hold the Windows/Super key, and press “r”.) Once the “Run” box appears type:
3. Click OK.
4. Scroll down the list of services to find the MySQL service. Right-click that entry, then left-click Stop.
Step 2: Launch a Text Editor
Click on the menu and search for Notepad.
Alternatively, you can use the path: menu > Windows Accessories > Notepad.
Step 3: Create a New Text File with the Password Command
1. Enter the following line into the text editor:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword';
Make sure you keep the quote marks and semicolon. Replace NewPassword with the password of your choice.
2. Use the File > Save As menu to save the file to the root of your hard drive (C: ). Choose a filename, such as mysql-init.txt.
Consequently, the localhost command makes the password change on your local system. If you’re changing the password on a system over the network, substitute the hostname for localhost.
Step 4: Open a Command Prompt
1. Press Ctrl+Shift+Esc.
2. Then, click on the File menu > Run new task.
3. Type cmd.exe, and check the box to run as administrator.
4. Click OK.
Step 5: Restart the MySQL Server with Your New Config File
1. Navigate to the MySQL directory using the command prompt:
cd "C:\Program Files\MySQL\MySQL Server 8.0\bin"
2. Then, enter the following:
Note that there are two slashes after the C: prompt.
Also, if you chose a different filename in Step 2, use that name after the double slash.
Step 6: Clean up
Now, you can log into your MySQL server as root using the new password.
Double-check to make sure it works. If you have unique configuration options (such as launching MySQL with a –defaults-file option), go ahead and do so.
Once MySQL launches, and you’ve confirmed the password change, delete the C:\mysql-init.txt file.