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

How to set user password in mysql

PHPz
Release: 2023-04-17 17:22:37
Original
3361 people have browsed it

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:

  1. Connecting to MySQL

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
Copy after login

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.

  1. Create a new user

The command to create a new user is as follows:

CREATE USER '用户名'@'localhost' IDENTIFIED BY '密码';
Copy after login

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';
Copy after login
  1. Grant permissions

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';
Copy after login

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';
Copy after login

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!

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