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?
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.
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.
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.
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.
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.
After entering the new password, you can see that it is connected to MySQL Workbench, which means that the password has been changed successfully.
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.
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.
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.
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.
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.
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.
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.
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.
Step 9: Now, you can connect to MySQL Server and MySQL Workbench using the new password.
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.