要管理 MySQL 账户,请使用用于此目的的 SQL 语句:To manage MySQL accounts, use the SQL statements intended for that purpose:
CREATE USER和DROP USER用于创建和删除账户。CREATE USERandDROP USERcreate and remove accounts.GRANT和REVOKE用于分配和收回账户权限。GRANTandREVOKEassign privileges to and revoke privileges from accounts.SHOW GRANTS显示账户权限分配情况。SHOW GRANTSdisplays account privilege assignments.
账户管理语句会导致服务器对底层授权表进行适当的修改,这在第 8.2.3 节,“授权表”中有讨论。Account-management statements cause the server to make appropriate modifications to the underlying grant tables, which are discussed in Section 8.2.3, “Grant Tables”.
不鼓励使用 INSERT、UPDATE 或 DELETE 等语句直接修改授权表,并且这样做风险自负。服务器可以忽略由于此类修改而导致格式错误的行。Direct modification of grant tables using statements such as
INSERT,
UPDATE, or
DELETE is discouraged and done at
your own risk. The server is free to ignore rows that become
malformed as a result of such modifications.
对于任何修改授权表的操作,服务器会检查表是否具有预期的结构,如果不是,则会产生错误。要将表更新为预期结构,请执行 MySQL 升级过程。请参见 第 3 章,升级 MySQL。For any operation that modifies a grant table, the server checks whether the table has the expected structure and produces an error if not. To update the tables to the expected structure, perform the MySQL upgrade procedure. See Chapter 3, Upgrading MySQL.
创建账户的另一种选择是使用 GUI 工具 MySQL Workbench。此外,还有几个第三方程序提供 MySQL 账户管理功能。phpMyAdmin 就是其中之一。Another option for creating accounts is to use the GUI tool
MySQL Workbench. Also, several third-party programs offer capabilities
for MySQL account administration. phpMyAdmin is
one such program.
本节讨论以下主题:This section discusses the following topics:
关于此处讨论的语句的更多信息,请参阅 第 15.7.1 节,“账户管理语句”。For additional information about the statements discussed here, see Section 15.7.1, “Account Management Statements”.
以下示例展示了如何使用 mysql 客户端程序来创建新账户。这些示例假设 MySQL 的 root 账户拥有 CREATE USER 权限以及它授予其他账户的所有权限。The following examples show how to use the
mysql client program to set up new accounts.
These examples assume that the MySQL root
account has the CREATE USER
privilege and all privileges that it grants to other accounts.
在命令行中,以 MySQL root 用户身份连接到服务器,并在密码提示时提供相应的密码:At the command line, connect to the server as the MySQL
root user, supplying the appropriate password
at the password prompt:
$>mysql -u root -pEnter password:(enter root password here)
连接到服务器后,您可以添加新账户。下面的示例使用 CREATE USER 和 GRANT 语句来设置四个账户(在看到 ' 时,请替换为适当的密码):After connecting to the server, you can add new accounts. The
following example uses password'CREATE USER
and GRANT statements
to set up four accounts (where you see
',
substitute an appropriate password):password'
CREATE USER 'finley'@'localhost' IDENTIFIED BY 'password'; GRANT ALL ON *.* TO 'finley'@'localhost' WITH GRANT OPTION; CREATE USER 'finley'@'%.example.com' IDENTIFIED BY 'password'; GRANT ALL ON *.* TO 'finley'@'%.example.com' WITH GRANT OPTION; CREATE USER 'admin'@'localhost' IDENTIFIED BY 'password'; GRANT RELOAD,PROCESS ON *.* TO 'admin'@'localhost'; CREATE USER 'dummy'@'localhost';
由这些语句创建的账户具有以下属性:The accounts created by those statements have the following properties:
有两个账户的用户名为
finley。这两个都是超级用户账户,拥有执行任何操作的全局权限。'finley'@'localhost'账户仅能在本地主机连接时使用。'finley'@'%.example.com'账户在主机部分使用了'%'通配符,因此可以从example.com域中的任何主机连接使用。Two accounts have a user name offinley. Both are superuser accounts with full global privileges to do anything. The'finley'@'localhost'account can be used only when connecting from the local host. The'finley'@'%.example.com'account uses the'%'wildcard in the host part, so it can be used to connect from any host in theexample.comdomain.如果
localhost存在匿名用户账户,则'finley'@'localhost'账户是必要的。如果没有'finley'@'localhost'账户,当finley从本地主机连接时,那个匿名用户账户会优先,finley会被视为匿名用户。原因是匿名用户账户的Host列值比'finley'@'%'账户更具体,因此在user表排序中会排在前面。(有关user表排序的信息,请参见 第 8.2.6 节,“访问控制,第 1 阶段:连接验证”。)The'finley'@'localhost'account is necessary if there is an anonymous-user account forlocalhost. Without the'finley'@'localhost'account, that anonymous-user account takes precedence whenfinleyconnects from the local host andfinleyis treated as an anonymous user. The reason for this is that the anonymous-user account has a more specificHostcolumn value than the'finley'@'%'account and thus comes earlier in theusertable sort order. (For information aboutusertable sorting, see Section 8.2.6, “Access Control, Stage 1: Connection Verification”.)该
'admin'@'localhost'account can be used only byadminto connect from the local host. It is granted the globalRELOAD以及PROCESSadministrative privileges. These privileges enable theadminuser to execute the mysqladmin reload, mysqladmin refresh,以及mysqladmin flush-xxxcommands, as well as mysqladmin processlist . No privileges are granted for accessing any databases. You could add such privileges usingGRANT语句。The'admin'@'localhost'account can be used only byadminto connect from the local host. It is granted the globalRELOADandPROCESSadministrative privileges. These privileges enable theadminuser to execute the mysqladmin reload, mysqladmin refresh, and mysqladmin flush-xxxcommands, as well as mysqladmin processlist . No privileges are granted for accessing any databases. You could add such privileges usingGRANTstatements.'dummy'@'localhost'账户没有密码(这不安全,不推荐)。该账户只能用于从本地主机连接。没有授予任何权限。假定您通过使用GRANT语句授予该账户特定权限。The'dummy'@'localhost'account has no password (which is insecure and not recommended). This account can be used only to connect from the local host. No privileges are granted. It is assumed that you grant specific privileges to the account usingGRANTstatements.
前面的示例是在全局级别授予权限。下一个示例创建三个账户,并在较低级别授予它们访问权限;也就是说,授予特定数据库或数据库内的对象权限。每个账户的用户名为 custom,但主机名部分不同:The previous example grants privileges at the global level. The
next example creates three accounts and grants them access at
lower levels; that is, to specific databases or objects within
databases. Each account has a user name of
custom, but the host name parts differ:
CREATE USER 'custom'@'localhost' IDENTIFIED BY 'password'; GRANT ALL ON bankaccount.* TO 'custom'@'localhost'; CREATE USER 'custom'@'host47.example.com' IDENTIFIED BY 'password'; GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON expenses.* TO 'custom'@'host47.example.com'; CREATE USER 'custom'@'%.example.com' IDENTIFIED BY 'password'; GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON customer.addresses TO 'custom'@'%.example.com';
这三个账户可以按如下方式使用:The three accounts can be used as follows:
'custom'@'localhost'账户拥有访问bankaccount数据库的所有数据库级权限。该账户只能从本地主机连接到服务器。The'custom'@'localhost'account has all database-level privileges to access thebankaccountdatabase. The account can be used to connect to the server only from the local host.'custom'@'host47.example.com'账户拥有访问expenses数据库的特定数据库级权限。该账户只能从主机host47.example.com连接到服务器。The'custom'@'host47.example.com'account has specific database-level privileges to access theexpensesdatabase. The account can be used to connect to the server only from the hosthost47.example.com.'custom'@'%.example.com'账户具有特定的表级权限,可以访问customer数据库中的addresses表,来自example.com域中的任何主机。由于账户名的主机部分使用了%通配符字符,该账户可以用来从域内的所有机器连接到服务器。The'custom'@'%.example.com'account has specific table-level privileges to access theaddressestable in thecustomerdatabase, from any host in theexample.comdomain. The account can be used to connect to the server from all machines in the domain due to use of the%wildcard character in the host part of the account name.
要查看账户的权限,请使用 SHOW GRANTS:To see the privileges for an account, use
SHOW GRANTS:
mysql> SHOW GRANTS FOR 'admin'@'localhost';
+-----------------------------------------------------+
| Grants for admin@localhost |
+-----------------------------------------------------+
| GRANT RELOAD, PROCESS ON *.* TO `admin`@`localhost` |
+-----------------------------------------------------+
要查看账户的非权限属性,请使用 SHOW CREATE USER:To see nonprivilege properties for an account, use
SHOW CREATE USER:
mysql>SET print_identified_with_as_hex = ON;mysql>SHOW CREATE USER 'admin'@'localhost'\G*************************** 1. row *************************** CREATE USER for admin@localhost: CREATE USER `admin`@`localhost` IDENTIFIED WITH 'caching_sha2_password' AS 0x24412430303524301D0E17054E2241362B1419313C3E44326F294133734B30792F436E77764270373039612E32445250786D43594F45354532324B6169794F47457852796E32 REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK PASSWORD HISTORY DEFAULT PASSWORD REUSE INTERVAL DEFAULT PASSWORD REQUIRE CURRENT DEFAULT
启用 print_identified_with_as_hex 系统变量会导致 SHOW CREATE
USER 显示包含不可打印字符的哈希值时,以十六进制字符串而不是普通字符串字面值的形式显示。Enabling the
print_identified_with_as_hex
system variable causes SHOW CREATE
USER to display hash values that contain unprintable
characters as hexadecimal strings rather than as regular string
literals.
要撤销账户权限,请使用 REVOKE 语句。权限可以在不同级别撤销,就像它们可以在不同级别授予一样。To revoke account privileges, use the
REVOKE statement. Privileges can
be revoked at different levels, just as they can be granted at
different levels.
撤销全局权限:Revoke global privileges:
REVOKE ALL ON *.* FROM 'finley'@'%.example.com'; REVOKE RELOAD ON *.* FROM 'admin'@'localhost';
撤销数据库级别权限:Revoke database-level privileges:
REVOKE CREATE,DROP ON expenses.* FROM 'custom'@'host47.example.com';
撤销表级别权限:Revoke table-level privileges:
REVOKE INSERT,UPDATE,DELETE ON customer.addresses FROM 'custom'@'%.example.com';
要检查权限撤销的效果,请使用 SHOW GRANTS:To check the effect of privilege revocation, use
SHOW GRANTS:
mysql> SHOW GRANTS FOR 'admin'@'localhost';
+---------------------------------------------+
| Grants for admin@localhost |
+---------------------------------------------+
| GRANT PROCESS ON *.* TO `admin`@`localhost` |
+---------------------------------------------+