Javatpoint Logo
Javatpoint Logo

How to Change Root Password in MySQL

In MySQL, the root password refers to the password given to the root user, the most important user with access to all the data, including sensitive data contained in the MySQL database. The root user can control all the activities of the database. The root password is the way the root user is authenticated to access the database. It is essential to set a strong root password to prevent unauthorized user access and keep your valuable data secure.

Why do we need to change the root password in MySQL?

  • It is vital to change the root password to secure access to the database from intruders and provide access to only authorized users.
  • With time comes new technologies like password cracking techniques which strengthen the password and prevent intruders from cracking the password, so it becomes necessary to change the password regularly.

You can change the root password in two ways, which are as follows:

Method 1: Steps to change root password in MySQL when you know the current password

Step 1: First, go to your C drive in your Windows computer and find Program Files, then go to MySQL here and click on MySQL Server 8.0 or whatever version you have. Afterward, go to the bin; here, you will find the mysqladmin command, which we have to use to change the root password, so we have to copy this path we have followed.

How to Change Root Password in MySQL

You can see the selected path as shown below. Use the Control + C shortcut keys to copy this path.

Step 2: Now, open the command prompt on your computer. Click the Start button, search the command prompt, and run it as administrator.

Step 3: To change the directory, you must use the cd command, paste the copied path, and then press Enter.

Now, you are in the bin folder, as you can see below.

How to Change Root Password in MySQL

Step 4: Now, you need to run the following command to change the root password. You will need to replace 'NewPassword' with a new password of your choice at the end of the following command.

Step 4: After executing the above command, it will ask you to enter the password, as you can see below. Here, you must write the old password to confirm the password change command. It will give you a warning, but you can ignore it.

How to Change Root Password in MySQL

Your password is changed and reset to the new password.

You can also check whether the password has been changed or not. You can follow the next steps.

Step 5: Open MySQL command line prompt. Enter the new password here and press Enter. It will connect to MySQL, and a message will appear that says, 'Welcome to MySQL Monitor.' as you can see below.

How to Change Root Password in MySQL

You can also check if the root password has been changed or not in your MySQL workbench. For that, open MySQL Workbench; it will look like shown below; enter the new password, and click OK.

How to Change Root Password in MySQL

After entering the new password, you can see that it is connected to MySQL Workbench, which means that the password has been changed successfully.

How to Change Root Password in MySQL

Method 2: Steps to change the root password in MySQL when you forget the current password

Step 1: First of all, you need to stop MySQL services from running under Windows services. It is vital to stop the MySQL service in order to make any changes to MySQL.

To stop a service, go to Start, find Services, and click on Open. You will see a lot of services; find MySQL service. You can click anywhere on the screen and press the M key to search MySQL service on the keyboard. Now, right-click on MySQL service and stop the service, as shown below.

How to Change Root Password in MySQL

You can again right-click on MySQL service to verify whether the service is stopped or not. If it shows the option to start the service as shown below, that means you have successfully stopped the service.

How to Change Root Password in MySQL

Step 2: Now, create a new document on any drive of your computer and give it a name. You can give any relevant name to the document. We will name the document 'change.txt'.

Step 3: Now, open it with Notepad or any other editor of your preference. You need to type the following command in the document and mention the new password you want to set at the end of the command where it says 'NewPassword'. After writing the following command in the document, save it.

Your document will look like the one shown below.

How to Change Root Password in MySQL

Step 4: We have to use the mysqld command, which is present inside the bin folder. So now, on your Windows computer, go to your C drive and then find Program Files, then go to MySQL, here click on MySQL Server 8.0 or whatever version you have. After that, go to the bin folder; now, you can see the selected path as shown below. We need to copy this path using Ctrl + C.

How to Change Root Password in MySQL

Step 5: Go to Start, search the command prompt, and run it as an administrator. Now type cd to change the directory and paste the copied path. Now, you will come to the bin folder.

Step 6: Again, go to C drive and search for the ProgramData folder, but it is a hidden folder. To unhide it, go to View and click on Hidden items. Now, you can see a ProgramData folder as you can see below, double-click on it.

How to Change Root Password in MySQL

Under the ProgramData folder, you will find MySQL, click on it, then go to MySQL Server 8.0; here, you will find the 'my' file.

You have to copy the selected path, which you can see below.

How to Change Root Password in MySQL

Step 7: Go back to the command prompt and type the following command:

After typing the above command in the command prompt, as shown below, your password will be changed.

How to Change Root Password in MySQL

Now, you can close the command prompt.

Step 8: As you can see below, it is ready for connection, but before that, you need to start MySQL service again.

How to Change Root Password in MySQL

Step 9: Now, you can connect to MySQL Server and MySQL Workbench using the new password.

Conclusion:

In this article, you have comprehended how to change the root password in MySQL. You have understood the need to change the root password in MySQL. You learned that there are two ways to change the root password in MySQL, one way is when you know the current password and want to switch to a new password, and the other way is when you do not remember your current password and want to change to the new password. After learning the steps mentioned above, you will be able to change the root password in MySQL effortlessly.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA