最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Node jsMongoDB replica set array in javascript - Stack Overflow

programmeradmin0浏览0评论

Warning: I'm a novice programmer (more of sysadmin). We were given an node js application that's using MongoDB. From what I can tell, the mongo.js file is using mongojs and monq java classes. It was setup with only one MongoDB and I'm trying to setup a new HA environment to use a replica set. Here is what they provided:

var mongojs = require('mongojs');
var monq = require('monq');
var dbName = 'exampledb';
var db = mongojs(dbName, ['collections']);
var client = monq('mongodb://127.0.0.1/exampledb', { w: 1 });

exports.db = db;
exports.ObjectId = mongojs.ObjectId;
exports.monqClient = client;

Now for a replica set, according to this article, I need to make the following change:

var db = mongojs('replset0, replset1, replset2/mydb?slaveOK=true?', ['collections']);

I'm not entirely sure what I need to do for the line after that. I'm guessing I would have to create an array that would contain the host name and port # for each member of the replica set (setup is primary, secondary, arbiter) such as:

var replSet = new replSet();
var replSet[0] = "server0:port0"
var replSet[1] = "server1.:port1"
var replSet[2] = "server2.:port2"

How would I go about detecting which node is the primary? Also if the primary were to fail, I would have to restart the node js application (using forever)?

Warning: I'm a novice programmer (more of sysadmin). We were given an node js application that's using MongoDB. From what I can tell, the mongo.js file is using mongojs and monq java classes. It was setup with only one MongoDB and I'm trying to setup a new HA environment to use a replica set. Here is what they provided:

var mongojs = require('mongojs');
var monq = require('monq');
var dbName = 'exampledb';
var db = mongojs(dbName, ['collections']);
var client = monq('mongodb://127.0.0.1/exampledb', { w: 1 });

exports.db = db;
exports.ObjectId = mongojs.ObjectId;
exports.monqClient = client;

Now for a replica set, according to this article, I need to make the following change:

var db = mongojs('replset0., replset1., replset2./mydb?slaveOK=true?', ['collections']);

I'm not entirely sure what I need to do for the line after that. I'm guessing I would have to create an array that would contain the host name and port # for each member of the replica set (setup is primary, secondary, arbiter) such as:

var replSet = new replSet();
var replSet[0] = "server0:port0"
var replSet[1] = "server1.:port1"
var replSet[2] = "server2.:port2"

How would I go about detecting which node is the primary? Also if the primary were to fail, I would have to restart the node js application (using forever)?

Share Improve this question edited Mar 11, 2015 at 20:25 MForMarlon 8619 silver badges24 bronze badges asked Oct 29, 2013 at 18:21 nocodenocode 1,3161 gold badge15 silver badges22 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

I found the answer as it's calling MongoDB's URI http://docs.mongodb/manual/reference/connection-string/

Should be something like:

var client = monq('mongodb://server0:port0,server1:port1,server2:port2/[dbname]?replicaSet=[replicaSet Name]

First question: As long as you give it all of the members in the connection string, the mongojs driver should be able to figure out which one is primary. No need to figure it out yourself.

Second question: No, you don't have to restart the node app. The other members in the set will elect a new primary, although it takes time for mongo to detect failure and run the election.

For more information, see the mongodb docs on replica sets.

发布评论

评论列表(0)

  1. 暂无评论