本文介绍了javascript中的节点js / MongoDB副本集数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

警告:我是新手程序员(更多的是sysadmin)。我们得到了一个使用MongoDB的节点js应用程序。据我所知,mongo.js文件使用的是mongojs和monq java类。它只设置了一个MongoDB,我正在尝试设置一个新的HA环境来使用副本集。以下是他们提供的内容:

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.com, replset1.com, replset2.com/mydb?slaveOK=true?', ['collections']);

我不完全确定在此之后我需要做什么。我猜我必须为副本集的每个成员创建一个包含主机名和端口号的数组(setup是primary,secondary,arbiter),例如:

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"

我如何检测哪个节点是主节点?此外,如果主要失败,我将不得不重新启动节点js应用程序(永远使用)?

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)?

推荐答案

我找到了回答,因为它正在调用MongoDB的URI

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

应该是这样的:

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

这篇关于javascript中的节点js / MongoDB副本集数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 14:10