Home  >  Article  >  Database  >  How to implement the statement to create a user in MySQL?

How to implement the statement to create a user in MySQL?

王林
王林Original
2023-11-08 12:30:251118browse

How to implement the statement to create a user in MySQL?

Title: How to create a user's statement in MySQL? Detailed explanation of specific code examples

In the MySQL database, creating a user and granting corresponding permissions is a very common operation. This article will detail how to use MySQL statements to create users and grant permissions to them. The specific operations are as follows:

  1. Connect to the MySQL database
    First, you need to open a terminal or command line window and connect to the MySQL database through the command line or other tools. Suppose we have a username called "root" and know its password.
mysql -u root -p

After the connection is successful, you will be asked to enter a password. After entering the password, you can enter the MySQL command line mode.

  1. Create a new user
    Use the following statements to create a new user, replacing "new_user" and "new_password" with the username and password you want to create.
CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'new_password';

In the above code, 'new_user' is the username of the new user, 'localhost' means that the user can only connect locally, and IDENTIFIED BY 'new_password' is the login password of the specified user.

If you need to allow users to connect remotely, you can replace 'localhost' with '%', which means that all remote connections are allowed. It should be noted that for security reasons, it is not recommended to open remote connection permissions in a production environment.

  1. Grant permissions

Generally, users do not have any database operation permissions after they are created, and the corresponding permissions need to be granted through authorization statements. The following is an example of an authorization statement:

GRANT ALL PRIVILEGES ON *.* TO 'new_user'@'localhost';

In the above statement, GRANT represents the authorization operation, ALL PRIVILEGES represents all permissions, '.' represents all databases and tables, 'new_user'@'localhost ' is the username and connection source to be authorized.

If you need to grant specific database permissions, you can replace '.' with the database name and table name. For example:

GRANT SELECT, INSERT ON database_name.* TO 'new_user'@'localhost';

The above statement indicates that new_user is granted SELECT and INSERT permissions in the database_name database.

  1. Refresh permissions
    After completing the user creation and authorization operations, you need to refresh the permissions to take effect. You can use the following statement to refresh the permissions:
FLUSH PRIVILEGES;

After executing the above statement, the new user can log in with the specified user name and password and have the corresponding permissions.

Summary
Through the above steps, we can create a new user in the MySQL database and grant permissions to it. It should be noted that security issues should be considered in a production environment and avoid opening too many permissions or allowing remote connections. At the same time, users who are no longer needed must be deleted or disabled in time to ensure the security of the database.

The above is the detailed content of How to implement the statement 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