Rechercher dans le manuel MySQL
13.7.1.6 GRANT Syntax
- priv_type [(column_list)]
- [, priv_type [(column_list)]] ...
- object_type: {
- | FUNCTION
- }
- priv_level: {
- *
- | *.*
- | db_name.*
- | db_name.tbl_name
- | tbl_name
- | db_name.routine_name
- }
- user_or_role: {
- | role
- }
- user:
- (see Section 6.2.4, “Specifying Account Names”)
- role:
- (see Section 6.2.5, “Specifying Role Names”)
The GRANT
statement assigns
privileges and roles to MySQL user accounts and roles. There are
several aspects to the GRANT
statement, described under the following topics:
GRANT General Overview
The GRANT
statement enables
system administrators to grant privileges and roles, which can
be granted to user accounts and roles. These syntax
restrictions apply:
The GRANT
statement enables
system administrators to grant privileges and roles, which can
be granted to user accounts and roles. These syntax
restrictions apply:
GRANT
cannot mix granting both privileges and roles in the same statement. A givenGRANT
statement must grant either privileges or roles.The
ON
clause distinguishes whether the statement grants privileges or roles:With
ON
, the statement grants privileges.Without
ON
, the statement grants roles.It is permitted to assign both privileges and roles to an account, but you must use separate
GRANT
statements, each with syntax appropriate to what is to be granted.
For more information about roles, see Section 6.3.4, “Using Roles”.
To use GRANT
, you must have the
GRANT OPTION
privilege, and you
must have the privileges that you are granting. When the
read_only
system variable is
enabled, GRANT
additionally
requires the CONNECTION_ADMIN
or SUPER
privilege.
GRANT
either succeeds for all
named users and roles or rolls back and has no effect if any
error occurs. The statement is written to the binary log only
if it succeeds for all named users and roles.
The REVOKE
statement is related
to GRANT
and enables
administrators to remove account privileges. See
Section 13.7.1.8, “REVOKE Syntax”.
Each account name uses the format described in Section 6.2.4, “Specifying Account Names”. Each role name uses the format described in Section 6.2.5, “Specifying Role Names”. For example:
The host name part of the account or role name, if omitted,
defaults to '%'
.
Normally, a database administrator first uses
CREATE USER
to create an
account and define its nonprivilege characteristics such as
its password, whether it uses secure connections, and limits
on access to server resources, then uses
GRANT
to define its privileges.
ALTER USER
may be used to
change the nonprivilege characteristics of existing accounts.
For example:
From the mysql program,
GRANT
responds with
Query OK, 0 rows affected
when executed
successfully. To determine what privileges result from the
operation, use SHOW GRANTS
. See
Section 13.7.6.21, “SHOW GRANTS Syntax”.
Under some circumstances,
GRANT
may be recorded in
server logs or on the client side in a history file such as
~/.mysql_history
, which means that
cleartext passwords may be read by anyone having read access
to that information. For information about the conditions
under which this occurs for the server logs and how to
control it, see Section 6.1.2.3, “Passwords and Logging”. For
similar information about client-side logging, see
Section 4.5.1.3, “mysql Client Logging”.
GRANT
supports host names up to
60 characters long. User names can be up to 32 characters.
Database, table, column, and routine names can be up to 64
characters.
Do not attempt to change the permissible length
for user names by altering the mysql.user
system table. Doing so results in unpredictable behavior
which may even make it impossible for users to log in to the
MySQL server. Never alter the structure of tables
in the mysql
system database in any
manner except by means of the procedure described in
Section 4.4.5, “mysql_upgrade — Check and Upgrade MySQL Tables”.
Several objects within GRANT
statements are subject to quoting, although quoting is
optional in many cases: Account, role, database, table,
column, and routine names. For example, if a
user_name
or
host_name
value in an account name
is legal as an unquoted identifier, you need not quote it.
However, quotation marks are necessary to specify a
user_name
string containing special
characters (such as -
), or a
host_name
string containing special
characters or wildcard characters (such as
%
); for example,
'test-user'@'%.com'
. Quote the user name
and host name separately.
To specify quoted values:
Quote database, table, column, and routine names as identifiers.
Quote user names and host names as identifiers or as strings.
Quote passwords as strings.
For string-quoting and identifier-quoting guidelines, see Section 9.1.1, “String Literals”, and Section 9.2, “Schema Object Names”.
The _
and %
wildcards
are permitted when specifying database names in
GRANT
statements that grant
privileges at the database level (GRANT ... ON
). This means,
for example, that to use a db_name
.*_
character as
part of a database name, specify it as \_
in the GRANT
statement, to
prevent the user from being able to access additional
databases matching the wildcard pattern; for example,
GRANT ... ON `foo\_bar`.* TO ...
.
When a database name not is used to grant privileges at the
database level, but as a qualifier for granting privileges to
some other object such as a table or routine (for example,
GRANT ... ON
),
wildcard characters are treated as normal characters.
db_name
.tbl_name
A user
value in a
GRANT
statement indicates a
MySQL account to which the statement applies. To accommodate
granting rights to users from arbitrary hosts, MySQL supports
specifying the user
value in the
form
'
.
user_name
'@'host_name
'
You can specify wildcards in the host name. For example,
'
applies to user_name
'@'%.example.com'user_name
for any host
in the example.com
domain, and
'
applies to user_name
'@'198.51.100.%'user_name
for any host
in the 198.51.100
class C subnet.
The simple form
'
is a
synonym for
user_name
''
.
user_name
'@'%'
MySQL does not support wildcards in user
names. To refer to an anonymous user, specify an
account with an empty user name with the
GRANT
statement:
In this case, any user who connects from the local host with the correct password for the anonymous user will be permitted access, with the privileges associated with the anonymous-user account.
For additional information about user name and host name values in account names, see Section 6.2.4, “Specifying Account Names”.
If you permit local anonymous users to connect to the MySQL
server, you should also grant privileges to all local users
as
'
.
Otherwise, the anonymous user account for
user_name
'@'localhost'localhost
in the
mysql.user
system table is used when
named users try to log in to the MySQL server from the local
machine. For details, see
Section 6.2.6, “Access Control, Stage 1: Connection Verification”.
To determine whether this issue applies to you, execute the following query, which lists any anonymous users:
To avoid the problem just described, delete the local anonymous user account using this statement:
The following tables summarize the permissible static and
dynamic priv_type
privilege types
that can be specified for the
GRANT
and
REVOKE
statements, and the
levels at which each privilege can be granted. For additional
information about each privilege, see
Section 6.2.1, “Privileges Provided by MySQL”. For information about
the differences between static and dynamic privileges, see
Section 6.2.2, “Static Versus Dynamic Privileges”.
Table 13.9 Permissible Static Privileges for GRANT and REVOKE
Privilege | Meaning and Grantable Levels |
---|---|
ALL [PRIVILEGES] |
Grant all privileges at specified access level except
GRANT OPTION and
PROXY . |
ALTER |
Enable use of ALTER TABLE . Levels:
Global, database, table. |
ALTER ROUTINE |
Enable stored routines to be altered or dropped. Levels: Global, database, routine. |
CREATE |
Enable database and table creation. Levels: Global, database, table. |
CREATE ROLE |
Enable role creation. Level: Global. |
CREATE ROUTINE |
Enable stored routine creation. Levels: Global, database. |
CREATE TABLESPACE |
Enable tablespaces and log file groups to be created, altered, or dropped. Level: Global. |
CREATE TEMPORARY TABLES |
Enable use of CREATE
TEMPORARY TABLE . Levels: Global, database. |
CREATE USER |
Enable use of CREATE USER ,
DROP USER ,
RENAME USER , and
REVOKE ALL
PRIVILEGES . Level: Global. |
CREATE VIEW |
Enable views to be created or altered. Levels: Global, database, table. |
DELETE |
Enable use of DELETE . Level: Global,
database, table. |
DROP |
Enable databases, tables, and views to be dropped. Levels: Global, database, table. |
DROP ROLE |
Enable roles to be dropped. Level: Global. |
EVENT |
Enable use of events for the Event Scheduler. Levels: Global, database. |
EXECUTE |
Enable the user to execute stored routines. Levels: Global, database, routine. |
FILE |
Enable the user to cause the server to read or write files. Level: Global. |
GRANT OPTION |
Enable privileges to be granted to or removed from other accounts. Levels: Global, database, table, routine, proxy. |
INDEX |
Enable indexes to be created or dropped. Levels: Global, database, table. |
INSERT |
Enable use of INSERT . Levels: Global,
database, table, column. |
LOCK TABLES |
Enable use of LOCK TABLES on tables for
which you have the SELECT
privilege. Levels: Global, database. |
PROCESS |
Enable the user to see all processes with SHOW
PROCESSLIST . Level: Global. |
PROXY |
Enable user proxying. Level: From user to user. |
REFERENCES |
Enable foreign key creation. Levels: Global, database, table, column. |
RELOAD |
Enable use of FLUSH operations. Level:
Global. |
REPLICATION CLIENT |
Enable the user to ask where master or slave servers are. Level: Global. |
REPLICATION SLAVE |
Enable replication slaves to read binary log events from the master. Level: Global. |
SELECT |
Enable use of SELECT . Levels: Global,
database, table, column. |
SHOW DATABASES |
Enable SHOW DATABASES to show all
databases. Level: Global. |
SHOW VIEW |
Enable use of SHOW CREATE VIEW . Levels:
Global, database, table. |
SHUTDOWN |
Enable use of mysqladmin shutdown. Level: Global. |
SUPER |
Enable use of other administrative operations such as
CHANGE MASTER TO ,
KILL ,
PURGE BINARY LOGS ,
SET
GLOBAL , and mysqladmin
debug command. Level: Global. |
TRIGGER |
Enable trigger operations. Levels: Global, database, table. |
UPDATE |
Enable use of UPDATE . Levels: Global,
database, table, column. |
USAGE |
Synonym for “no privileges” |
Table 13.10 Permissible Dynamic Privileges for GRANT and REVOKE
Privilege | Meaning and Grantable Levels |
---|---|
APPLICATION_PASSWORD_ADMIN |
Enable dual password administration. Level: Global. |
AUDIT_ADMIN |
Enable audit log configuration. Level: Global. |
BACKUP_ADMIN |
Enable backup administration. Level: Global. |
BINLOG_ADMIN |
Enable binary log control. Level: Global. |
BINLOG_ENCRYPTION_ADMIN |
Enable activation and deactivation of binary log encryption. Level: Global. |
CONNECTION_ADMIN |
Enable connection limit/restriction control. Level: Global. |
ENCRYPTION_KEY_ADMIN |
Enable InnoDB key rotation. Level: Global. |
FIREWALL_ADMIN |
Enable firewall rule administration, any user. Level: Global. |
FIREWALL_USER |
Enable firewall rule administration, self. Level: Global. |
GROUP_REPLICATION_ADMIN |
Enable Group Replication control. Level: Global. |
PERSIST_RO_VARIABLES_ADMIN |
Enable persisting read-only system variables. Level: Global. |
REPLICATION_SLAVE_ADMIN |
Enable regular replication control. Level: Global. |
RESOURCE_GROUP_ADMIN |
Enable resource group administration. Level: Global. |
RESOURCE_GROUP_USER |
Enable resource group administration. Level: Global. |
ROLE_ADMIN |
Enable use of WITH ADMIN OPTION . Level: Global. |
SESSION_VARIABLES_ADMIN |
Enable setting restricted session system variables. Level: Global. |
SET_USER_ID |
Enable setting non-self DEFINER values. Level:
Global. |
SYSTEM_VARIABLES_ADMIN |
Enable modifying or persisting global system variables. Level: Global. |
VERSION_TOKEN_ADMIN |
Enable use of Version Tokens UDFs. Level: Global. |
XA_RECOVER_ADMIN |
Enable XA
RECOVER execution. Level: Global. |
A trigger is associated with a table. To create or drop a
trigger, you must have the
TRIGGER
privilege for the
table, not the trigger.
In GRANT
statements, the
ALL
[PRIVILEGES]
or PROXY
privilege must be named by itself and cannot be specified
along with other privileges.
ALL
[PRIVILEGES]
stands for all privileges available for
the level at which privileges are to be granted except for the
GRANT OPTION
and
PROXY
privileges.
MySQL account information is stored in the tables of the
mysql
system database. For additional
details, consult Section 6.2, “The MySQL Access Privilege System”, which
discusses the mysql
system database and the
access control system extensively.
If the grant tables hold privilege rows that contain
mixed-case database or table names and the
lower_case_table_names
system
variable is set to a nonzero value,
REVOKE
cannot be used to revoke
these privileges. It will be necessary to manipulate the grant
tables directly. (GRANT
will
not create such rows when
lower_case_table_names
is
set, but such rows might have been created prior to setting
that variable. The
lower_case_table_names
setting can only be configured at server startup.)
Privileges can be granted at several levels, depending on the
syntax used for the ON
clause. For
REVOKE
, the same
ON
syntax specifies which privileges to
remove.
For the global, database, table, and routine levels,
GRANT ALL
assigns only the privileges that exist at the level you are
granting. For example, GRANT ALL ON
is a
database-level statement, so it does not grant any global-only
privileges such as db_name
.*FILE
.
Granting ALL
does not assign
the GRANT OPTION
or
PROXY
privilege.
The object_type
clause, if present,
should be specified as TABLE
,
FUNCTION
, or PROCEDURE
when the following object is a table, a stored function, or a
stored procedure.
The privileges that a user holds for a database, table,
column, or routine are formed additively as the logical
OR
of the account privileges at
each of the privilege levels. For example, if a user has a
global SELECT
privilege, the
privilege cannot be denied by an absence of the privilege at
the database, table, or column level. Details of the
privilege-checking procedure are presented in
Section 6.2.7, “Access Control, Stage 2: Request Verification”.
If you are using table, column, or routine privileges for even one user, the server examines table, column, and routine privileges for all users and this slows down MySQL a bit. Similarly, if you limit the number of queries, updates, or connections for any users, the server must monitor these values.
MySQL enables you to grant privileges on databases or tables
that do not exist. For tables, the privileges to be granted
must include the CREATE
privilege. This behavior is by design,
and is intended to enable the database administrator to
prepare user accounts and privileges for databases or tables
that are to be created at a later time.
MySQL does not automatically revoke any privileges when you drop a database or table. However, if you drop a routine, any routine-level privileges granted for that routine are revoked.
Global privileges are administrative or apply to all databases
on a given server. To assign global privileges, use
ON *.*
syntax:
The CREATE TABLESPACE
,
CREATE USER
,
FILE
,
PROCESS
,
RELOAD
,
REPLICATION CLIENT
,
REPLICATION SLAVE
,
SHOW DATABASES
,
SHUTDOWN
, and
SUPER
static privileges are
administrative and can only be granted globally.
Dynamic privileges are all global and can only be granted globally.
Other privileges can be granted globally or at more specific levels.
The effect of GRANT OPTION
granted at the global level differs for static and dynamic
privileges:
GRANT OPTION
granted for any static global privilege applies to all static global privileges.GRANT OPTION
granted for any dynamic privilege applies only to that dynamic privilege.
GRANT ALL
at the global level grants all
static global privileges and all currently registered dynamic
privileges. A dynamic privilege registered subsequent to
execution of the GRANT
statement is not
granted retroactively to any account.
MySQL stores global privileges in the
mysql.user
system table.
Database privileges apply to all objects in a given database.
To assign database-level privileges, use ON
syntax:
db_name
.*
If you use ON *
syntax (rather than
ON *.*
), privileges are assigned at the
database level for the default database. An error occurs if
there is no default database.
The CREATE
,
DROP
,
EVENT
,
GRANT OPTION
,
LOCK TABLES
, and
REFERENCES
privileges can be
specified at the database level. Table or routine privileges
also can be specified at the database level, in which case
they apply to all tables or routines in the database.
MySQL stores database privileges in the
mysql.db
system table.
Table privileges apply to all columns in a given table. To
assign table-level privileges, use ON
syntax:
db_name.tbl_name
If you specify tbl_name
rather than
db_name.tbl_name
, the statement
applies to tbl_name
in the default
database. An error occurs if there is no default database.
The permissible priv_type
values at
the table level are ALTER
,
CREATE VIEW
,
CREATE
,
DELETE
,
DROP
,
GRANT OPTION
,
INDEX
,
INSERT
,
REFERENCES
,
SELECT
,
SHOW VIEW
,
TRIGGER
, and
UPDATE
.
Table-level privileges apply to base tables and views. They do
not apply to tables created with CREATE
TEMPORARY TABLE
, even if the table names match. For
information about TEMPORARY
table
privileges, see Section 13.1.20.3, “CREATE TEMPORARY TABLE Syntax”.
MySQL stores table privileges in the
mysql.tables_priv
system table.
Column privileges apply to single columns in a given table. Each privilege to be granted at the column level must be followed by the column or columns, enclosed within parentheses.
The permissible priv_type
values
for a column (that is, when you use a
column_list
clause) are
INSERT
,
REFERENCES
,
SELECT
, and
UPDATE
.
MySQL stores column privileges in the
mysql.columns_priv
system table.
The ALTER ROUTINE
,
CREATE ROUTINE
,
EXECUTE
, and
GRANT OPTION
privileges apply
to stored routines (procedures and functions). They can be
granted at the global and database levels. Except for
CREATE ROUTINE
, these
privileges can be granted at the routine level for individual
routines.
The permissible priv_type
values at
the routine level are ALTER
ROUTINE
, EXECUTE
, and
GRANT OPTION
.
CREATE ROUTINE
is not a
routine-level privilege because you must have the privilege at
the global or database level to create a routine in the first
place.
MySQL stores routine-level privileges in the
mysql.procs_priv
system table.
The PROXY
privilege enables one
user to be a proxy for another. The proxy user impersonates or
takes the identity of the proxied user; that is, it assumes
the privileges of the proxied user.
When PROXY
is granted, it must
be the only privilege named in the
GRANT
statement, and the only
permitted WITH
option is WITH
GRANT OPTION
.
Proxying requires that the proxy user authenticate through a
plugin that returns the name of the proxied user to the server
when the proxy user connects, and that the proxy user have the
PROXY
privilege for the proxied user. For
details and examples, see Section 6.3.11, “Proxy Users”.
MySQL stores proxy privileges in the
mysql.proxies_priv
system table.
GRANT
syntax without an
ON
clause grants roles rather than
individual privileges. A role is a named collection of
privileges; see Section 6.3.4, “Using Roles”. For example:
Each role to be granted must exist, as well as each user account or role to which it is to be granted.
If the GRANT
statement includes
the WITH ADMIN OPTION
clause, each named
user becomes able to grant the named roles to other users or
roles, or revoke them from other users or roles. This includes
the ability to use WITH ADMIN OPTION
itself.
It is possible to create circular references with
GRANT
. For example:
Circular grant references are permitted but add no new privileges or roles to the grantee because a user or role already has its privileges and roles.
The optional WITH
clause is used to enable
a user to grant privileges to other users. The WITH
GRANT OPTION
clause gives the user the ability to
give to other users any privileges the user has at the
specified privilege level.
To grant the GRANT OPTION
privilege to an account without otherwise changing its
privileges, do this:
Be careful to whom you give the GRANT
OPTION
privilege because two users with different
privileges may be able to combine privileges!
You cannot grant another user a privilege which you yourself
do not have; the GRANT OPTION
privilege enables you to assign only those privileges which
you yourself possess.
Be aware that when you grant a user the
GRANT OPTION
privilege at a
particular privilege level, any privileges the user possesses
(or may be given in the future) at that level can also be
granted by that user to other users. Suppose that you grant a
user the INSERT
privilege on a
database. If you then grant the
SELECT
privilege on the
database and specify WITH GRANT OPTION
,
that user can give to other users not only the
SELECT
privilege, but also
INSERT
. If you then grant the
UPDATE
privilege to the user on
the database, the user can grant
INSERT
,
SELECT
, and
UPDATE
.
For a nonadministrative user, you should not grant the
ALTER
privilege globally or for
the mysql
system database. If you do that,
the user can try to subvert the privilege system by renaming
tables!
For additional information about security risks associated with particular privileges, see Section 6.2.1, “Privileges Provided by MySQL”.
The biggest differences between the MySQL and standard SQL
versions of GRANT
are:
MySQL associates privileges with the combination of a host name and user name and not with only a user name.
Standard SQL does not have global or database-level privileges, nor does it support all the privilege types that MySQL supports.
MySQL does not support the standard SQL
UNDER
privilege.Standard SQL privileges are structured in a hierarchical manner. If you remove a user, all privileges the user has been granted are revoked. This is also true in MySQL if you use
DROP USER
. See Section 13.7.1.5, “DROP USER Syntax”.In standard SQL, when you drop a table, all privileges for the table are revoked. In standard SQL, when you revoke a privilege, all privileges that were granted based on that privilege are also revoked. In MySQL, privileges can be dropped with
DROP USER
orREVOKE
statements.In MySQL, it is possible to have the
INSERT
privilege for only some of the columns in a table. In this case, you can still executeINSERT
statements on the table, provided that you insert values only for those columns for which you have theINSERT
privilege. The omitted columns are set to their implicit default values if strict SQL mode is not enabled. In strict mode, the statement is rejected if any of the omitted columns have no default value. (Standard SQL requires you to have theINSERT
privilege on all columns.) For information about strict SQL mode and implicit default values, see Section 5.1.11, “Server SQL Modes”, and Section 11.7, “Data Type Default Values”.
Traduction non disponible
Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-grant.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.