| by Arround The Web | No comments

How to Change MySQL Root Password in Linux or Windows

MySQL provides the user with the facility to manage their databases and perform administrative tasks. In case the user wants to change the password due to any security reasons, it is possible using the “ALTER USER” command. This command is only available for the administrator or the root user. The administrator can also reset the root password.

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:

mysql -u root -p

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:

FLUSH PRIVILEGES;

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:

ALTER USER 'root'@'localhost' IDENTIFIED BY '<NEW PASSWORD>';

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:

ALTER USER 'root'@'localhost' IDENTIFIED BY '<NEW PASSWORD>';

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”:

cd "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:

mysqld --defaults-file="C:\ProgramData\MySQL\MySQL Server 8.0\my.ini" --init-file=C:\mysql-init.txt

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:

sudo systemctl stop mysql

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:

nano mysqlini.txt

Type the command using this syntax in the file:

ALTER USER 'root'@'localhost' IDENTIFIED BY '<NEW PASSWORD>';

Provide the new root password, save and exit the file by pressing “CTRL + X” key:

Initialize the file using this command:

Sudo mysqld --init-file=/home/username/mysqlini.txt &

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.

Share Button

Source: linuxhint.com

Leave a Reply