我可以使用 MongoDb 驱动程序从 node.js 调用 rs.initiate() 和 rs.Add() 吗?
Can I call rs.initiate() and rs.Add() from node.js using the MongoDb driver?
我希望在使用 Docker 和 Kubernetes 时通过 sidecar 自动设置 MongoDb 副本集。
上面的设置不是很重要,归结起来就是我需要能够调用 mongo 副本集命令(例如 rs.initiate()
、rs.add('anotherserver')
、rs.conf()
、rs.reconfig()
等)来自 node.js 应用程序。
注意:它不一定来自节点应用程序,如果有人知道完成相同事情的另一种方法,请分享您的想法。
更新:我能够使它正常工作,并已将 sidecar 开源供其他人使用。
副本集管理助手是如何实现的?
mongo
shell 中的 rs.*
replica set admin helpers 是 MongoDB 命令的包装器,您可以从任何驱动程序发送这些命令。
您可以通过参考 MongoDB 文档来查看每个 shell 帮助程序包装了哪些命令:
rs.initiate()
provides a wrapper around the replSetInitiate
数据库命令。
rs.add()
provides a wrapper around some of the functionality of the replSetReconfig
数据库命令和相应的 mongo shell helper rs.reconfig()
.
rs.conf()
wraps the replSetGetConfig
数据库命令。
请注意,mongo
shell 助手可能会对配置进行一些额外的验证或操作,因为它们旨在通过交互式 mongo
shell 使用。
您可以通过调用 shell 中不带尾随括号的命令来确认 shell 助手是如何实现的,例如:
> rs.initiate
function (c) { return db._adminCommand({ replSetInitiate: c }); }
从 Node.js
调用副本集数据库命令
等效逻辑可以通过 Node.js 驱动程序 API 使用 command()
:
实现
// Rough equivalent of rs.initiate()
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
// Use the admin database for commands
var adminDb = db.admin();
// Default replica set conf
var conf = {};
adminDb.command({replSetInitiate: conf}, function(err, info) {
console.log(info);
});
});
Note: it doesn't have to be from a node application, if someone knows of another way of getting the same thing done, please share your thoughts.
而不是在 Node.js 中重新实现副本集助手,您可以使用 --eval
命令调用 mongo
shell 运行 shell 助手(提示:包含 --quiet
以抑制不必要的消息)。
例如,从您的 Node 应用调用:
var exec = require('child_process').exec;
var rsAdmin = exec('mongo --eval "var res = rs.initiate(); printjson(res)" --quiet', function (error, stdout, stderr) {
// output is in stdout
console.log(stdout);
});
我希望在使用 Docker 和 Kubernetes 时通过 sidecar 自动设置 MongoDb 副本集。
上面的设置不是很重要,归结起来就是我需要能够调用 mongo 副本集命令(例如 rs.initiate()
、rs.add('anotherserver')
、rs.conf()
、rs.reconfig()
等)来自 node.js 应用程序。
注意:它不一定来自节点应用程序,如果有人知道完成相同事情的另一种方法,请分享您的想法。
更新:我能够使它正常工作,并已将 sidecar 开源供其他人使用。
副本集管理助手是如何实现的?
mongo
shell 中的 rs.*
replica set admin helpers 是 MongoDB 命令的包装器,您可以从任何驱动程序发送这些命令。
您可以通过参考 MongoDB 文档来查看每个 shell 帮助程序包装了哪些命令:
rs.initiate()
provides a wrapper around thereplSetInitiate
数据库命令。rs.add()
provides a wrapper around some of the functionality of thereplSetReconfig
数据库命令和相应的 mongo shell helperrs.reconfig()
.rs.conf()
wraps thereplSetGetConfig
数据库命令。
请注意,mongo
shell 助手可能会对配置进行一些额外的验证或操作,因为它们旨在通过交互式 mongo
shell 使用。
您可以通过调用 shell 中不带尾随括号的命令来确认 shell 助手是如何实现的,例如:
> rs.initiate
function (c) { return db._adminCommand({ replSetInitiate: c }); }
从 Node.js
调用副本集数据库命令等效逻辑可以通过 Node.js 驱动程序 API 使用 command()
:
// Rough equivalent of rs.initiate()
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
// Use the admin database for commands
var adminDb = db.admin();
// Default replica set conf
var conf = {};
adminDb.command({replSetInitiate: conf}, function(err, info) {
console.log(info);
});
});
Note: it doesn't have to be from a node application, if someone knows of another way of getting the same thing done, please share your thoughts.
而不是在 Node.js 中重新实现副本集助手,您可以使用 --eval
命令调用 mongo
shell 运行 shell 助手(提示:包含 --quiet
以抑制不必要的消息)。
例如,从您的 Node 应用调用:
var exec = require('child_process').exec;
var rsAdmin = exec('mongo --eval "var res = rs.initiate(); printjson(res)" --quiet', function (error, stdout, stderr) {
// output is in stdout
console.log(stdout);
});