Home  >  Article  >  Database  >  How to set the root user's password in MySQL?

How to set the root user's password in MySQL?

PHPz
PHPzOriginal
2024-03-14 14:27:031279browse

How to set the root users password in MySQL?

Setting the root user's password in MySQL is a very important security measure, which can effectively protect the data in the database from unauthorized access. The following are the specific steps and code examples on how to set the root user password in MySQL:

Step 1: Log in to the MySQL database
First, you need to log in to the MySQL database as the root user. Enter the following command in the command line window and press Enter:

mysql -u root -p

The system will prompt you to enter the password of the root user. Enter the correct password and press Enter to log in to the MySQL database.

Step 2: Set the password for the root user
Once you successfully log in to the MySQL database, you can set the password for the root user through the following code example:

ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';

Please change the above code Replace New Password with the new password you want to set and execute this command.

Step 3: Refresh permissions for the changes to take effect
After setting the root user password, you need to refresh the permissions for MySQL for the changes to take effect. Execute the following command:

FLUSH PRIVILEGES;

After executing the above steps, the password of the root user has been successfully changed to the new password you set.

If you want to reset the root user's password to the default password, you can use the following code example:

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

After executing the above command, the root user's password will be reset to empty, i.e. no password protection.

Through the above steps, you can easily set the password of the root user in MySQL and improve the security of the database. Remember to change your passwords regularly and keep your database passwords securely. Have fun using MySQL!

The above is the detailed content of How to set the root user's password in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn