Open In App

How to Reset MySQL Root Password in Windows Using CMD?

Last Updated : 30 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

If you ever forget your MySQL database password or need to enhance security by changing it regularly, changing the MySQL root password is crucial as resetting the MySQL root password strengthens security and protects your database from unauthorized use.

In this article, we will learn how to Change MySQL root password using cmd in Windows. To do so follow the below steps:

How to Reset MySQL Root Password in Windows using cmd?

Note: Before Resetting MySQL Root Password, make sure that you are Log in to your system as Administrator.

Step 1: Stop the MySQL server

  • Press Win+R and type the following command in the Run box and Click OK.
services.msc

How-to-Reset-MySQL-Root-Password-in-Windows_1

  • Scroll down the list of services to find the MySQL service. Right-click that entry then left-clicks Stop.

For detailed guide, check out: How to Stop MySQL Server on Windows and Linux?

Step 2: Launch a Text Editor

  • Click on the menu and search for Notepad.
  • Alternatively, press Ctrl + Alt + N to open Notepad.

Step 3: Create a New Text File with the Password Command

  • Enter the following command to the text editor:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword';

Note: Copy-paste this command to avoid any mistake. Make a new password and Replace ‘NewPassword’ with it.

How-to-Reset-MySQL-Root-Password-in-Windows_2

  • 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:
mysqld --init-file=C:\\mysql-init.txt

Note:

  • There are two slashes after the C: prompt.
  • Also, if you choose 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 changed MySQL root password,

MySQL is widely used Database Management System. It helps in efficiently managing data through querying. The MySQL Root password feature denies unauthorized access. Therefore, it is a good practice to always create a MySQL Root password for data privacy. Since MySQL data is crucial, secure your MySQL database using a strong password. Check out Tips For Choosing a Strong Password

Also Read


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads