我使用mongoskin npm模块连接到我的mongo数据库副本集群。我尝试了所有可能的方法,但我的nodejs代码无法建立连接。我能够使用独立连接连接相同的主数据库,但是当我尝试使用副本服务器时它失败了。以下是更多细节
Code working for standalone:
var express = require('express');
var app = express();
var mongo = require('mongoskin');
//connect primary as standalone
var db = mongo.db("mongodb://username:password@177.77.66.9:27017/bhs", {native_parser: true});
app.get('/', function (req, res) {
db.bind('goalsfrequencymappings');
db.goalsfrequencymappings.find().toArray(function (err, items) {
res.json(items);
db.close();
});
});
var server = app.listen(8080, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
代码不能用作副本服务器:
var express = require('express');
var mongo = require('mongoskin');
var app = express();
var Server = mongo.Server;
var Db = mongo.Db;
var ReplSetServers = mongo.ReplSetServers;
var replSet = new ReplSetServers([
new Server('username:password@177.77.66.9:27017', 27017),
new Server('username:password@88.052.72.91', 27017),
new Server('username:password@95.46.865.455', 27017)
]);
var db = new Db('bhs', replSet, {w: 0, native_parser: (process.env['TEST_NATIVE'] != null)});
app.get('/', function (req, res) {
db.collection('goalsfrequencymappings').find().toArray(function (err, items) {
if(err){
console.log("error :"+err);
res.json(err);
} else {
console.log("items :"+items);
res.json(items);
}
db.close();
});
});
var server = app.listen(8080, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
打印以下错误:
Example app listening at http://0.0.0.0:8080
error :Error: No valid replicaset instance servers found
当我使用cli连接到群集数据库并运行status和config命令时,它看起来不错。
myReplicaCluster:PRIMARY> rs.status();
{
"set" : "myReplicaCluster",
"date" : ISODate("2015-05-22T05:18:47Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "177.77.66.9:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 6415902,
"optime" : Timestamp(1432268531, 1),
"optimeDate" : ISODate("2015-05-22T04:22:11Z"),
"electionTime" : Timestamp(1425856025, 11),
"electionDate" : ISODate("2015-03-08T23:07:05Z"),
"self" : true
},
{
"_id" : 1,
"name" : "88.052.72.91:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 6415566,
"optime" : Timestamp(1432268531, 1),
"optimeDate" : ISODate("2015-05-22T04:22:11Z"),
"lastHeartbeat" : ISODate("2015-05-22T05:18:46Z"),
"lastHeartbeatRecv" : ISODate("2015-05-22T05:18:47Z"),
"pingMs" : 4,
"syncingTo" : "177.77.66.9:27017"
},
{
"_id" : 2,
"name" : "95.46.865.455:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 6415543,
"optime" : Timestamp(1432268531, 1),
"optimeDate" : ISODate("2015-05-22T04:22:11Z"),
"lastHeartbeat" : ISODate("2015-05-22T05:18:46Z"),
"lastHeartbeatRecv" : ISODate("2015-05-22T05:18:46Z"),
"pingMs" : 0,
"syncingTo" : "177.77.66.9:27017"
}
],
"ok" : 1
}
myReplicaCluster:PRIMARY> rs.config();
{
"_id" : "myReplicaCluster",
"version" : 5,
"members" : [
{
"_id" : 0,
"host" : "177.77.66.9:27017"
},
{
"_id" : 1,
"host" : "88.052.72.91:27017"
},
{
"_id" : 2,
"host" : "95.46.865.455:27017"
}
]
}
请帮忙。
答案 0 :(得分:1)
在关注其他帖子之后,我意识到我正在为每个不需要的种子传递用户名,密码。只需遵循mongodb连接池语法即可。以下是我修改后的代码的详细信息,该代码在生产中正常运行。
//Set seed mongo replicas.
url = 'mongodb://username:password@177.77.66.9:27017,88.052.72.91:27017,95.46.865.455:27017/dbname?replicaSet=yourReplicaCluster';
// Connect DB
var db = mongoskin.db(url, {native_parser: true, 'auto_reconnect': true, 'poolSize': 100, socketOptions: {
keepAlive: 50,
connectTimeoutMS: 1000,
socketTimeoutMS: 0
}});
// Get documents
getDocumentByQuery: function (collection, callback) {
var query={};
//Add additional filter to exclude soft deleted documents
query.isDeleted = {$ne: true}
DBModule.db.collection(collection).find(query).toArray(function (err, docuemnts) {
if (!err) {
callback(null, docuemnts);
} else {
callback(err, null);
}
});
}