我正在運行一個運行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