The GRANT statement can be used in mysql to add permissions to users. The syntax is "GRANT permission type ON permission level value TO user [IDENTIFIED BY 'password'] [WITH clause];"; where the parameter "user" represents the user account , the format is "'username'@'hostname'".
The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.
In MySQL, you can use the GRANT statement to authorize users and increase their permissions.
The syntax format is as follows:
GRANT priv_type [(column_list)] ON database.table TO user [IDENTIFIED BY 'password'] [WITH with_option [with_option]...]
Among them:
priv_type parameter indicates the permission type;
The columns_list parameter indicates which columns the permissions apply to. When this parameter is omitted, it means it applies to the entire table;
database.table is used to specify the level of permissions;
The user parameter represents the user account, which is composed of user name and host name. The format is "'username'@'hostname'";
IDENTIFIED BY parameter is used to set the user name Password;
# The password parameter is the user's new password.
The permissions that can be granted in MySQL are as follows:
Column permissions are related to a specific column in the table. For example, you can use the UPDATE statement to update permissions on the value of the name column in the students table.
Table permissions are related to all data in a specific table. For example, you can use the SELECT statement to query the permissions for all data in the students table.
Database permissions are related to all tables in a specific database. For example, you can create new tables in the existing database mytest.
User permissions are related to all databases in MySQL. For example, you can delete an existing database or create a new database.
Correspondingly, the values that can be used to specify the permission level in the GRANT statement have the following formats:
*: indicates the current database of all tables.
*.*: Indicates all tables in all databases.
db_name.*: Indicates all tables in a database, db_name specifies the database name.
db_name.tbl_name: Represents a table or view in a database, db_name specifies the database name, tbl_name specifies the table name or view name.
db_name.routine_name: Represents a stored procedure or function in a database, routine_name specifies the stored procedure name or function name.
TO clause: If permission is granted to a user that does not exist, MySQL will automatically execute a CREATE USER statement to create the user, but a password must be set for the user.
In MySQL, only users with GRANT permissions can execute GRANT statements.
Example:
Use the GRANT statement to create a new user testUser with the password testPwd. User testUser has query and insert permissions on all data, and is granted GRANT permissions.
mysql> GRANT SELECT,INSERT ON *.* -> TO 'testUser'@'localhost' -> IDENTIFIED BY 'testPwd' -> WITH GRANT OPTION; Query OK, 0 rows affected, 1 warning (0.05 sec)
Use the SHOW GRANTS statement to query the permissions of user testUser, as shown below.
Extended knowledge: Permission type description
1) When granting database permissions, the
Permission name | corresponds to the field in the user table | Description |
---|---|---|
SELECT | Select_priv | means granting the user permission to use the SELECT statement to access all tables and views in a specific database. |
INSERT | Insert_priv | Indicates that the user is granted permission to use the INSERT statement to add data rows to all tables in a specific database. |
DELETE | Delete_priv | Indicates that the user is granted permission to use the DELETE statement to delete data rows from all tables in a specific database. |
UPDATE | Update_priv | means granting the user permission to use the UPDATE statement to update the values of all data tables in a specific database. |
REFERENCES | References_priv | indicates that the user is granted permission to create foreign keys pointing to tables in a specific database. |
CREATE | Create_priv | Represents the authority that authorizes a user to create a new table in a specific database using the CREATE TABLE statement. |
ALTER | Alter_priv | Indicates that the user is granted permission to use the ALTER TABLE statement to modify all data tables in a specific database. |
SHOW VIEW | Show_view_priv | Indicates that the user is granted permission to view the view definitions of existing views in a specific database. |
CREATE ROUTINE | Create_routine_priv | means granting the user permission to create stored procedures and stored functions for a specific database. |
ALTER ROUTINE | Alter_routine_priv | indicates that the user is granted permission to update and delete existing stored procedures and stored functions in the database. |
INDEX | Index_priv | indicates that the user is granted permission to define and delete indexes on all data tables in a specific database. |
DROP | Drop_priv | means granting the user permission to delete all tables and views in a specific database. |
CREATE TEMPORARY TABLES | Create_tmp_table_priv | Indicates that the user is granted permission to create temporary tables in a specific database. |
CREATE VIEW | Create_view_priv | means granting the user permission to create new views in a specific database. |
EXECUTE ROUTINE | Execute_priv | means granting the user permission to call stored procedures and stored functions of a specific database. |
LOCK TABLES | Lock_tables_priv | indicates that the user is granted permission to lock existing data tables of a specific database. |
ALL or ALL PRIVILEGES or SUPER | Super_priv | means all the above permissions/super permissions |
2) When granting table permissions,
Permission name | corresponds to the user table Field | Description |
---|---|---|
SELECT | Select_priv | Grants users permission to access specific tables using the SELECT statement |
INSERT | Insert_priv | Grants the user permission to add rows to a specific table using the INSERT statement |
DELETE | Delete_priv | Grants the user permission to delete data rows from a specific table using the DELETE statement |
DROP | Drop_priv | Grant users the permission to delete data tables |
UPDATE | Update_priv | Grant users the permission to update specific data tables using the UPDATE statement Permissions |
ALTER | Alter_priv | Grants users permission to use the ALTER TABLE statement to modify the data table |
REFERENCES | References_priv | Grants the user permission to create a foreign key to reference a specific data table |
CREATE | Create_priv | Grants the user permission to create a data table using a specific name |
INDEX | Index_priv | Grants the user the permission to create a data table in the table Permissions defined on the index |
ALL or ALL PRIVILEGES or SUPER | Super_priv | All permission names |
3) When granting column permissions, the value of
4) The most efficient permission is user permission.
When granting user permissions, the
mysql video tutorial]
The above is the detailed content of How to increase permissions in mysql. For more information, please follow other related articles on the PHP Chinese website!