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:
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.
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.
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.
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!