惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
N
Netflix TechBlog - Medium
Google Online Security Blog
Google Online Security Blog
月光博客
月光博客
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
V
V2EX
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
M
MIT News - Artificial intelligence
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
H
Help Net Security
The Cloudflare Blog
Blog — PlanetScale
Blog — PlanetScale
F
Full Disclosure
G
Google Developers Blog
罗磊的独立博客
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
A
About on SuperTechFans
IT之家
IT之家
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队
GbyAI
GbyAI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
The Register - Security
The Register - Security
U
Unit 42
D
Docker
Martin Fowler
Martin Fowler
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
阮一峰的网络日志
阮一峰的网络日志
C
Cybersecurity and Infrastructure Security Agency CISA
博客园_首页
Google DeepMind News
Google DeepMind News

PaulGuo

DeepSeek研究路线和原理分析 诸行无常:迈向醒觉之路 ChatGPT Plugin开发记录 技术规划的逻辑 前端感官性能的衡量和优化实践 美团点评酒旅前端的技术体系 NodeJS服务监控报警系统的核心实现和开源共建 美团酒店Node全栈开发实践 Node Cluster 笔记
深入了解MongoDB连接池
2015-05-02 · via PaulGuo

深入MongoDB连接池

随着应用程序的规模增大、功能增多,更好的管理资源显得尤为重要。对于大部分开发者而言,不合理的利用连接池是造成MongoDB性能问题的罪魁祸首。

连接池

新创建一个数据库连接的代价是非常昂贵的。因此不要为每个请求都创建一个新的连接然后销毁,而应当尽可能的复用现有的连接。这就是连接池的由来。

连接池顾名思义就是数据库连接的缓存,因此当需要有新的连接建立时可以复用现有的连接。如果使用恰当的话,连接池可以最大限度的降低数据库的新连接数目和创建频率。

Opening too many connections

Getting connection refused because too many open connections: 819

Default: depends on system (i.e. ulimit and file descriptor) limits. Unless set, MongoDB will not limit its own connections. You cannot set maxConns to a value higher than 20000.

MongoDB有一个名为maxConns的属性,可以用来设置最大连接数。默认情况下,maxConns的值为连接可用文件描述符数目的80%,因此Mac上默认值为204(Mac OS X下每个进程可用文件描述符默认值是256),CentOS下默认值为819(1024 * 0.8)。

ulimit -n

You open do MongoClient.connect once when your app boots up and reuse the db object. It’s not a singleton connection pool each .connect creates a new connection pool. [4]

如何查看当前的连接数?

> db.serverStatus().connections
> { "current" : 71, "available" : 748 }

mongostat

mongostat是mongdb自带的状态检测工具,在命令行下使用。它会间隔固定时间获取mongodb的当前运行状态并输出。当发现数据库突然变慢或者其它异常情况时,mongostat可以帮助我们快速的定位问题。

参考链接

  • http://stackoverflow.com/questions/15680985/what-is-the-right-way-to-deal-with-mongodb-connections
  • http://blog.mongolab.com/2013/11/deep-dive-into-connection-pooling/
  • http://xjsunjie.blog.51cto.com/999372/1360043
  • http://blog.csdn.net/largetalk/article/details/11656473
  • http://www.ttlsa.com/mongodb/initandlisten-connection-refused-because-too-many-open-connections-819-of-819/
  • http://www.littlelostmanuals.com/2011/11/increasing-mongodb-connection-pool.html
  • http://tomycat.github.io/2014/06/04/mongodb-note/
  • http://www.qttc.net/201304300.html
  • http://stackoverflow.com/questions/12124677/mongodb-connections-keep-increasing