多节点 - mongodb本地连接

时间:2014-11-05 04:09:39

标签: javascript node.js mongodb node-mongodb-native

当我运行这个Node.js代码时:

var mongodb = require('mongodb'),
  MongoClient = mongodb.MongoClient;
MongoClient.connect('mongodb://localhost:27017/mydb', function(error, db) {
  if (error) {
    throw (error);
  }
  console.log('Connected!');
});

mongo日志显示5个连接打开:

sudo mongod
mongod --help for help and startup options
2014-11-04T21:03:23.107-0700 [initandlisten] MongoDB starting : pid=27572 port=27017 dbpath=/data/db 64-bit host=mylaptop
2014-11-04T21:03:23.107-0700 [initandlisten] db version v2.6.2
2014-11-04T21:03:23.107-0700 [initandlisten] git version: nogitversion
2014-11-04T21:03:23.107-0700 [initandlisten] build info: Darwin minimountain.local 12.5.0 Darwin Kernel Version 12.5.0: Sun Sep 29 13:33:47 PDT 2013; root:xnu-2050.48.12~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
2014-11-04T21:03:23.107-0700 [initandlisten] allocator: tcmalloc
2014-11-04T21:03:23.107-0700 [initandlisten] options: {}
2014-11-04T21:03:23.110-0700 [initandlisten] journal dir=/data/db/journal
2014-11-04T21:03:23.110-0700 [initandlisten] recover : no journal files present, no recovery needed
2014-11-04T21:03:23.136-0700 [initandlisten] waiting for connections on port 27017
2014-11-04T21:03:28.315-0700 [initandlisten] connection accepted from 127.0.0.1:61163 #1 (1 connection now open)
2014-11-04T21:03:28.323-0700 [conn1] end connection 127.0.0.1:61163 (0 connections now open)
2014-11-04T21:03:28.326-0700 [initandlisten] connection accepted from 127.0.0.1:61164 #2 (1 connection now open)
2014-11-04T21:03:28.326-0700 [initandlisten] connection accepted from 127.0.0.1:61165 #3 (2 connections now open)
2014-11-04T21:03:28.327-0700 [initandlisten] connection accepted from 127.0.0.1:61166 #4 (3 connections now open)
2014-11-04T21:03:28.328-0700 [initandlisten] connection accepted from 127.0.0.1:61167 #5 (4 connections now open)
2014-11-04T21:03:28.328-0700 [initandlisten] connection accepted from 127.0.0.1:61168 #6 (5 connections now open)

这看起来是对的吗?

1 个答案:

答案 0 :(得分:3)

不确定。 MongoClient使用节点本机驱动程序中的连接池选项。实际上这实际上是Server Object,默认情况下连接数为5。

您可以覆盖以下设置:

var async = require('async'),
    mongo = require('mongo'),
    MongoClient = mongo.MongoClient;


MongoClient.connect('mongodb://localhost/test',{ server: { poolSize: 1  }},function(err,db) {


});

设置" poolSize"在服务器选项中指定池中使用的连接数。最好坚持使用默认值或更高版本。