One means of restricting client use of MySQL server resources is to set the global 限制客户端使用MySQL服务器资源的一种方法是将全局max_user_connections
system variable to a nonzero value. max_user_connections
系统变量设置为非零值。This limits the number of simultaneous connections that can be made by any given account, but places no limits on what a client can do once connected. 这限制了任何给定帐户可以同时进行的连接数量,但对客户端连接后可以做什么没有限制。In addition, setting 此外,设置max_user_connections
does not enable management of individual accounts. Both types of control are of interest to MySQL administrators.max_user_connections
不会启用对单个帐户的管理。MySQL管理员对这两种类型的控制都很感兴趣。
To address such concerns, MySQL permits limits for individual accounts on use of these server resources:为了解决这些问题,MySQL允许对个人帐户使用这些服务器资源进行限制:
The number of queries an account can issue per hour帐户每小时可以发出的查询数
The number of updates an account can issue per hour帐户每小时可以发布的更新次数
The number of times an account can connect to the server per hour帐户每小时可以连接到服务器的次数
The number of simultaneous connections to the server by an account帐户同时连接到服务器的数量
Any statement that a client can issue counts against the query limit. Only statements that modify databases or tables count against the update limit.客户端可以发出的任何语句都会计入查询限制。只有修改数据库或表的语句才计入更新限制。
An “account” in this context corresponds to a row in the 此上下文中的“帐户”对应于mysql.user
system table. That is, a connection is assessed against the User
and Host
values in the user
table row that applies to the connection. mysql.user
系统表中的一行。也就是说,根据应用于连接的user
表行中的User
和Host
值来评估连接。For example, an account 例如,帐户'usera'@'%.example.com'
corresponds to a row in the user
table that has User
and Host
values of usera
and %.example.com
, to permit usera
to connect from any host in the example.com
domain. 'usera'@'%.example.com'
对应于user
表中的一行,该行的User
和Host
值分别为usera
和%.example.com
,以允许usera
从example.com
域中的任何主机连接。In this case, the server applies resource limits in this row collectively to all connections by 在这种情况下,服务器将此行中的资源限制集中应用于usera
from any host in the example.com
domain because all such connections use the same account.example.com
域中任何主机的usera
的所有连接,因为所有这些连接都使用相同的帐户。
Before MySQL 5.0, an “account” was assessed against the actual host from which a user connects. 在MySQL 5.0之前,“帐户”是根据用户连接的实际主机进行评估的。This older method of accounting may be selected by starting the server with the 通过使用--old-style-user-limits
option. --old-style-user-limits
选项启动服务器,可以选择这种较旧的计费方法。In this case, if 在这种情况下,如果usera
connects simultaneously from host1.example.com
and host2.example.com
, the server applies the account resource limits separately to each connection. usera
同时从host1.example.com
和host2.example.com
连接,服务器将分别对每个连接应用帐户资源限制。If 如果usera
connects again from host1.example.com
, the server applies the limits for that connection together with the existing connection from that host.usera
再次从host1.example.com
连接,服务器将对该连接以及来自该主机的现有连接应用限制。
To establish resource limits for an account at account-creation time, use the 要在帐户创建时为帐户建立资源限制,请使用CREATE USER
statement. CREATE USER
语句。To modify the limits for an existing account, use 要修改现有帐户的限制,请使用ALTER USER
. ALTER USER
。Provide a 提供一个WITH
clause that names each resource to be limited. WITH
子句,命名每个要限制的资源。The default value for each limit is zero (no limit). For example, to create a new account that can access the 每个限制的默认值为零(无限制)。例如,要创建一个可以访问customer
database, but only in a limited fashion, issue these statements:customer
数据库的新帐户,但只能以有限的方式访问,请发出以下语句:
mysql>CREATE USER 'francis'@'localhost' IDENTIFIED BY 'frank'
->WITH MAX_QUERIES_PER_HOUR 20
->MAX_UPDATES_PER_HOUR 10
->MAX_CONNECTIONS_PER_HOUR 5
->MAX_USER_CONNECTIONS 2;
The limit types need not all be named in the 限制类型不需要在WITH
clause, but those named can be present in any order. The value for each per-hour limit should be an integer representing a count per hour. WITH
子句中全部命名,但命名的类型可以以任何顺序出现。每小时限制的值应该是一个整数,表示每小时的计数。For 对于MAX_USER_CONNECTIONS
, the limit is an integer representing the maximum number of simultaneous connections by the account. MAX_USER_CONNECTIONS
,限制是一个整数,表示帐户同时连接的最大数量。If this limit is set to zero, the global 如果此限制设置为零,则全局max_user_connections
system variable value determines the number of simultaneous connections. max_user_connections
系统变量值将确定同时连接的数量。If 如果max_user_connections
is also zero, there is no limit for the account.max_user_connections
也为零,则帐户没有限制。
To modify limits for an existing account, use an 要修改现有帐户的限制,请使用ALTER USER
statement. The following statement changes the query limit for francis
to 100:ALTER USER
语句。以下语句将francis
的查询限制更改为100:
mysql> ALTER USER 'francis'@'localhost' WITH MAX_QUERIES_PER_HOUR 100;
The statement modifies only the limit value specified and leaves the account otherwise unchanged.该语句仅修改指定的限制值,否则帐户保持不变。
To remove a limit, set its value to zero. For example, to remove the limit on how many times per hour 要删除限制,请将其值设置为零。例如,要取消francis
can connect, use this statement:francis
每小时可以连接的次数限制,请使用以下语句:
mysql> ALTER USER 'francis'@'localhost' WITH MAX_CONNECTIONS_PER_HOUR 0;
As mentioned previously, the simultaneous-connection limit for an account is determined from the 如前所述,帐户的同时连接限制由MAX_USER_CONNECTIONS
limit and the max_user_connections
system variable. MAX_USER_CONNECTIONS
限制和MAX_USER_CONNECTIONS
系统变量确定。Suppose that the global 假设全局max_user_connections
value is 10 and three accounts have individual resource limits specified as follows:max_user_connections
值为10,并且三个帐户具有如下指定的单独资源限制:
ALTER USER 'user1'@'localhost' WITH MAX_USER_CONNECTIONS 0; ALTER USER 'user2'@'localhost' WITH MAX_USER_CONNECTIONS 5; ALTER USER 'user3'@'localhost' WITH MAX_USER_CONNECTIONS 20;
user1
has a connection limit of 10 (the global max_user_connections
value) because it has a MAX_USER_CONNECTIONS
limit of zero. user1
的连接限制为10(全局MAX_USER_CONNECTIONS
值),因为它的MAX_USER_CONNECTIONS
限制为零。user2
and user3
have connection limits of 5 and 20, respectively, because they have nonzero MAX_USER_CONNECTIONS
limits.user2
和user3
的连接限制分别为5和20,因为它们具有非零的MAX_USER_CONNECTIONS
限制。
The server stores resource limits for an account in the 服务器将帐户的资源限制存储在与帐户对应的user
table row corresponding to the account. user
表行中。The max_questions
, max_updates
, and max_connections
columns store the per-hour limits, and the max_user_connections
column stores the MAX_USER_CONNECTIONS
limit. max_questions
、max_updates
和max_connections
列存储每小时限制,max_user_connections
栏存储max_user_connections
限制。(See Section 6.2.3, “Grant Tables”.)(参阅第6.2.3节,“授权表”。)
Resource-use counting takes place when any account has a nonzero limit placed on its use of any of the resources.当任何帐户对任何资源的使用都有非零限制时,就会进行资源使用计数。
As the server runs, it counts the number of times each account uses resources. If an account reaches its limit on number of connections within the last hour, the server rejects further connections for the account until that hour is up. 当服务器运行时,它会统计每个帐户使用资源的次数。如果帐户在过去一小时内达到其连接数限制,服务器将拒绝该帐户的进一步连接,直到该小时结束。Similarly, if the account reaches its limit on the number of queries or updates, the server rejects further queries or updates until the hour is up. In all such cases, the server issues appropriate error messages.同样,如果帐户达到查询或更新次数的限制,服务器将拒绝进一步的查询或更新,直到一小时结束。在所有这些情况下,服务器都会发出相应的错误消息。
Resource counting occurs per account, not per client. For example, if your account has a query limit of 50, you cannot increase your limit to 100 by making two simultaneous client connections to the server. Queries issued on both connections are counted together.资源计数按每个帐户进行,而不是按每个客户端进行。例如,如果您的帐户的查询限制为50,则无法通过同时连接两个客户端到服务器将限制增加到100。对这两个连接发出的查询将一起计算。
The current per-hour resource-use counts can be reset globally for all accounts, or individually for a given account:当前每小时资源使用计数可以全局重置所有帐户,也可以单独重置给定帐户:
To reset the current counts to zero for all accounts, issue a 要将所有帐户的当前计数重置为零,请发出FLUSH USER_RESOURCES
statement. FLUSH USER_RESOURCES
语句。The counts also can be reset by reloading the grant tables (for example, with a 计数也可以通过重新加载授权表来重置(例如,使用FLUSH PRIVILEGES
statement or a mysqladmin reload command).FLUSH PRIVILEGES
语句或mysqladmin reload
命令)。
The counts for an individual account can be reset to zero by setting any of its limits again. Specify a limit value equal to the value currently assigned to the account.通过再次设置其任何限制,可以将单个帐户的计数重置为零。指定一个等于当前分配给帐户的值的限制值。
Per-hour counter resets do not affect the 每小时计数器重置不会影响MAX_USER_CONNECTIONS
limit.MAX_USER_CONNECTIONS
限制。
All counts begin at zero when the server starts. Counts do not carry over through server restarts.服务器启动时,所有计数都从零开始。计数不会在服务器重新启动时结转。
For the 对于MAX_USER_CONNECTIONS
limit, an edge case can occur if the account currently has open the maximum number of connections permitted to it: A disconnect followed quickly by a connect can result in an error (ER_TOO_MANY_USER_CONNECTIONS
or ER_USER_LIMIT_REACHED
) if the server has not fully processed the disconnect by the time the connect occurs. MAX_USER_CONNECTIONS
限制,如果帐户当前已打开允许的最大连接数,则可能会出现边缘情况:如果服务器在连接发生时未完全处理断开连接,则断开连接后快速连接可能会导致错误(ER_TOO_MANY_USER_CONNECTIONS
或ER_USER_limit_REACHED
)。When the server finishes disconnect processing, another connection is once more permitted.当服务器完成断开连接处理时,再次允许另一个连接。