MongoDB 连接数高产生原因及解决

Wesley13
• 阅读 1451

MongoDB Sharding架构下连接数很容易达到很高,这里连接数分为几个概念:
tcp 连接数 netstat可以统计的,一般这个是最高.如果mongod/mongos在同一台服务器,更明显。
参考命令:netstat -ant|awk '{print $5}' |awk -F: '{print $1}'|sort |uniq -c|sort -rn
mongos/mongod 连接数 mongostat/db.serverStatus()/connPoolStats可统计。
连接数多高算高呢?
这要看连接到mongodb集群应用服务器实例数、qps(增删改查)等判断。
应用服务器单台,如果qps<100, ,mongos连接数超过2000,肯定是高了。这一般是由于连接池配置不合理导致。
mongod/mongos 默认最大连接数maxConns=20000,2.4版本及以前版本最大不能超过这个数值,2.6版本(开发版2.5版本)取消这个限制。
相关链接http://nosqldb.org/topic/50ca8a50ee680fee790001f2

什么原因导致连接数过高

  • 连接池配置不合理
    分片情况下,现象是tcp 连接数过高(如达到20000),mongos连接数过高(如超过10000)
    java为例,connectionsPerHost 不宜配置过大,官方默认值由原来10改成100了,而且有默认5倍的乘数(threadsAllowedToBlockForConnectionMultiplier),一般20~50就可以了。

  • 应用服务器实例过多

我们遇到的场景,当连接到mongos的应用服务器(如Tomcat实例数量)过百,甚至达到近200台时,tcp连接数超高,达到15000以上,查看mongod对应端口连接数高达8000多,mongos 2000多。此时ops(query,insert,update,delete)低于200每秒,。定期重启(如一周一次)mongos可适当缓解该问题。

  • mongodb本身的原因表现为mongos连接数不高(如1000+),mongod连接数比较高(如8000+)。

 

如何解决
总结一下,连接数高分为几个场景:
应用服务器实例过多,可统计每个实例建立的连接数,适当调低连接池参数。
mongos连接数高,这种就是配置的问题,更改连接池参数。
mongos连接数不高,mongod连接数比较高,如超过5000,如果连接池配置合理还比较高的话,尝试启用releaseConnectionsAfterResponse参数(2.2.4版本以上),该参数为
隐藏参数releaseConnectionsAfterResponse

  1. mongos> use admin

  2. switched to db admin

  3. mongos > db.runCommand({ setParameter : 1, releaseConnectionsAfterResponse : true })

  4. { "was" : false, "ok" : 1 }

或者

shell> mongos --setParameter "releaseConnectionsAfterResponse=true" --configdb ...

该参数注意事项:
写操作需要立即调用getLastError (w=1,即安全写模式),w=2(等待从库写确认)的时候可能会有些错误。
升级过后,或者重启mongos进程后,需要重新设置该参数,该参数只对单个mongos生效。
启用releaseConnectionsAfterResponse 参数,tcp 连接数明显降低到比较稳定数目。几个小时,tcp连接数从8000多降到4000多,效果不错。

  • releaseConnectionsAfterResponse 参数原理

 

通常,对于每个mongos->mongod连接是单独缓存的,并且该连接不能重复使用,即使该连接是空闲时也是如此,一直到连接关闭连接回到连接池中才能再使用;releaseConnectionsAfterResponse 参数启用后,mongos->mongod之间的连接在完成一个读操作或者安全写操作后能够重复使用(把连接放到连接池中而不是缓存,即更早的回归到连接池中),releaseConnectionsAfterResponse参数简单讲就是mongos->mongod的连接更早的回到连接池中,这样就不会开太多的连接了,从而减少连接数。
Create a new serverParameter for mongos, "releaseConnectionsAfterResponse," which enables returning ShardConnections from the per-socket pool to the global pool after each read operation. This should reduce the total number of outgoing mongos connections to each shard.
the option allows better use of the connection pool, it doesn't invalidate the connections in the pool. Normally, mongos->mongod connections for insert/update/delete/query are cached individually for each incoming connection, and can't be re-used until the incoming connection is closed, even if they are idle and there are other active incoming connections.
What the releaseConnectionsAfterResponse option does is allow the mongos->mongod connection to be re-used (returned to the pool) after any read op (including getLastError(), so after safe writes as well). It shouldn't have a significant performance impact - the connection isn't destroyed, it's just returned from the incoming connection cache to the shared pool early.

代码:
https://github.com/mongodb/mongo/commit/706459a8af0b278609d70e7122595243df6aeee8
https://github.com/mongodb/mongo/commit/74323d671a216c8c87fcb295ed743f830d5212ee
https://github.com/mongodb/mongo/commit/5d5fe49dfb5f452832b9d44fddbfb2a4e8b42f2a

===============
- connPoolTimeout设置

(该参数不在官方没有)
效果

  1. mongos> db.runCommand({ setParameter : 1, connPoolTimeout : 900 })

  2. { "was" : 1800, "ok" : 1 }

初步测试,效果不明显。

  • releaseConnections

 

计划添加个命令releaseConnections,从mongod运行,减少复制集连接数。

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
JS 苹果手机日期显示NaN问题
问题描述newDate("2019122910:30:00")在IOS下显示为NaN原因分析带的日期IOS下存在兼容问题解决方法字符串替换letdateStr"2019122910:30:00";datedateStr.repl
Stella981 Stella981
2年前
Python之time模块的时间戳、时间字符串格式化与转换
Python处理时间和时间戳的内置模块就有time,和datetime两个,本文先说time模块。关于时间戳的几个概念时间戳,根据1970年1月1日00:00:00开始按秒计算的偏移量。时间元组(struct_time),包含9个元素。 time.struct_time(tm_y
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这