MySQL is a relational database management system and an open source software that supports multi-user, multi-thread and multi-site operations. The default manager of MySQL is root, which has the highest authority, but in order to ensure the security of the database, we need to set a user password.
In MySQL, setting a user password mainly includes the following three steps:
First, you need to use the root user to connect to MySQL. We can enter the following command in the terminal:
mysql -u root -p
Among them, -u represents the user name, and -p represents requiring a password. If the root user has a password, enter the system password after entering the command.
The command to create a new user is as follows:
CREATE USER '用户名'@'localhost' IDENTIFIED BY '密码';
Among them, 'username' represents the username you want to create,' localhost' means that the user can only connect to MySQL locally. If you want to allow this user to connect from outside, change 'localhost' to '%'. 'Password' represents the password for this user. It should be noted here that the password must be enclosed in single quotes, otherwise an error will occur during setting.
For example, if we want to create a user named 'guest', we can enter the following command:
CREATE USER 'guest'@'localhost' IDENTIFIED BY 'mypassword';
Create new user After that, we need to grant permissions to it. In MySQL, we can authorize users through the GRANT command.
The authorization command is as follows:
GRANT 权限列表 ON 数据库名.表名 TO '用户名'@'localhost';
Among them, the permission list indicates the permissions you want to grant to the user, which can be ALL PRIVILEGES, SELECT, INSERT, UPDATE, DELETE, etc. Database name. Table name indicates the database and table you want to grant permissions to.
For example, if we want to give the guest user all permissions to access the test library, we can enter the following command:
GRANT ALL PRIVILEGES ON test.* TO 'guest'@'localhost';
After the above three steps, we have successfully set the MySQL user password. From now on, we can use this user to connect to MySQL.
Summary
When using MySQL, in order to ensure the security of the database, the user password must be set and permissions must be granted. Setup steps include connecting to MySQL, creating new users, and granting permissions. Authorization commands can grant ALL PRIVILEGES, SELECT, INSERT, UPDATE, DELETE and other permissions to users. When operating, caution is required to avoid data loss or leakage.
The above is the detailed content of How to set user password in mysql. For more information, please follow other related articles on the PHP Chinese website!