Home > Database > Mysql Tutorial > body text

How to create users and grant permissions in MySQL

不言
Release: 2019-03-01 10:33:04
Original
11785 people have browsed it

How to create a MySQL user and grant permissions? For good security, separate user accounts need to be created for each application rather than root accessing the database. This will ensure that applications cannot access other applications' databases. Therefore mysql administrator (root) rights are required to create user accounts and assign permissions to the database.

How to create users and grant permissions in MySQL

For information, the MySQL root account is different from the system root account and there is no relationship between them. (Related recommendations: MySQL Tutorial)

1. Create a new user in MySQL

Use the root user with shell access to log in to the MySQL server and create a new user named "rahul". The following command only allows access to user rahul's MySQL server from the localhost system.

mysql> CREATE USER 'rahul'@'localhost' IDENTIFIED BY 'password';
Copy after login

Now assign permissions to a specific database. The following command will allow user rahul to have all permissions on the database "mydb".

mysql> GRANT ALL ON mydb.* TO 'rahul'@'localhost';
Copy after login

After creating the user and assigning the appropriate permissions, make sure to reload the permissions.

mysql> FLUSH PRIVILEGES;
Copy after login

2. Create a MySQL user with remote access

Allow any user to connect to the MySQL server from a remote system. You need to specify the hostname or IP address of the remote system. You can also use % to allow any host

mysql> CREATE USER 'rahul'@'123.45.67.89' IDENTIFIED BY 'password';
mysql> CREATE USER 'rahul'@'%' IDENTIFIED BY 'password';
mysql> FLUSH PRIVILEGES;
Copy after login

3. Grant specific user permissions in MySQL

The following is a list of common permissions for MySQL users. Visit here for a complete list of permissions for MySQL users.

ALL [PRIVILEGES] - Grants the user all permissions.

CREATE - Grants the user permission to create new databases and tables.

DROP - Grants the user permission to delete (drop) databases and tables.

DELETE - Grants the user permission to delete rows in the table.

ALTER - Grants the user permission to modify the table structure.

INSERT - Grants the user permission to insert (add) rows into the table.

SELECT - Grants the user permission to run the select command to read data from the table.

UPDATE - Grants the user permission to update data in the table.

EXECUTE - Grants the user permission to execute a stored routine.

FILE - Grants the user access to files on the server host.

GRANT OPTION - Grants a user the ability to grant or remove permissions from other users.

Here, you can specify comma-separated permissions instead of all permissions. For example, allow CREATE, DELETE, INSERT, UPDATE access to 'rahul'@'localhost' on database mydb.

mysql> GRANT CREATE,DELETE,INSERT,UPDATE ON mydb.* TO 'rahul'@'localhost';
mysql> FLUSH PRIVILEGES;
Copy after login

4. Revoke user permissions in MySQL

Use the REVOKE command to remove any specific permissions from the user. For example, remove DELETE permission from user 'rahul'@'localhost' on mydb database.

mysql> REVOKE DELETE ON mydb.* TO 'rahul'@'localhost';
mysql> FLUSH PRIVILEGES;
Copy after login

5. Delete users in MySQL

You can use the DROP command to delete any user from MySQL. For example, to delete user 'rahul'@'localhost', you can use the following command.

mysql> DROP USER 'rahul'@'localhost';
mysql> FLUSH PRIVILEGES;
Copy after login

The above is the detailed content of How to create users and grant permissions in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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