Purpose
This statement is used to grant privileges to a specified user or role, or to grant a role to a user or role.
Limitations and considerations
Circular GRANT operations are not supported. For example, if Role A is granted to Role B, and Role B is granted to Role C, then granting Role C to Role A will result in an error.
Required privileges
Privileges required for granting privileges to a user or role
When executing a
GRANTstatement, the current user must have the corresponding privileges. For example, if useruser1wants to grant theSELECTprivilege on tabletbl1to useruser2, useruser1must already have theSELECTprivilege on tabletbl1.When executing a
GRANTstatement, the current user must have theGRANT OPTIONprivilege. For more information about privileges in OceanBase Database, see Privilege types in MySQL mode.
Note
After privileges are granted to a user, the user must reconnect to OceanBase Database for the privileges to take effect.
Privileges required for granting roles to a user or another role
- If the current user has the
SUPERprivilege, they canGRANTall roles. - When granting a role to other users or roles, the current user must own the role being granted and have the
ADMIN OPTIONprivilege for the grant to succeed. For more information about how to view the privileges owned by the current user, see Query user privileges.
Syntax
Grant object privileges to a user or role
GRANT priv_type[(column_name_list)] [, priv_type[(column_name_list)] ...]
ON [object_type] priv_level
TO {user [, user...]}
[WITH GRANT OPTION];
column_name_list:
column_name [, column_name ...]
object_type:
TABLE
| FUNCTION
| PROCEDURE
| CATALOG
user:
user_or_role
| user_name IDENTIFIED [WITH auth_plugin] BY password
| user_name IDENTIFIED [WITH auth_plugin] BY PASSWORD password
user_or_role:
user_name | role_name
Grant a role to a user or role
GRANT role_name [, role_name ...]
TO user_or_role [, user_or_role ...]
[WITH ADMIN OPTION];
user_or_role:
user_name | role_name
Parameters
| Parameter | Description |
|---|---|
| priv_type | The type of privileges to grant. Multiple privilege types can be granted by separating them with commas (,). For more information, see Privilege types in MySQL mode. Note: The PROXY privilege is currently supported only in the syntax, and cannot be used. |
| column_name_list | Optional. A list used to grant column-level privileges, where the granted permissions apply only to the specified columns. If not specified, the privileges apply to the entire table or object. For more information, see Directly grant privileges. |
| object_type | Optional. Specifies the type of object to which the privilege is being granted. For more information, see object_type below. |
| priv_level | Specifies the level at which the privileges are granted. The format is as follows:
|
| user | Specifies the user(s) to whom the privileges are granted. It can be one or more users, separated by commas (,). If the user does not exist, this statement will directly create the user. |
| auth_plugin | Specifies the authentication method for the user. Currently, only the mysql_native_password authentication plugin is supported. |
| BY password | Specifies a plaintext password for the user being granted privileges. After being saved in the mysql.user table, the server will store the password in encrypted form. If the password contains special characters such as ~!@#%^&*_-+=`|(){}[]:;',.?/, it must be enclosed in single quotes ('') or double quotes (""). |
| BY PASSWORD password | Specifies an encrypted password for the user being granted privileges. This encrypted password will be stored directly in the mysql.user table. |
| WITH GRANT OPTION | Specifies whether the granted privileges can be further granted to other users. When revoking privileges, this will cascade. |
| role_name | Specifies the name of the role. |
| WITH ADMIN OPTION | Specifies whether the role's privileges can be further granted to others. When revoking privileges, this will not cascade. |
object_type
Notice
When you use the object_type clause, make sure that the specified objects are of the corresponding types. Otherwise, a syntax error will occur.
TABLE: indicates that the object type to which the privileges apply is a table.FUNCTION: indicates that the object type to which the privileges apply is a function.PROCEDURE: indicates that the object type to which the privileges apply is a stored procedure.CATALOG: indicates that the object type to which the privileges apply is a catalog.Note
For OceanBase Database V4.3.5, the object type
CATALOGis introduced starting from V4.3.5 BP2.
Here are some examples:
Grant the
SELECTprivilege touser001on all objects in thetestdatabase. Since the specified objects are not of the specified type, an error occurs.GRANT SELECT ON TABLE test.* TO user001;The return result is as follows:
ERROR 1144 (42000): Illegal GRANT/REVOKE command; please consult the manual to see which privileges can be usedGrant the
SELECTprivilege touser001on thetbl1table in thetestdatabase.GRANT SELECT ON TABLE test.tbl1 TO user001;Grant the execute privilege to
user002on thecalculate_salaryfunction in thetestdatabase.GRANT EXECUTE ON FUNCTION test.calculate_salary TO user002;Grant the execute privilege to
user003on thepro_generate_datastored procedure in thetestdatabase.GRANT EXECUTE ON PROCEDURE test.pro_generate_data TO user003;Grant
user004theSELECTandUSE CATALOGprivileges on the catalog objecttest_odps_catalog.GRANT SELECT, USE CATALOG ON CATALOG test_odps_catalog TO user004 WITH GRANT OPTION;
Examples
Example 1: Grant object privileges
Grant the existing user
user1theCREATE VIEWprivilege on databasedb1and allow the privilege to be granted to other users.GRANT CREATE VIEW ON db1.* TO user1 WITH GRANT OPTION;Grant the existing user
user1theCREATEprivilege on databasedb1and updateuser1's password.GRANT CREATE ON db1.* TO user1 IDENTIFIED BY '********';After execution, check the
mysql.usertable for the password of useruser1, and you will see it has been updated to the new password.Grant the non-existent user
user2theCREATEprivilege on databasedb1and set a password foruser2.GRANT CREATE ON db1.* TO user2 IDENTIFIED BY '********';Grant the existing user
user001theSELECTprivilege on columncol1of the tabletbl1in the databasetest.GRANT SELECT(col1) ON test.tbl1 TO user001;Grant the user
user005global-level privilegesCREATE CATALOGandUSE CATALOG.GRANT CREATE CATALOG, USE CATALOG ON *.* TO user004 WITH GRANT OPTION;
Example 2: Grant a role to a user or role
Grant the privileges of role
role001to rolerole002with the ability to grant it further.GRANT role001 TO role002 WITH ADMIN OPTION;Grant the privileges of role
role001to useruser001with the ability to grant it further.GRANT role001 TO user001 WITH ADMIN OPTION;
References
- For more information about how to grant privileges, see Grant privileges.
- For more information about how to view user privileges, see View user privileges.
- For more information about how to query the
mysql.usertable, see mysql.user. - For more information about how to add privileges to a role, see Add privileges to a role.
- For more information about how to grant a role to a user or another role, see Grant a role to a user or role.
- For more information about how to activate a role, see Activate a role.
- For more information about indirect authorization, see Indirect authorization