Situatie
Resetting the MySQL root password using Command Prompt (CMD) in Windows is a crucial skill for database administrators and developers. Whether you’ve forgotten your root password or need to change it for security reasons, CMD offers a straightforward solution.
Solutie
Pasi de urmat
- Press Win+R and type the following command in the Run box and Click OK.
- Scroll down the list of services to find the MySQL service. Right-click that entry then left-clicks Stop.
- Launch a Text Editor
- Click on the menu and search for Notepad.
- Alternatively, press Ctrl + Alt + N to open Notepad.
Create a New Text File with the Password Command
- Enter the following command to the text editor:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword';
- 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.
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.
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:
mysqld --init-file=C:\\mysql-init.txt
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 changed MySQL root password.
Leave A Comment?