- MySQL 5.1 Reference Manual :: 12 SQL Statement Syntax :: 12.4 Database Administration Statements :: 12.4.1 Account Management Statements :: 12.4.1.3 GRANT Syntax
-
- MySQL 5.1 Reference Manual
- Preface, Notes, Licenses
- 1 General Information
- 2 Installing and Upgrading MySQL
- 3 Tutorial
- 4 MySQL Programs
- 5 MySQL Server Administration
- 6 Backup and Recovery
- 7 Optimization
- 8 Language Structure
- 9 Internationalization and Localization
- 10 Data Types
- 11 Functions and Operators
- 12 SQL Statement Syntax
- 13 Storage Engines
- 14 High Availability and Scalability
- 15 MySQL Enterprise Monitor
- 16 Replication
- 17 MySQL Cluster NDB 6.X/7.X
- 18 Partitioning
- 19 Stored Programs and Views
- 20 INFORMATION_SCHEMA Tables
- 21 Connectors and APIs
- 22 Extending MySQL
- A MySQL 5.1 Frequently Asked Questions
- B Errors, Error Codes, and Common Problems
- C MySQL Change History
- D Restrictions and Limits
- Index
- Standard Index
- C Function Index
- Command Index
- Function Index
- INFORMATION_SCHEMA Index
- Transaction Isolation Level Index
- JOIN Types Index
- Operator Index
- Option Index
- Privileges Index
- SQL Modes Index
- Status Variable Index
- Statement/Syntax Index
- System Variable Index
GRANT
priv_type
[(column_list
)] [,priv_type
[(column_list
)]] ... ON [object_type
]priv_level
TOuser
[IDENTIFIED BY [PASSWORD] 'password
'] [,user
[IDENTIFIED BY [PASSWORD] 'password
']] ... [REQUIRE {NONE |ssl_option
[[AND]ssl_option
] ...}] [WITHwith_option
...]object_type
: TABLE | FUNCTION | PROCEDUREpriv_level
: * | *.* |db_name
.* |db_name.tbl_name
|tbl_name
|db_name
.routine_name
ssl_option
: SSL | X509 | CIPHER 'cipher
' | ISSUER 'issuer
' | SUBJECT 'subject
'with_option
: GRANT OPTION | MAX_QUERIES_PER_HOURcount
| MAX_UPDATES_PER_HOURcount
| MAX_CONNECTIONS_PER_HOURcount
| MAX_USER_CONNECTIONScount
The
GRANT
statement enables system administrators to grant privileges to MySQL user accounts.GRANT
also serves to specify other account characteristics such as use of secure connections and limits on access to server resources. To useGRANT
, you must have theGRANT OPTION
privilege, and you must have the privileges that you are granting.Normally,
CREATE USER
is used to create an account andGRANT
to define its privileges. However, if an account named in aGRANT
statement does not already exist,GRANT
may create it under the conditions described later in the discussion of theNO_AUTO_CREATE_USER
SQL mode.The
REVOKE
statement is related toGRANT
and enables administrators to remove account privileges. To determine what privileges an account has, useSHOW GRANTS
. See Section 12.4.1.5, “REVOKE
Syntax”, and Section 12.4.5.22, “SHOW GRANTS
Syntax”.Important
Some releases of MySQL introduce changes to the structure of the grant tables to add new privileges or features. Whenever you update to a new version of MySQL, you should update your grant tables to make sure that they have the current structure so that you can take advantage of any new capabilities. See Section 4.4.8, “mysql_upgrade — Check Tables for MySQL Upgrade”.
MySQL Enterprise. For automated notification of users with inappropriate privileges, subscribe to the MySQL Enterprise Monitor. For more information, see http://www.mysql.com/products/enterprise/advisors.html.
The following table summarizes the allowable
priv_type
privilege types that can be specified for theGRANT
andREVOKE
statements. For additional information about these privileges, see Section 5.4.1, “Privileges Provided by MySQL”.Privilege Meaning ALL [PRIVILEGES]
Grant all privileges at specified access level except GRANT OPTION
ALTER
Enable use of ALTER TABLE
ALTER ROUTINE
Enable stored routines to be altered or dropped CREATE
Enable database and table creation CREATE ROUTINE
Enable stored routine creation CREATE TEMPORARY TABLES
Enable use of CREATE TEMPORARY TABLE
CREATE USER
Enable use of CREATE USER
,DROP USER
,RENAME USER
, andREVOKE ALL PRIVILEGES
CREATE VIEW
Enable views to be created or altered DELETE
Enable use of DELETE
DROP
Enable databases, tables, and views to be dropped EVENT
Enable use of events for the Event Scheduler EXECUTE
Enable the user to execute stored routines FILE
Enable the user to cause the server to read or write files GRANT OPTION
Enable privileges to be granted to or removed from other accounts INDEX
Enable indexes to be created or dropped INSERT
Enable use of INSERT
LOCK TABLES
Enable use of LOCK TABLES
on tables for which you have theSELECT
privilegePROCESS
Enable the user to see all processes with SHOW PROCESSLIST
REFERENCES
Not implemented RELOAD
Enable use of FLUSH
operationsREPLICATION CLIENT
Enable the user to ask where master or slave servers are REPLICATION SLAVE
Enable replication slaves to read binary log events from the master SELECT
Enable use of SELECT
SHOW DATABASES
Enable SHOW DATABASES
to show all databasesSHOW VIEW
Enable use of SHOW CREATE VIEW
SHUTDOWN
Enable use of mysqladmin shutdown SUPER
Enable use of other adminstrative operations such as CHANGE MASTER TO
,KILL
,PURGE BINARY LOGS
,SET GLOBAL
, and mysqladmin debug commandTRIGGER
Enable triggers to be created or dropped UPDATE
Enable use of UPDATE
USAGE
Synonym for “no privileges” The
EVENT
andTRIGGER
privileges were added in MySQL 5.1.6. A trigger is associated with a table, so to create or drop a trigger, you must have theTRIGGER
privilege for the table, not the trigger. (Before MySQL 5.1.6, theSUPER
privilege was required to create or drop triggers.)In
GRANT
statements, theALL [PRIVILEGES]
privilege is named by itself and cannot be specified along with other privileges. It stands for all privileges available for the level at which privileges are to be granted except for theGRANT OPTION
privilege.USAGE
can be specified when you want to create a user that has no privileges, or to specify theREQUIRE
orWITH
clauses for an account without changing its existing privileges.MySQL account information is stored in the tables of the
mysql
database. This database and the access control system are discussed extensively in Chapter 5, MySQL Server Administration, which you should consult for additional details.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 whenlower_case_table_names
is set, but such rows might have been created prior to setting the variable.)Privileges can be granted at several levels, depending on the syntax used for the
ON
clause. ForREVOKE
, the sameON
syntax specifies which privileges to take away. The examples shown here include noIDENTIFIED BY '
clause for brevity, but you should include one if the account does not already exist to avoid creating an account with no password.password
'Global Privileges
Global privileges are administrative or apply to all databases on a given server. To assign global privileges, use
ON *.*
syntax:GRANT ALL ON *.* TO 'someuser'@'somehost'; GRANT SELECT, INSERT ON *.* TO 'someuser'@'somehost';
Before MySQL 5.1.12, privileges also are assigned at the global level if you use
ON *
syntax and you have not selected a default database. As of 5.1.12,ON *
requires a default database and produces an error is there is none.The
CREATE USER
,FILE
,PROCESS
,RELOAD
,REPLICATION CLIENT
,REPLICATION SLAVE
,SHOW DATABASES
,SHUTDOWN
, andSUPER
privileges are administrative and can only be granted globally.Other privileges can be granted globally or at more specific levels.
Global privileges are stored in the
mysql.user
table.Database Privileges
Database privileges apply to all objects in a given database. To assign database-level privileges, use
ON
syntax:db_name
.*GRANT ALL ON mydb.* TO 'someuser'@'somehost'; GRANT SELECT, INSERT ON mydb.* TO 'someuser'@'somehost';
Privileges also are assigned at the database level (for the default database) if you use
ON *
syntax and you have selected a default database.The
CREATE
,DROP
,EVENT
, andGRANT OPTION
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.Database privileges are stored in the
mysql.db
andmysql.host
tables.GRANT
andREVOKE
affect thedb
table, but not thehost
table, which is rarely used.Table Privileges
Table privileges apply to all columns in a given table. To assign table-level privileges, use
ON
syntax:db_name.tbl_name
GRANT ALL ON mydb.mytbl TO 'someuser'@'somehost'; GRANT SELECT, INSERT ON mydb.mytbl TO 'someuser'@'somehost';
If you specify
tbl_name
rather thandb_name.tbl_name
, the statement applies totbl_name
in the default database. An error occurs if there is no default database.The allowable
priv_type
values for a table areALTER
,CREATE VIEW
,CREATE
,DELETE
,DROP
,GRANT OPTION
,INDEX
,INSERT
,SELECT
,SHOW VIEW
,TRIGGER
, andUPDATE
.Table privileges are stored in the
mysql.tables_priv
table.Column Privileges
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.
GRANT SELECT (col1), INSERT (col1,col2) ON mydb.mytbl TO 'someuser'@'somehost';
The allowable
priv_type
values for a column (that is, when you use acolumn_list
clause) areINSERT
,SELECT
, andUPDATE
.Column privileges are stored in the
mysql.columns_priv
table.Routine Privileges
The
ALTER ROUTINE
,CREATE ROUTINE
,EXECUTE
, andGRANT OPTION
privileges apply to stored routines (procedures and functions). They can be granted at the global and database levels. Except forCREATE ROUTINE
, these privileges can be granted at the routine level for individual routines.GRANT CREATE ROUTINE ON mydb.* TO 'someuser'@'somehost'; GRANT EXECUTE ON PROCEDURE mydb.myproc TO 'someuser'@'somehost';
The allowable
priv_type
values at the routine level areALTER ROUTINE
,EXECUTE
, andGRANT OPTION
.CREATE ROUTINE
is not a routine-level privilege because you must have this privilege to create a routine in the first place.Routine-level privileges are stored in the
mysql.procs_priv
table.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 asdb_name
.*FILE
.The
object_type
clause, if present, should be specified asTABLE
,FUNCTION
, orPROCEDURE
when the following object is a table, a stored function, or a stored procedure.The privileges for a database, table, column, or routine are formed additively as the logical
OR
of the privileges at each of the privilege levels. For example, if a user has a globalSELECT
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 5.4.5, “Access Control, Stage 2: Request Verification”.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.Important
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.
The
user
value indicates the MySQL account to which theGRANT
statement applies. To accommodate granting rights to users from arbitrary hosts, MySQL supports specifying theuser
value in the form
. If auser_name
@host_name
user_name
orhost_name
value is legal as an unquoted identifier, you need not quote it. However, quotation marks are necessary to specify auser_name
string containing special characters (such as “-
”), or ahost_name
string containing special characters or wildcard characters (such as “%
”); for example,'test-user'@'%.com'
. Quote the user name and host name separately.You can specify wildcards in the host name. For example,
applies touser_name
@'%.example.com'user_name
for any host in theexample.com
domain, and
applies touser_name
@'192.168.1.%'user_name
for any host in the192.168.1
class C subnet.The simple form
user_name
is a synonym for
.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:GRANT ALL ON test.* TO ''@'localhost' ...
In this case, any user who connects from the local host with the correct password for the anonymous user will be allowed access, with the privileges associated with the anonymous-user account.
For additional information about user and host values in account names, see Section 5.4.3, “Specifying Account Names”.
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 8.1.1, “Strings”, and Section 8.2, “Schema Object Names”.
The “
_
” and “%
” wildcards are allowed when specifying database names inGRANT
statements that grant privileges at the global or database levels. This means, for example, that if you want to use a “_
” character as part of a database name, you should specify it as “\_
” in theGRANT
statement, to prevent the user from being able to access additional databases matching the wildcard pattern; for example,GRANT ... ON `foo\_bar`.* TO ...
.Warning
If you allow anonymous users to connect to the MySQL server, you should also grant privileges to all local users as
. Otherwise, the anonymous user account foruser_name
@localhostlocalhost
in themysql.user
table (created during MySQL installation) is used when named users try to log in to the MySQL server from the local machine. For details, see Section 5.4.4, “Access Control, Stage 1: Connection Verification”.You can determine whether the preceding warning applies to you by executing the following query, which lists any anonymous users:
SELECT Host, User FROM mysql.user WHERE User='';
To avoid the problem just described, delete the local anonymous user account using this statement:
DROP USER ''@'localhost';
GRANT
supports host names up to 60 characters long. Database, table, column, and routine names can be up to 64 characters. User names can be up to 16 characters.Warning
The allowable length for user names cannot be changed by altering the
mysql.user
table. Attempting to do so results in unpredictable behavior which may even make it impossible for users to log in to the MySQL server. You should never alter any of the tables in themysql
database in any manner whatsoever except by means of the procedure described in Section 4.4.8, “mysql_upgrade — Check Tables for MySQL Upgrade”.If the
NO_AUTO_CREATE_USER
SQL mode is not enabled and the account named in aGRANT
statement does not exist in themysql.user
table,GRANT
creates it. If you specify noIDENTIFIED BY
clause or provide an empty password, the user has no password. This is very insecure.If
NO_AUTO_CREATE_USER
is enabled and the account does not exist,GRANT
fails and does not create the account unless theIDENTIFIED BY
clause is given to provide a nonempty password.When the
IDENTIFIED BY
clause is present and you have global grant privileges, the password becomes the new password for the account, even if the account exists and already has a password.MySQL Enterprise. The MySQL Enterprise Monitor specifically guards against user accounts with no passwords. To find out more, see http://www.mysql.com/products/enterprise/advisors.html.
REVOKE
does not removemysql.user
table entries; you must do that usingDROP USER
orDELETE
.Passwords can also be set with the
SET PASSWORD
statement. See Section 12.4.1.6, “SET PASSWORD
Syntax”.In the
IDENTIFIED BY
clause, the password should be given as the literal password value. It is unnecessary to use thePASSWORD()
function as it is for theSET PASSWORD
statement. For example:GRANT ... IDENTIFIED BY 'mypass';
If you do not want to send the password in clear text and you know the hashed value that
PASSWORD()
would return for the password, you can specify the hashed value preceded by the keywordPASSWORD
:GRANT ... IDENTIFIED BY PASSWORD '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4';
The
WITH
clause is used for several purposes:To enable a user to grant privileges to other users
To specify resource limits for a user
To specify whether and how a user must use secure connections to the server
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. You should be careful to whom you give theGRANT 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 theINSERT
privilege on a database. If you then grant theSELECT
privilege on the database and specifyWITH GRANT OPTION
, that user can give to other users not only theSELECT
privilege, but alsoINSERT
. If you then grant theUPDATE
privilege to the user on the database, the user can grantINSERT
,SELECT
, andUPDATE
.For a nonadministrative user, you should not grant the
ALTER
privilege globally or for themysql
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 5.4.1, “Privileges Provided by MySQL”.
Several
WITH
clause options specify limits on use of server resources by an account:The
MAX_QUERIES_PER_HOUR
,count
MAX_UPDATES_PER_HOUR
, andcount
MAX_CONNECTIONS_PER_HOUR
limits restrict the number of queries, updates, and connections to the server allowed to this account during any given one-hour period. (Queries for which results are served from the query cache do not count against thecount
MAX_QUERIES_PER_HOUR
limit.) Ifcount
is0
(the default), this means that there is no limitation for the account.The
MAX_USER_CONNECTIONS
limit restricts the maximum number of simultaneous connections to the server by the account. A nonzerocount
count
specifies the limit for the account explicitly. Ifcount
is0
(the default), the server determines the number of simultaneous connections for the account from the global value of themax_user_connections
system variable. Ifmax_user_connections
is also zero, there is no limit for the account.
To specify resource limits for an existing user without affecting existing privileges, use
GRANT USAGE
at the global level (ON *.*
) and name the limits to be changed. For example:GRANT USAGE ON *.* TO ... WITH MAX_QUERIES_PER_HOUR 500 MAX_UPDATES_PER_HOUR 100;
Limits not specified retain their current values.
For more information on restricting access to server resources, see Section 5.5.4, “Setting Account Resource Limits”.
MySQL can check X509 certificate attributes in addition to the usual authentication that is based on the user name and password. To specify SSL-related options for a MySQL account, use the
REQUIRE
clause of theGRANT
statement. (For background information on the use of SSL with MySQL, see Section 5.5.6, “Using SSL for Secure Connections”.)There are a number of different possibilities for limiting connection types for a given account:
REQUIRE NONE
indicates that the account has no SSL or X509 requirements. This is the default if no SSL-relatedREQUIRE
options are specified. Unencrypted connections are allowed if the user name and password are valid. However, encrypted connections can also be used, at the client's option, if the client has the proper certificate and key files. That is, the client need not specify any SSL command options, in which case the connection will be unencrypted. To use an encrypted connection, the client must specify either the--ssl-ca
option, or all three of the--ssl-ca
,--ssl-key
, and--ssl-cert
options.-
The
REQUIRE SSL
option tells the server to allow only SSL-encrypted connections for the account.GRANT ALL PRIVILEGES ON test.* TO 'root'@'localhost' IDENTIFIED BY 'goodsecret' REQUIRE SSL;
To connect, the client must specify the
--ssl-ca
option, and may additionally specify the--ssl-key
and--ssl-cert
options. -
REQUIRE X509
means that the client must have a valid certificate but that the exact certificate, issuer, and subject do not matter. The only requirement is that it should be possible to verify its signature with one of the CA certificates.GRANT ALL PRIVILEGES ON test.* TO 'root'@'localhost' IDENTIFIED BY 'goodsecret' REQUIRE X509;
To connect, the client must specify the
--ssl-ca
,--ssl-key
, and--ssl-cert
options. This is also true forISSUER
andSUBJECT
because thoseREQUIRE
options implyX509
. -
REQUIRE ISSUER '
places the restriction on connection attempts that the client must present a valid X509 certificate issued by CAissuer
''
. If the client presents a certificate that is valid but has a different issuer, the server rejects the connection. Use of X509 certificates always implies encryption, so theissuer
'SSL
option is unnecessary in this case.GRANT ALL PRIVILEGES ON test.* TO 'root'@'localhost' IDENTIFIED BY 'goodsecret' REQUIRE ISSUER '/C=FI/ST=Some-State/L=Helsinki/ O=MySQL Finland AB/CN=Tonu Samuel/Email=tonu@example.com';
Note that the
'
value should be entered as a single string.issuer
' -
REQUIRE SUBJECT '
places the restriction on connection attempts that the client must present a valid X509 certificate containing the subjectsubject
'subject
. If the client presents a certificate that is valid but has a different subject, the server rejects the connection.GRANT ALL PRIVILEGES ON test.* TO 'root'@'localhost' IDENTIFIED BY 'goodsecret' REQUIRE SUBJECT '/C=EE/ST=Some-State/L=Tallinn/ O=MySQL demo client certificate/ CN=Tonu Samuel/Email=tonu@example.com';
Note that the
'
value should be entered as a single string.subject
' -
REQUIRE CIPHER '
is needed to ensure that ciphers and key lengths of sufficient strength are used. SSL itself can be weak if old algorithms using short encryption keys are used. Using this option, you can ask that a specific cipher method is used to allow a connection.cipher
'GRANT ALL PRIVILEGES ON test.* TO 'root'@'localhost' IDENTIFIED BY 'goodsecret' REQUIRE CIPHER 'EDH-RSA-DES-CBC3-SHA';
The
SUBJECT
,ISSUER
, andCIPHER
options can be combined in theREQUIRE
clause like this:GRANT ALL PRIVILEGES ON test.* TO 'root'@'localhost' IDENTIFIED BY 'goodsecret' REQUIRE SUBJECT '/C=EE/ST=Some-State/L=Tallinn/ O=MySQL demo client certificate/ CN=Tonu Samuel/Email=tonu@example.com' AND ISSUER '/C=FI/ST=Some-State/L=Helsinki/ O=MySQL Finland AB/CN=Tonu Samuel/Email=tonu@example.com' AND CIPHER 'EDH-RSA-DES-CBC3-SHA';
The order of the options does not matter, but no option can be specified twice. The
AND
keyword is optional betweenREQUIRE
options.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.
The biggest differences between the standard SQL and MySQL versions of
GRANT
are:In MySQL, privileges are associated 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 12.4.1.2, “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 only with explicit
REVOKE
statements or by manipulating values stored in the MySQL grant tables.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 omit those columns for which you do not 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.) Section 5.1.7, “Server SQL Modes”, discusses strict mode. Section 10.1.4, “Data Type Default Values”, discusses implicit default values.