Mysql报too many connections详解

用过mysql的小伙伴们,大部分都会碰到应用程序或者数据库维护人员链接数据库的时候,报too many connections的错误,这个错误是怎么产生的,该如何解决呢,下面就给你们进行详细解答mysql

下面是个人mysql 5.7的测试环境,查看一下和链接相关的参数配置sql

mysql> show variables like '%connections%';
+----------------------+-------+
| Variable_name        | Value |
+----------------------+-------+
| max_connections      | 500   |
| max_user_connections | 0     |
+----------------------+-------+
2 rows in set (0.01 sec)复制代码

为了尽快让数据库链接耗尽,我在这里会修改一下参数配置数据库

mysql> set global max_connections=3;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like '%connections%';
+----------------------+-------+
| Variable_name        | Value |
+----------------------+-------+
| max_connections      | 3     |
| max_user_connections | 0     |
+----------------------+-------+
2 rows in set (0.01 sec)复制代码

设置了用户最大链接数为3,下面使用一个普通用户(tony)进行测试架构

mysql> select * from performance_schema.users;
+------+---------------------+-------------------+
| USER | CURRENT_CONNECTIONS | TOTAL_CONNECTIONS |
+------+---------------------+-------------------+
| NULL |                  28 |            636237 |
| tony |                   2 |           5071859 |
| root |                   1 |                44 |
+------+---------------------+-------------------+
3 rows in set (0.00 sec)复制代码

能够看到tony用户的链接到2个了,加上root用户,总的链接数据已经达到3个了,若是再使用tony用户进行链接库,会发生什么呢运维

[root@cbov10-tidb57-206 ~]# /u02/mysql/bin/mysql --socket=/u02/run/3308/mysql.sock -utony -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1040 (08004): Too many connections复制代码

已经开始报too many connections错误了,这个时候管理员去数据库进行定位,是什么缘由致使的这个报错,看看能不能链接上数据库socket

mysql> select * from performance_schema.users;
+------+---------------------+-------------------+
| USER | CURRENT_CONNECTIONS | TOTAL_CONNECTIONS |
+------+---------------------+-------------------+
| NULL |                  28 |            637195 |
| tony |                   2 |           5071860 |
| root |                   2 |                49 |
+------+---------------------+-------------------+
3 rows in set (0.00 sec)复制代码

到这里,细心的同窗已经发现问题了,tony用户和root用户链接数据已经超过max_connections定义的阀值3了,多了1个,这是怎么回事,难道是这个参数不起做用吗,那若是再用root用户链接数据库,看看时候能链接数据库性能

[root@cbov10-tidb57-206 ~]# /u02/mysql/bin/mysql --socket=/u02/run/3308/mysql.sock -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1040 (HY000): Too many connections复制代码

这个时候root用户也没法链接了,在这里解释一下为何总链接数会超一个,原来mysql数据库在max_connections以外,额外提供一个链接,提供给super权限用户进行故障诊断使用,因此你们在使用mysql数据库的时候,应用程序千万别用root去链接数据库,一旦发生问题,dba连看数据库性能的机会都没有了。测试

喜欢的同窗能够关注个人公众号(db_arch)(Mysql数据库运维与架构设计)

喜欢的同窗能够关注个人公众号(db_arch)(Mysql数据库运维与架构设计)spa

相关文章
相关标签/搜索