GRANT
GRANTprivilege[,...] [(column[,...])][, ...] ON [TABLE|FUNCTION|PROCEDURE] {[{database|*}.{table|*}] | *} TO 'user'@'host' [IDENTIFIED BY [PASSWORD] 'password'][, ...] [REQUIRE NONE | [{SSL|X509}] [CIPHER 'cipher' [AND]] [ISSUER 'issue' [AND]] [SUBJECT 'subject']] [WITH [GRANT OPTION | MAX_QUERIES_PER_HOURcount| MAX_UPDATES_PER_HOURcount| MAX_CONNECTIONS_PER_HOURcount| MAX_USER_CONNECTIONScount] ...]
This statement may be used to create new MySQL users, but its primary use is for granting user privileges. Privileges can be global (apply to all databases on the server), database-specific, table-specific, or column-specific. Users can now also be limited by functions and procedures. Additionally, users can be limited by number of connections or by a maximum of resources per hour.
The privileges to grant to a user are listed immediately after
the GRANT keyword in a comma-separated list. To
restrict a user to specific columns in a table, list those columns in
a comma-separated list within parentheses. This is then followed by
the ON clause in which the privileges granted may
be limited to a database, table, function, or procedure. To limit the
privileges to a function, use the FUNCTION keyword;
to limit them to a procedure,
use the PROCEDURE keyword.
For tables, the keyword TABLE is optional and the
default. You can then specify the database to which the privileges
relate in quotes, followed by a period (.) and the
name of the table, function, or procedure in quotes. You may also use
the asterisk wildcard (*) to specify all
databases or all tables, functions, or procedures offered by the
database.
In the TO clause, give the username (in
quotes) and the IP address or host (also in quotes) for which the user
account privileges are permitted, separated by an at sign
(@). To provide the password for the user account, add the IDENTIFIED BY clause, followed by
the user’s password in plain text and enclosed in quotes. To provide
the password in encrypted hash form, add the keyword
PASSWORD just before the password given. You can
use the WITH clause to grant the GRANT
OPTION privilege to a user so that that user may execute
this statement. The GRANT statement with the
IDENTIFIED BY clause can be used to change a
password for an existing user.
For an explanation of how to restrict user accounts based on types of connections, see the next section of this statement (GRANT: Type of connection restrictions”). For information on how to restrict user accounts based on the amount of activity for a period of time or the number of connections permitted, see the last section of this statement (GRANT: Time and number of connection limits”). To see the privileges for a given user, use the SHOW GRANTS statement described later in this chapter.
A large variety of privileges may be granted to a user, so a
common set of privileges has been combined in the
ALL keyword. Here is an example:
GRANT ALL PRIVILEGES ON *.* TO 'evagelia'@'localhost' IDENTIFIED BY 'papadimitrou1234' WITH GRANT OPTION;
In this example, the user evagelia is
created and granted all basic privileges because of the
ALL keyword. This does not include the
GRANT privilege, the ability to use the
GRANT statement. To do that, the WITH
GRANT OPTION clause is given, as shown here, explicitly to give that
privilege to the user. It’s not a good idea to give users this
privilege unless they are MySQL server administrators. Table 4-2 later in this chapter lists and
describes each privilege.
As mentioned before, a user’s privileges can be refined to
specific SQL statements and specific databases. A GRANT statement can also
restrict a user to only certain tables and columns. Here is an example
that leaves the user fairly limited:
GRANT SELECT ON workrequests.* TO 'jerry'@'localhost' IDENTIFIED BY 'neumeyer3186'; GRANT SELECT,INSERT,UPDATE ON workrequests.workreq TO 'jerry'@'localhost' IDENTIFIED BY 'neumeyer3186';
Assuming the user jerry does not already
exist, the first statement here creates the user and gives him
SELECT privileges only for the
workrequests database for all of its tables. This
will allow him to read from the various tables but not edit the data.
The second SQL statement grants jerry the right
to add and change data in the workreq table of the
workrequests database. This
will allow him to enter work requests and make changes to them. The
first statement causes an entry to be made to the
db table in the mysql database.
The second affects the tables_priv table. An entry
is also made to the user table showing the user
jerry, but he has no global privileges. This is
the equivalent of granting just the USAGE
privilege.
GRANTprivilege[,...] [(column[,...])][, ...] ON [TABLE|FUNCTION|PROCEDURE] {[{database|*}.{table|*}] | *} TO 'user'@'host' [IDENTIFIED BY [PASSWORD] 'password'][, ...] [REQUIRE NONE | [{SSL|X509} [AND]] [CIPHER 'cipher' [AND]] [ISSUER 'issue' [AND]] [SUBJECT 'subject']] [time and number of connection limits] ...]
A user can also be restricted to certain types of
connections with the REQUIRE clause. There are several
options that may be given together with the keyword
AND. Each option can be used only once in a
statement. REQUIRE NONE is the default and
indicates that no such restrictions are required. Encrypted and
unencrypted connections from clients are permitted from the user that
has been properly authenticated.
The REQUIRE SSL option restricts the user
account to only SSL-encrypted connections. The
mysql client of the user account would start the
client with the --ssl-ca option, and also the
--ssl-key and --ssl-cert options
if necessary:
GRANT ALL PRIVILEGES ON workrequests.* TO 'rusty'@'localhost' IDENTIFIED BY 'her_password' REQUIRE SSL;
Use the REQUIRE X509 option to require the user account to have a valid CA certificate. This does not require any
specific certificate, though. The mysql client
would need to be started with the --ssl-ca,
--ssl-key, and --ssl-cert
options. To simplify handling of these options, the user can put them
in a options file in her home directory on the server (e.g.,
~/.my.cnf). The following is a sample of
what that options file would contain to conform to the user account
restrictions:
[client] ssl-ca=/data/mysql/cacert.pem ssl-key=/data/mysql/rusty-key.pem ssl-cert=/data/mysql/rusty-cert.pem
Use the REQUIRE CIPHER option to require that
the user account use a given cipher method:
GRANT ALL PRIVILEGES ON workrequests.* TO 'rusty'@'localhost' IDENTIFIED BY 'her_password' REQUIRE CIPHER 'EDH-RSA-DES-CBC3-SHA';
REQUIRE ISSUER is used to require the user to
supply a valid X.509 certificate issued by the given CA. Although the string given for an issuer
may be lengthy, it must be written as one string without an embedded
line break:
GRANT ALL PRIVILEGES ON workrequests.* TO 'rusty'@'localhost' IDENTIFIED BY 'her_password' REQUIRE ISSUER '/C=US/ST=Louisiana/L=New+20Orleans/O=WorkRequesters/CN= cacert.workrequests.com/emailAddress=admin@workrequests.com';
The REQUIRE SUBJECT option requires that the
X.509 certificate used by the user account have the given subject:
GRANT ALL PRIVILEGES ON workrequests.* TO 'rusty'@'localhost' IDENTIFIED BY 'her_password' REQUIRE SUBJECT '/C=US/ST=Louisiana/L=New+20Orleans/O=WorkRequesters/CN= Rusty Osborne/emailAddress=rusty@workrequests.com';
GRANTprivilege[,...] [(column[,...])][, ...] ON [TABLE|FUNCTION|PROCEDURE] {[{database|*}.{table|*}] | *} TO 'user'@'host' [IDENTIFIED BY [PASSWORD] 'password'][, ...] [type of connection restrictions] [WITH [MAX_QUERIES_PER_HOURcount| MAX_UPDATES_PER_HOURcount| MAX_CONNECTIONS_PER_HOURcount| MAX_USER_CONNECTIONScount] ...]
You can use the WITH clause along with
the MAX_QUERIES_PER_HOUR option to specify the maximum number of queries that a user
account may execute per hour. The
MAX_UPDATES_PER_HOUR option is used to give the
maximum number of UPDATE statements that may be
issued per hour by the user account. The maximum number of connections
by a user account to the server per hour can be set with the
MAX_CONNECTIONS_PER_HOUR option. The default values
for these three options are all 0. This value indicates that there is
no limit or restrictions for these resources. The
MAX_USER_CONNECTIONS option is used to set the
maximum number of simultaneous connections the given user account may
have. If this value is not set or is set to 0, the value of the system
variable max_user_connections
is used instead. Here is an example of how a user might be limited in
such a way:
GRANT SELECT ON catalogs.* TO 'webuser'@'%' WITH MAX_QUERIES_PER_HOUR 1000 MAX_CONNECTIONS_PER_HOUR 100;
This account is designed for large numbers of users running
queries through a web server. The statement creates the
webuser user and allows it to read tables from
the catalogs database. The user may not run more
than 1,000 queries in an hour and may establish only 100 connections
in an hour.
To change an existing user account’s resources without changing
the account’s existing privileges, you can use the USAGE keyword. Simply enter a
statement like this:
GRANT USAGE ON catalogs.* TO 'webuser'@'%' WITH MAX_QUERIES_PER_HOUR 10000 MAX_CONNECTIONS_PER_HOUR 100;
In this example, the existing user account has been limited in resources without changing the user account’s privileges. See Table 4-2 for a list of privileges.
Privilege | Description |
| Grants all of the basic privileges. Does not
include |
| Allows use of the |
| Allows the user account to alter or drop stored
routines. This includes the |
| Grants |
| Allows the user account to create stored
routines. This includes the |
| Allows the |
| Allows the user account to execute several user
account management statements: |
| Allows the |
| Allows the |
| Allows the user to execute |
| Allows the user account to create events for the
event scheduler. As of version 5.1.12 of MySQL, this privilege
allows the use of the |
| Allows the execution of stored procedures. This is available as of version 5 of MySQL. |
| Allows the use of |
| Allows the use of the |
| Allows the use of |
| Allows the use of |
| Allows the use of |
| Allows the use of |
| This is not used. It’s for future releases. |
| Allows the use of |
| Allows the user to query master and slave servers for status information. |
| Required for replication slave servers. Allows binary log events to be read from the master server. |
| Allows the use of the |
| Permits the use of the |
| Allows the use of the |
| Allows the use of the |
| Allows the use of |
| Allows the user account to create and drop
triggers: the |
| Allows the use of the |
| Used to create a user without privileges, or to modify resource limits on an existing user without affecting the existing privileges. |