Home > Database > Mysql Tutorial > How to set root password in mysql

How to set root password in mysql

PHPz
Release: 2023-04-20 11:44:05
Original
1571 people have browsed it

MySQL is a commonly used relational database management system. It is open source, so it is widely used. In MySQL, the root user is the user with the highest privileges and can access and modify all databases and tables. To ensure security, MySQL does not set a password for the root user during the default installation. Therefore, after installing MySQL, you need to manually set the root user's password.

The following are the steps for MySQL to set the root user password:

  1. First, open a terminal or console and enter the following command to log in to MySQL:
mysql -u root
Copy after login

At this point, you should be able to see the MySQL prompt, indicating that you have successfully logged in to MySQL.

  1. Next, enter the following command to view the information of all current users:
SELECT user,authentication_string,host FROM mysql.user;
Copy after login

When executing the above command, it was found that in MySQL version 8.0, the authentication of the root user The method is changed from the deprecated password to caching_sha2_password, so using the above command will display that the value of the authentication_string column in the root user is caching_sha2_password. To change the root user's password normally, you need to change its authentication method to the password format used in MySQL 5.x. It can be modified through the following command:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';
Copy after login
  1. After the input is completed, please use the following command to make the change effective:
FLUSH PRIVILEGES;
Copy after login
  1. After completing the above steps, you can pass The following command exits MySQL:
exit;
Copy after login

In this way, you have successfully set the root user's password for MySQL. During future use, please be sure to keep the password you set properly to ensure safety.

Tip: If you have set the root user password when installing MySQL, then after logging in to MySQL, you can directly use the following command to modify the root user password:

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

But when you use When using the ALTER USER command to change a user's password, you must use the latest authentication plug-in. Therefore, caching_sha2_password is recommended in MySQL 8.0. If you use legacy commands to modify a user's password without changing the authentication plugin, you may have problems logging in as the root user.

In short, in MySQL, the root user has the highest authority, and it is very important to ensure the security of his account. Setting a password for the root user can effectively protect the security of the database and prevent data leakage and abuse.

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template