当前位置:首页 > PHP教程 > PHP总结归纳

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

mongodb sharding架构下连接数很容易达到很高,这里连接数分为几个概念: tcp 连接数 netstat可以统计的,一般这个是最高.如果mongod/mongos在同一台服务器,更明显。 参考命令:netstat -ant|awk {print $5} |awk -f: {print $1}|sort |uniq -c|sort -rn mo

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 use admin switched to db admin mongos> db.runcommand({ setparameter : 1, releaseconnectionsafterresponse : true }){ "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 inpidually 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设置

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

mongos> db.runcommand({ setparameter : 1, connpooltimeout : 900 }){ "was" : 1800, "ok" : 1 }

初步测试,效果不明显。

  • releaseconnections

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

转载自:http://nosqldb.org/topic/518510c8735345ad0a04fef8


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!