< Day Day Up > |
12.2 User Account ManagementThe MySQL access control system enables you to create MySQL accounts and define what each account can do. Several types of privileges can be assigned to an account. They should be granted according to how the account is to be used. Some examples:
The MySQL server bases access control on the contents of the grant tables in the mysql database. These tables define MySQL accounts and the privileges they hold. To manage their contents, use the GRANT and REVOKE statements. These statements provide an interface to the grant tables that enables you to specify privileges without having to determine how to modify the tables directly. When you use GRANT and REVOKE to perform a privilege operation, the MySQL server determines what changes to the grant tables are needed and makes the modifications for you. This section describes the structure and contents of the grant tables and how you set up user accounts using GRANT and REVOKE. Section 12.3, "Client Access Control," describes how the server uses the grant tables to check access privileges when clients connect. 12.2.1 Types of Privileges That MySQL SupportsYou can grant several types of privileges to a MySQL account, and you can grant privileges at different levels (globally or just for particular databases, tables, or columns). For example, you can allow a user to select from any table in any database by granting the SELECT privilege at the global level. Or you might grant an account no global privileges, but give it complete control over a specific database. That allows the account to create the database and tables in it, select from the tables, and add new records, delete them, or update them. The privileges that MySQL supports are shown in the following lists. The first names the administrative privileges and the second names the database-access privileges. Administrative Privileges:
Some privileges not shown in these lists can be assigned to accounts but currently are unused. EXECUTE is reserved for future versions of MySQL, when stored procedures are implemented. REFERENCES may be implemented in relation to foreign key support at some point. There are also some special privilege specifiers:
Privileges can exist at different levels:
12.2.2 The Grant TablesFour grant tables in the mysql database contain most of the access control information used by the server. They contain information to indicate what the legal accounts are and the privileges held at each access level by each account:
Every account must have a user table record because the server uses that table's contents when determining whether to accept or reject client connection attempts. An account also will have records in the other grant tables if it has privileges at other than the global level. Each grant table has columns that identify which accounts its records apply to:
Use of the grant tables for controlling what clients can do is discussed further in section 12.3, "Client Access Control." There is also a fifth grant table named host that exists for historical reasons. It is not affected by the GRANT and REVOKE statements, so it's discussed no further here. For more information about the host table, see the MySQL Reference Manual. The grant tables are stored as MyISAM tables. The MyISAM storage engine is always guaranteed to be enabled, which is not true for storage engines such as InnoDB and BDB. As already mentioned, the server uses the information in the grant tables to determine whether to allow clients to connect, and to determine for every statement that a connected client issues whether the client has sufficient privileges to execute it. However, the server does not actually access the on-disk grant tables each time it needs to verify client access because that would result in a great deal of overhead. Instead, the server reads the grant tables into memory during its startup sequence and uses the in-memory copies to check client access. The server refreshes its in-memory copies of the grant tables under the following conditions:
12.2.3 Granting and Revoking PrivilegesIt's possible to manage MySQL accounts by modifying the grant tables directly with SQL statements such as INSERT, DELETE, and UPDATE. The procedure described in section 12.1.2, "Securing the Initial MySQL Accounts," is an example of how UPDATE and DELETE can be used in this way. In general, however, the recommended way to set up and modify MySQL accounts is to use the GRANT and REVOKE statements because they offer these advantages:
In addition to GRANT and REVOKE, the SET PASSWORD statement is useful when all you want to do is change an account's password. SET PASSWORD causes the server to automatically refresh its in-memory grant tables when you use it. Despite the advantages of GRANT and REVOKE, it is occasionally necessary to manipulate the grant tables directly. The principal reason for this is that REVOKE does not remove records from the user table. You can use REVOKE to disable the global privileges recorded in that table, but it leaves the record in the table in case you want to assign different privileges later. If you want to eliminate all traces of an account from the grant tables, you must also use DELETE to remove its user table record. 12.2.3.1 The GRANT StatementThe syntax for the GRANT statement includes several sections. In simplest form, you specify the following:
As an example, the following statement grants the SELECT privilege for all tables in the world database to a user named jim, who must connect from the local host and use a password of Abc123: GRANT SELECT ON world.* TO 'jim'@'localhost' IDENTIFIED BY 'Abc123'; The parts of the statement have the following effects:
To specify an anonymous-user account (that is, an account that matches any username), specify an empty string for the user part of the account name: GRANT SELECT ON world.* TO ''@'localhost'; The host part of an account name may be given in any of the following formats:
It's allowable to omit the host part of an account name in the GRANT statement. An account name specified as 'user_name' is equivalent to 'user_name'@'%'. Keep the proper perspective in mind when specifying the host part of an account name in GRANT statements. When you connect to the server using a client program, you specify the host to which you want to connect. On the other hand, when the server checks the client against Host column values in the grant tables, it uses the host from which the client connects. When setting up an account with GRANT, you should specify the client host from the server's point of view. For example, if the server runs on server.example.com and you want to allow jim to connect from client.example.com, the GRANT statement should look like this: GRANT ... TO 'jim'@'client.example.com' ... ; Be aware that it is possible to have multiple accounts that could apply to a given client. For example, if you set up accounts for 'jim'@'localhost' and 'jim'@'%', the server could use either one when jim connects from the local host. The rules that the server employs to determine which account to use in such cases are covered in section 12.3, "Client Access Control." If you want to give an account the capability to grant its privileges to other accounts, add a WITH GRANT OPTION clause to the statement. For example, if you want jim to have read access to the world database and to be able to create other users that have read access to that database, use this statement: GRANT SELECT ON world.* TO 'jim'@'localhost' IDENTIFIED BY 'Abc123' WITH GRANT OPTION; To find out what privileges a particular account has, use the SHOW GRANTS statement. It displays the GRANT statements that would be required to set up the account. The account name for this statement has the same 'user_name'@'host_name' format as that used with GRANT. You can always see your own privileges with SHOW GRANTS. You cannot see the privileges for other accounts unless you have the SELECT privilege for the mysql database. Suppose that you've set up an account for a user jen who connects from the host myhost.example.com. To see this account's privileges, use the following statement:
mysql> SHOW GRANTS FOR 'jen'@'myhost.example.com';
+----------------------------------------------------------------+
| Grants for jen@myhost.example.com |
+----------------------------------------------------------------+
| GRANT FILE ON *.* TO 'jen'@'myhost.example.com' |
| GRANT SELECT ON `mydb`.* TO 'jen'@'myhost.example.com' |
| GRANT UPDATE ON `test`.`mytable` TO 'jen'@'myhost.example.com' |
+----------------------------------------------------------------+
The output displayed here by SHOW GRANTS consists of three GRANT statements. Their ON clauses indicate that jen has privileges at the global, database, and table levels, respectively. If the account has a password, SHOW GRANTS displays an IDENTIFIED BY PASSWORD clause at the end of the GRANT statement that lists the account's global privileges. (The word PASSWORD after IDENTIFIED BY indicates that the password value shown is the encrypted value stored in the user table, not the actual password.) If the account can grant some or all of its privileges to other accounts, SHOW GRANTS displays WITH GRANT OPTION at the end of each GRANT statement to which it applies. SHOW GRANTS displays privileges only for the exact account specified in the statement. For example, the preceding SHOW GRANTS statement shows privileges only for 'jen'@'myhost.example.com', not for 'jen'@'%.example.com', 'jen'@'%.com', or 'jen'@'%'. 12.2.3.2 The REVOKE StatementUse the REVOKE statement to revoke privileges from an account. Its syntax has the following sections:
Suppose that jim on the local host has SELECT, DELETE, INSERT, and UPDATE privileges on the world database, but you want to change the account so that he has SELECT access only. To do this, revoke those privileges that allow him to make changes: REVOKE DELETE, INSERT, UPDATE ON world.* FROM 'jim'@'localhost'; To revoke the GRANT OPTION privilege from an account that has it, you must revoke it in a separate statement. For example, if jill has the ability to grant her privileges for the world database to other users, you can revoke that ability as follows: REVOKE GRANT OPTION ON world.* FROM 'jill'@'localhost'; If you use REVOKE to remove all the privileges enabled by a record in the db, tables_priv, or columns_priv tables, REVOKE removes the record entirely. However, REVOKE does not remove an account's user table record, even if you revoke all privileges for the account. It's necessary to use DELETE to remove a user record. A later example demonstrates this. To determine what REVOKE statements are needed to revoke an account's privileges, SHOW GRANTS might be helpful. Consider again the output from SHOW GRANTS for the jen@localhost account:
mysql> SHOW GRANTS FOR 'jen'@'myhost.example.com';
+----------------------------------------------------------------+
| Grants for jen@myhost.example.com |
+----------------------------------------------------------------+
| GRANT FILE ON *.* TO 'jen'@'myhost.example.com' |
| GRANT SELECT ON `mydb`.* TO 'jen'@'myhost.example.com' |
| GRANT UPDATE ON `test`.`mytable` TO 'jen'@'myhost.example.com' |
+----------------------------------------------------------------+
This output indicates that the account has global, database-level, and table-level privileges. To remove these privileges, convert those GRANT statements to the following corresponding REVOKE statements. The privilege names, privilege levels, and account name must be the same as displayed by SHOW GRANTS: mysql> REVOKE FILE ON *.* FROM 'jen'@'myhost.example.com'; mysql> REVOKE SELECT ON mydb.* FROM 'jen'@'myhost.example.com'; mysql> REVOKE UPDATE ON test.mytable FROM 'jen'@'myhost.example.com'; After issuing the REVOKE statements, SHOW GRANTS produces this result:
mysql> SHOW GRANTS FOR 'jen'@'myhost.example.com';
+--------------------------------------------------+
| Grants for jen@myhost.example.com |
+--------------------------------------------------+
| GRANT USAGE ON *.* TO 'jen'@'myhost.example.com' |
+--------------------------------------------------+
This means that the account no longer has any privileges, although it does still exist and thus can be used to connect to the server. (In other words, the user table still contains a record for the account, but all the global privileges listed in the record are disabled.) To remove the last trace of the account, use a DELETE statement to remove the user table record, and then tell the server to reload the grant tables: mysql> USE mysql; mysql> DELETE FROM user WHERE User = 'jen' AND Host = 'myhost.example.com'; mysql> FLUSH PRIVILEGES; After that, the account no longer exists and cannot be used to connect to the server. 12.2.3.3 When Privilege Changes Take EffectThe effects of changes to the grant tables apply to existing client connections as follows:
12.2.4 Changing Account PasswordsAs discussed earlier, you can specify a password for an account by including an IDENTIFIED BY clause in a GRANT statement. If the account is new, the clause assigns its initial password. If the account already exists, the clause changes its password. To change an existing account's password without changing any of its privileges, you have two options:
Note that with SET PASSWORD, you use PASSWORD() to encrypt the password, whereas with GRANT, you do not use it. To allow a user to connect without specifying a password, change the password to the empty string. However, you cannot revoke the password this way with REVOKE. Instead, use either of the following statements: SET PASSWORD FOR 'jim'@'localhost' = ''; GRANT USAGE ON *.* TO 'jim'@'localhost' IDENTIFIED BY ''; Be certain that you want to do this, however. It isn't a good idea to have accounts without passwords. 12.2.5 Specifying Resource LimitsBy default, there is no limit on the number of times that a client can connect to the server or the number of queries it can issue. If that is not suitable, GRANT can establish limits on an account's resource consumption for the following characteristics:
Each of these resource limits is specified using an option in a WITH clause. The following example creates an account that can use the test database, but can connect to the server a maximum of only 10 times per hour. The account can issue 50 queries per hour, and at most 20 of those queries can modify data: GRANT ALL ON test.* TO 'quinn'@'localhost' IDENTIFIED BY 'SomePass' WITH MAX_CONNECTIONS_PER_HOUR 10 MAX_QUERIES_PER_HOUR 50 MAX_UPDATES_PER_HOUR 20; The order in which you name the options in the WITH clause doesn't matter. To reset an existing limit to the default of no limit, specify a value of zero. For example: GRANT USAGE ON *.* TO 'quinn'@'localhost' WITH MAX_CONNECTIONS_PER_HOUR 0; |
< Day Day Up > |