为什么我的NodeJS程序打开与Mongo的多个连接(使用本机驱动程序) [英] Why my NodeJS program opens multiple connections to Mongo (using native driver)
问题描述
从NodeJS程序中,我使用本机驱动程序连接到Mongodb.我启动mongod
并看到服务器正在等待连接.当我的程序连接时,我可以看到5个连接而不是一个.我不明白为什么会这样,因为我似乎并没有做任何异常的事情.来自mongod
的消息如下:
From my NodeJS program I connect to Mongodb with native driver. I start mongod
and see server waiting for connections. When my program connects I can see however 5 connections instead of one. I don't understand why is happening as I do nothing unusual it seems. Mesage from mongod
is below:
2015-05-02T15:35:17.635+1000 [initandlisten] waiting for connections on port 27017
2015-05-02T15:36:17.638+1000 [clientcursormon] mem (MB) res:51 virt:508
2015-05-02T15:36:17.639+1000 [clientcursormon] mapped (incl journal view):320
2015-05-02T15:36:17.639+1000 [clientcursormon] connections:0
2015-05-02T15:37:11.594+1000 [initandlisten] connection accepted from 127.0.0.1:52976 #1 (1 connection now open)
2015-05-02T15:37:11.615+1000 [conn1] end connection 127.0.0.1:52976 (0 connections now open)
2015-05-02T15:37:11.625+1000 [initandlisten] connection accepted from 127.0.0.1:52977 #2 (1 connection now open)
2015-05-02T15:37:11.626+1000 [initandlisten] connection accepted from 127.0.0.1:52978 #3 (2 connections now open)
2015-05-02T15:37:11.627+1000 [initandlisten] connection accepted from 127.0.0.1:52979 #4 (3 connections now open)
2015-05-02T15:37:11.628+1000 [initandlisten] connection accepted from 127.0.0.1:52980 #5 (4 connections now open)
2015-05-02T15:37:11.628+1000 [initandlisten] connection accepted from 127.0.0.1:52981 #6 (5 connections now open)
这是我的NodeJS代码,为清晰起见,我删除了其中的一部分:
This is my NodeJS code, I stripped out some parts of it for clarity:
MongoClient.connect(mongodb_connection_string, function (err, db) {
var collections = {};
collections.users = db.collection("users");
collections.disciplines = db.collection("disciplines");
var users = require("./models/users"),
disciplines = require("./models/disciplines");
users.setDb(collections);
disciplines.setDb(collections);
app.use(session({
secret: 'keyboard cat',
store: new MongoStore({
db: db,
ttl: 2 // in minutes
// ttl: 14 * 24 * 60 * 60 // 14 days
}),
saveUninitialized: false,
resave: true
}));
app.all("/*", function (req, res, next) {
res.sendfile("index.html", {
root: __dirname + "/public"
});
});
var server = app.listen(server_port, server_ip_address, function () {});
});
当我从mongo控制台连接时,只有一个连接. Nodejs本机mongo驱动程序是否维护一些连接池?那是我最好的猜测,否则不知道我在做什么错.
When I connect from mongo console I got one connection only. Is Nodejs native mongo driver maintaining some pool of connection maybe? Thats my best guess, otherwise not sure what I'm doing wrong.
推荐答案
从文档 https://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html
Connection pool configuration:
maxPoolSize=n: The maximum number of connections in the connection pool
Default value is 5
这篇关于为什么我的NodeJS程序打开与Mongo的多个连接(使用本机驱动程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!