There might arise a situation where you need to reset the root password for your MySQL database. It can be because you forgot your password or you need to change the password for security reasons.
In this article, we will look into the process of resetting the MySQL password using Windows cmd. To do so follow the below steps:
Step 1: Stop the MySQL server
- Begin by checking if you’re logged in as an administrator.
- Press Win+R (hold the Windows/Super key, and press “r”.) Once the “Run” box appears type:
- Click OK.
- Scroll down the list of services to find the MySQL service. Right-click that entry then left-clicks 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
- 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.
- Use the File > Save As menu to save the file to the root of your hard drive (C:\ ). Choose a filename that makes sense, such as mysql-init.txt.
Consequently, the localhost command will make the password change on your local system. If you’re trying to change the password on a system over the network, substitute the hostname for localhost.
Step 4: Open a Command Prompt
- Press Ctrl+Shift+Esc.
- Then, click on the File menu > Run new task.
- Type cmd.exe, and check the box to run as administrator.
- Click OK.
Step 5: Restart the MySQL Server with Your New Config File
- Navigate to the MySQL directory using the command prompt:
cd "C:\Program Files\MySQL\MySQL Server 8.0\bin"
Accordingly, the command line will show that you’re working in this folder.
- Enter the following:
Note that there are two slashes after the C: prompt.
Also, if you chose a different name in Step 2, use the same 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:\myswl-init.txt file.
At this stage, you have successfully reset the root password for your MySQL database.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.