Home  >  Article  >  Database  >  How to create a user in MySQL

How to create a user in MySQL

autoload
autoloadOriginal
2021-03-30 18:16:264164browse

MySQL During installation, a user named root will be created by default. This user has super permissions and can control the entire MySQL server, but in When you first get started, your lack of understanding of the database may lead to incorrect use of the database, so we usually create some users with appropriate permissions.

1. Use the CREATE USER statement to create a user

CREATE USER <用户> [ IDENTIFIED BY [ PASSWORD ] &#39;password&#39; ] [ ,用户 [ IDENTIFIED BY [ PASSWORD ] &#39;password&#39; ]]
  • User: Specify to create a user account, the format is user_name' @'host_name. Here user_name is the user name, host_name is the host name

  • IDENTIFIED BY clause: is used to specify the user password. New users do not need to have an initial password. If the user does not have a password, this clause can be omitted.

  • PASSWORD 'password': PASSWORD means using a hash value to set the password. This parameter is optional. If the password is a plain string, there is no need to use the PASSWORD keyword. 'password' represents the password used by the user to log in and needs to be enclosed in single quotes.

2. Use the INSERT statement to create a new user

You can use the INSERT statement to add user information to mysql.user table, but must have INSERT permission on the mysql.user table. Usually the INSERT statement only adds the values ​​​​of the three fields Host, User and authentication_string.

The password field in the user table of MySQL 5.7 has changed from Password to authentication_string. If you are using a version before MySQL 5.7, just replace the authentication_string field with Password.

INSERT INTO mysql.user(Host, User,  authentication_string, ssl_cipher, x509_issuer, x509_subject) VALUES (&#39;hostname&#39;, &#39;username&#39;, PASSWORD(&#39;password&#39;), &#39;&#39;, &#39;&#39;, &#39;&#39;);

#3. Use the GRANT statement to create a new user

Although both the CREATE USER and INSERT INTO statements can create ordinary users, this Two methods are inconvenient to grant user permissions. So MySQL provides the GRANT statement.

GRANT priv_type ON database.table TO user [IDENTIFIED BY [PASSWORD] &#39;password&#39;]
  • The priv_type parameter indicates the permissions of the new user;

  • The database.table parameter indicates the permission scope of the new user, that is, it can only be used within the specified Use your own permissions on the database and tables;

  • The user parameter specifies the new user's account, which consists of the user name and host name;

  • IDENTIFIED BY Keywords are used to set passwords;

  • The password parameter represents the password of the new user.

Recommendation: "mysql tutorial"

The above is the detailed content of How to create a user 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