ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

openstack中数据库连接数太多--pymysql.err.OperationalError,1040, u'Too many connections'

2021-06-03 22:56:48  阅读:263  来源: 互联网

标签:1040 err -- max mysql 连接数 connections MySQL +-----------------+-------+


1.出现问题:

openstack运行过程中出现如下问题:

OperationalError: (pymysql.err.OperationalError) (1040, u'Too many connections')

DBConnectionError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'controller' ([Errno 111] ECONNREFUSED)") [SQL: u'SELECT 1']

2.查看 mysql 状态

 2.1查看 mysql 的最大连接数

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

2.2 查看服务器响应的最大连接数

mysql> show global status like 'Max_used_connections';
+----------------------+-------+
| Variable_name    | Value     |
+----------------------+-------+
| Max_used_connections | 215   |
+----------------------+-------+
1 row in set (0.00 sec)

对于 mysql 服务器最大连接数值的设置范围比较理想的是:服务器响应的最大连接数值占服务器上限连接数值的比例值在 10% 以上,如果在 10% 以下,说明 mysql 服务器最大连接上限值设置过高。

Max_used_connections / max_connections * 100%

 

这的服务器响应的最大连接数已经达到了 mysql 的最大连接数的最大值

3.问题分析

3.1 max_connections

MySQL无论如何都会保留一个用于管理员(SUPER)登陆的连接,用于管理员连接数据库进行维护操作,即使当前连接数已经达到了max_connections。因此MySQL的实际最大可连接数为 max_connections+1

这个参数实际起作用的最大值(实际最大可连接数)为16384,即该参数最大值不能超过16384,即使超过也以16384为准; 增加max_connections参数的值,不会占用太多系统资源。系统资源(CPU、内存)的占用主要取决于查询的密度、效率等; 该参数设置过小的最明显特征是出现”Too many connections”错误;

3.2 mysql 最大连接数 214 问题

如果我设置连接小于214时,比如 200,那么实际连接数就是 200,也就是说,我的配置文件是没有问题的。

查 MySQL 官方文档,里面说了

The maximum number of connections MySQL can support depends on the quality of the thread library on a given platform, the amount of RAM available, how much RAM is used for each connection, the workload from each connection, and the desired response time. Linux or Solaris should be able to support at 500 to 1000 simultaneous connections routinely and as many as 10,000 connections if you have many gigabytes of RAM available and the workload from each is low or the response time target undemanding. Windows is limited to (open tables × 2 + open connections) < 2048 due to the Posix compatibility layer used on that platform.

Increasing open-files-limit may be necessary. Also see Section 2.5, “Installing MySQL on Linux”, for how to raise the operating system limit on how many handles can be used by MySQL.

大概意思是 MySQL 能够支持的最大连接数量受限于操作系统,必要时可以增大 open-files-limit。换言之,连接数与文件打开数有关。

4 问题解决

更改 MySQL 在 Linux 的最大文件描述符限制,编辑/usr/lib/systemd/system/mariadb.service文件,在文件[Service]下添加:

LimitNOFILE=65535
LimitNPROC=65535

 

保存后,执行下面命令,使配置生效

# systemctl daemon-reload
# systemctl restart  mariadb.service

 

问题解决

MariaDB [(none)]> show variables like 'max_connections';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 4096  |
+-----------------+-------+

标签:1040,err,--,max,mysql,连接数,connections,MySQL,+-----------------+-------+
来源: https://blog.51cto.com/u_14841814/2855069

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有