SoFunction
Updated on 2025-05-19

Detailed tutorial on setting and modifying MySQL root password

1. Common scenarios for setting MySQL root password

1.1 Set password for the first time after installing MySQL

After the first installation of MySQL, you usually need to set the root user's password so that the database can be accessed and managed safely in the future.

1.2 Reset after forgetting root password

If you forget the root user's password, you can reset it through specific steps to restore access to the database.

2. Set MySQL root password in Linux system

2.1 Set password using the mysql_secure_installation tool

On Linux systems, MySQL is usually installed with a tool called mysql_secure_installation, which can help you complete multiple security configurations including setting a root password.

2.1.1 Run mysql_secure_installation

Open the terminal and run the following command:

sudo mysql_secure_installation

2.1.2 Follow the prompts to set the root password

After running this command, the system will prompt you to enter the current root password (if it is the first time you install it, you may not have a password set, just press Enter). Then, you will be prompted to set a new root password. Follow the prompts to enter and confirm the new password to complete the setting.

2.2 Set password directly through SQL commands

If you are already able to log in to MySQL as root, you can also modify your password directly through SQL commands.

2.2.1 Login MySQL

mysql -u root -p

2.2.2 Modify password

On the MySQL command line, execute the following command to modify the root user's password:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

Willnew_passwordReplace with the new password you want to set.

2.2.3 Refresh permissions

After modifying the password, execute the following command to refresh the permissions:

FLUSH PRIVILEGES;

3. Set MySQL root password in Windows system

3.1 Setting password using MySQL Installer

On Windows systems, MySQL Installer is usually used when installing MySQL. During the installation process, the installer will prompt you to set the password of the root user.

3.1.1 Running MySQL Installer

Double-click the downloaded MySQL Installer installation file and start the installer.

3.1.2 Follow the prompts to set the password

During the installation process, find the steps to set the root password, enter and confirm the new password, and then continue to complete the installation.

3.2 Set password through MySQL Workbench

If you have MySQL Workbench installed, you can also use this tool to modify the root password.

3.2.1 Open MySQL Workbench

Start MySQL Workbench and connect to your MySQL server.

3.2.2 Modify password

In the MySQL Workbench navigation bar, select Manage -> Users and Permissions. Find the root user in the user list, click the "Edit Permissions" button, find the "Authentication" tab in the pop-up window, enter a new password and save.

4. How to reset the root password after forgetting

If you forget your root password, you can reset it through the following steps.

4.1 Reset password in Linux system

4.1.1 Stop MySQL Service

sudo systemctl stop mysql

4.1.2 Start the MySQL service and skip the permission table

sudo mysqld_safe --skip-grant-tables &

4.1.3 Login MySQL

mysql -u root

4.1.4 Modify password

On the MySQL command line, execute the following command to modify the root user's password:

FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

Willnew_passwordReplace with the new password you want to set.

4.1.5 Exit and restart MySQL service

exit
sudo systemctl restart mysql

4.2 Reset password in Windows system

4.2.1 Stop MySQL Service

Stop the MySQL service through the Service Manager or command line.

4.2.2 Start the MySQL service and skip the permission table

Open a command prompt and run the following command:

mysqld --skip-grant-tables --console

4.2.3 Login MySQL

mysql -u root

4.2.4 Modify password

On the MySQL command line, execute the following command to modify the root user's password:

FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

Willnew_passwordReplace with the new password you want to set.

4.2.5 Exit and restart MySQL service

Close the Command Prompt window and restart the MySQL service from the Service Manager or command line.

5. Summary

Setting and modifying MySQL root passwords is a basic operation in database management. Through the introduction of this article, you have mastered the method of setting and resetting the root password in different operating system environments. Whether you set your password after installing MySQL for the first time or resetting your password after forgetting it, you can easily complete it by following the above steps. To ensure the security of the database, it is recommended to change the root password regularly and use a strong password policy. I hope this article can help you become more skillful in MySQL database management.

The above is the detailed tutorial on setting and modifying MySQL root password. For more information about setting and modifying MySQL root password, please follow my other related articles!