我正在运行一个运行MongoDB的应用程序2.4.5,目前升级是不可能的。在MongoDB 2.4.5驱动程序中替代`replSetGetConfig`吗?
我在node.js中写了一些自动化脚本来启动一个副本集,但是由于我刚开始使用3个完全相同的现有mongodb节点,我不能只在所有3个节点上使用replSetInitiate
命令 - 我需要初始化一个我打算成为主节点的节点,然后再拨打replSetReconfig
并使用附加节点2使它们擦除并同步。
问题是,我打电话replSetGetConfig
命令来获取我可以操纵和发回的配置对象,但是此命令仅在mongodb 3.0中添加。那么我的替代品是什么?有没有替代命令replSetGetConfig
?在replSetInitiate
完成后,我有什么办法可以自己生成适当的配置对象吗?或者我应该放弃并使用rs.conf()
运行一个mongo外壳?
这是代码是什么样子,现在,它不工作,所说的版本:
return connectToMongoDB(host)
.then((db) => {
// Initial configuration contains only the intended primary
var cfg = {
_id : id,
members : [{ _id: 0, host: host }]
};
return executeMongoCommand(db, { replSetInitiate : cfg })
.then((res) => {
// Passing on the db object so I don't have to reconnect
return {
db: db
};
});
})
.then((data) => {
// This would work in 3.0.0 and up to get the current RS config, but doesn't work at all in 2.4.5
return executeMongoCommand(data.db, { replSetGetConfig: 1 })
.then((res) => {
// storing the config we got and passing it on with the db object to the next step
data.cfg = data;
return data;
})
})
.then((data) => {
otherNodes.forEach((val, idx) => {
data.cfg.members.push({ _id: idx+1, host: val });
});
return executeMongoCommand(data.db, { replSetReconfig : data.cfg });
})
.catch(console.error);
而返回的错误是no such cmd: replSetGetConfig
(作为一个方面说明,rs.conf()
应该是一个包装replSetGetConfig
不知何故包装是支持和底层功能不支持,不明白)
UPDATE/ANSWER:
基于@Stennie的回答下面我实现了以下功能得到这个信息的版本3.0.0双方:
function getRSconfig(db){
return new Promise((resolve, reject) => {
if(parseInt(mongoVersion, 10) < 3){
db.db("local").collection("system.replset").findOne()
.then((data) => {
resolve(data);
}, (err) => {
reject(err);
});
}
else {
executeMongoCommand(db, { replSetGetConfig: 1 })
.then((data) => {
resolve(data);
}, (err) => {
reject(err);
})
}
});
}
而且使用这一个获取最新版本:
function getMongoVersion(db){
var adminDb = db.admin();
adminDb.serverStatus(function(err, info) {
mongoVersion = info.version;
});
}
谢谢!像魅力一样工作。 – motig88