This post contains a procedural guide for changing the MySQL root password in Windows or Linux.
Change MySQL Root Password Using “ALTER USER” Command in Windows
In Windows, for changing the root password of MySQL, open the command prompt as administrator and login to the MySQL server using this syntax:
Provide the username and hit “Enter”. Enter your password and log in to your server successfully:
To reload the grant table to perform flush-privileges operations, use this command:
The success message will display “Query OK, 0 rows affected”:
The “ALTER USER” command aids in modifying the changes in MySQL account, use the given below command to change the root password of your account:
The message will appear as “Query OK” if this command will execute successfully:
The Root Password is changed successfully. Let’s see the same process on Linux.
Change MySQL Root Password Using “ALTER USER” Command in Linux
Execute the same commands to change the root password of MySQL in Linux:
The root password of MySQL is changed successfully. Let’s now reset the password on Windows and Linux.
Reset MySQL Root Password in Windows
For resetting the root password of MySQL, stop its service. For that purpose, press the “Windows Logo + R” key, type “services.msc” and click on “OK”:
Select “MySQL80” in “Services” and click on the “Stop the service” link:
The service will stop successfully:
Create a text file as an administrator in the drive containing the operating system(preferable) and type the command using this syntax:
Provide the new root password in the syntax and save the file:
Name your file and save it:
Open the command prompt as Administrator:
Head into the directory containing MySQL. For this post, the directory is “C:\Program Files\MySQL\MySQL Server 8.0\bin”:
The directory will change successfully:
The next step is to reset the password by replacing initializing the file you just created for the password, by creating:
The error-free output will reset the root password in Windows:
The password is reset successfully, let’s do the same procedure for Linux.
Reset MySQL Root Password in Linux
To reset the root password in Linux, and you need to first stop the MySQL service by typing:
The status of MySQL service will change to “inactive (dead)”:
Create a txt file using any text editor. This post is using the nano editor by typing:
Type the command using this syntax in the file:
Provide the new root password, save and exit the file by pressing “CTRL + X” key:
Initialize the file using this command:
The root password will change successfully:
You have successfully Reset the MySQL Root password in Linux.
Conclusion
For changing the root password of MySQL in Windows and Linux, run the “ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘<NEW PASSWORD>’;” command. Use the same command in a file and initiate it to reset the password. This guide provided you with ways to change and reset the root password of MySQL in Linux and Windows.