开发者

nodejs/mongoose: connect to db that's using replica sets

开发者 https://www.devze.com 2023-04-02 03:02 出处:网络
i have installed mongodb (1.8.3) on two seperate servers and set them up to use \"replica sets\" as found here: http://www.mongodb.org/display/DOCS/Replica+Set+Tutorial

i have installed mongodb (1.8.3) on two seperate servers and set them up to use "replica sets" as found here: http://www.mongodb.org/display/DOCS/Replica+Set+Tutorial

everything looks good so far: one server is recognized as primary, one as secondary (when i access them via commandline).

the problem is that i can't connect to the DB using node.js (0.4.10) and mongoose (2.1.0) like this:

var mongo = require('mongoose');
mongo.connectSet('mongodb://host/dbname,mongodb://host2/dbname');

i always get the following error message:

TypeError: Cannot read property 'reconnectWait' of undefined
    at new <anonymous> (/var/www/node/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connections/repl_set_servers.js:23:31)
    at NativeConnection.doOpenSet (/var/www/node/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:80:18)
    at NativeConnection.openSet (/var/www/node/node_modules/mongoose/lib/connection.js:252:8)
    at Mongoose.connectSet (/var/www/node/node_modules/mongoose/lib/index.js:116:27)
...

searched around a bit and found a post somewhere saying that i also have to supply the name of the replica set - so i tried this instead:

mongo.connectSet('mongodb://host/dbname,mongodb://host2/dbname', rs_name:"n开发者_如何学运维ame_replicaset"});

what am i doing wrong here ...?!


ok, there was an error in the https://github.com/christkv/node-mongodb-native module. it's fixed now but not yet pushed to NPM. so for all you guys getting the same error, here is the fix:

https://github.com/christkv/node-mongodb-native/pull/340

after that, you can just say

var mongo = require('mongoose');
mongo.connectSet('mongodb://host:27018/testdb, host2:27017/testdb, host3:27019/testdb', function (err) {
  if (err) {
    console.log("could not connect to DB: " + err);
  }
});
mongo.connection.on('open', function () {
  console.log("mongodb connection open");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消