解决pymysql.err.OperationalError: (1040, ‘Too many connections‘)

我用sqlalchemy orm方式连接数据库,处理数据。一台机器上程序运行正常。但是另一台机器上就报错pymysql.err.OperationalError: (1040, 'Too many connections')。

首先我确定连接方式肯定没错。程序也没错。那猜测是两台机器上mysql的配置不同。百度之后,获得答案如下:以下是转载别人的答案。

pymysql.err.OperationalError: (1040, 'Too many connections') 超出连接数据库最大连接数所致,修改最大连接数

一、连接远程数据库:

YoungdeMacBook-Pro:ids Young$ mysql -h 172.117.43.14 -u root -p
Enter password: 

二、查看 mysql 状态

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

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

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

同时查看2条数据:

   '''
  mysql>  show variables like '%max_connections%';
  +------------------------+-------+
  | Variable_name          | Value |
  +------------------------+-------+
  | max_connections        | 151   |
  | mysqlx_max_connections | 100   |
  +------------------------+-------+
  2 rows in set (0.00 sec)
  '''

修改最大连接数:

  set global max_connections=1000 

查看数据库当前运行状态:

  mysql> show status like 'Threads%';
  +-------------------+-------+
  | Variable_name     | Value |
  +-------------------+-------+
  | Threads_cached    | 58    |
  | Threads_connected | 57    |   ###这个数值指的是打开的连接数
  | Threads_created   | 3676  |
  | Threads_running   | 4     |   ###这个数值指的是激活的连接数,这个数值一般远低于connected数值
  +-------------------+-------+

Threads_connected 跟show processlist结果相同,表示当前连接数。准确的来说,Threads_running是代表当前并发数

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

Max_used_connections / max_connections * 100%

三、问题分析

3.1 max_connections

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

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

3.2 mysql 最大连接数 151 问题

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

查 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  |
+-----------------+-------+


链接:https://www.jianshu.com/p/f682d5e7bc96
 

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值