MySQL 参数解析 - max_connections 与user.max_connections

MySQL
问题描述

参数组的max_connections 与 mysql.user 表中的 max_connections 以及 max_user_connections 谁的优先级更高?

问题分析

参数组中的max_connections

max_connections 是一个全局的概念,意味着服务器所能承受的最大连接数,针对所有用户。比如设置为2000,意味着这个实例的最大连接数只能到达2000。

mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 2000  |
+-----------------+-------+
1 row in set (0.00 sec)

摘自官方文档:The maximum permitted number of simultaneous client connections[1]

System Variable[max_connections]
ScopeGlobal
DynamicYes

mysql.user表中的max_connection

mysql.user表中的max_connection 意味着一个特定的用户每小时能链接数据库的次数。当我们创建一个用户时,可以指定如下相关参数:这些参数在一定程度上限制了用户对于资源的使用。

resource_option: {
    MAX_QUERIES_PER_HOUR count
  | MAX_UPDATES_PER_HOUR count
  | MAX_CONNECTIONS_PER_HOUR count
  | MAX_USER_CONNECTIONS count
}

对于MAX_CONNECTIONS_PER_HOUR,官方文档的解释为:"connections to the server are permitted to each account during any given one-hour period"[2]。

尝试创建一个普通用户:这里将MAX_CONNECTIONS_PER_HOUR 设置为3

mysql> CREATE USER 'rudonx'@'%' identified by 'xxxxxxxx' with MAX_CONNECTIONS_PER_HOUR 3; 

进行查看 user 的相关信息:

mysql> select * from mysql.user where user='rudonx'\G;
*************************** 1. row ***************************
                  Host: %
                  User: rudonx
  .....
       max_connections: 3 -->MAX_CONNECTIONS_PER_HOUR 为3,这里的max_connections=MAX_CONNECTIONS_PER_HOUR
  max_user_connections: 0
                plugin: mysql_native_password
 authentication_string: *39611CF83250A61AA29C2EE534DE8095EE9E93B1
      password_expired: N
 password_last_changed: 2022-01-05 03:21:54
     password_lifetime: NULL
        account_locked: N
1 row in set (0.03 sec)
1 row in set (0.02 sec)

优先级

假设我们将 user 表中的 max_connections 设置为2,而全局参数max_connections 设置为3,当使用同一个用户进行登录,当connections 大于2时,用户的配置会先于max_connections 生效。

参考文档

[1] https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_max_connections

[2] https://dev.mysql.com/doc/refman/5.7/en/create-user.html#create-user-resource-limits

如果您有其他问题,欢迎您联系火山引擎技术支持服务

136
0
0
0
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论