[英]Can I call rs.initiate() and rs.Add() from node.js using the MongoDb driver?
I'm looking to automate the process of setting up a MongoDb replica set via a sidecar when using Docker and Kubernetes. 我想在使用Docker和Kubernetes时通过边车自动设置MongoDb副本集的过程。
The above setup isn't terribly important , what it boils down to is that I need to be able to call the mongo replica set commands (eg rs.initiate()
, rs.add('anotherserver')
, rs.conf()
, rs.reconfig()
, etc) from a node.js application. 上面的设置并不是非常重要 ,它归结为我需要能够调用mongo副本集命令(例如
rs.initiate()
, rs.add('anotherserver')
, rs.conf()
来自node.js应用程序的rs.reconfig()
等)。
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. 注意:它不必来自节点应用程序,如果有人知道完成同样事情的另一种方式,请分享您的想法。
UPDATE: I was able to get this working and have made the sidecar open source for others to use. 更新:我能够使这个工作,并使其他人使用的边车开源。
The rs.*
replica set admin helpers in the mongo
shell are wrappers for MongoDB commands which you can send from any driver. mongo
shell中的rs.*
副本集管理助手是MongoDB命令的包装器,您可以从任何驱动程序发送它们。
You can see which command(s) each shell helper wraps by referring to the MongoDB documentation: 您可以通过引用MongoDB文档来查看每个shell帮助程序包装的命令:
rs.initiate()
provides a wrapper around the replSetInitiate
database command. rs.initiate()
提供了replSetInitiate
数据库命令的包装器。 rs.add()
provides a wrapper around some of the functionality of the replSetReconfig
database command and the corresponding mongo shell helper rs.reconfig()
. rs.add()
提供了replSetReconfig
数据库命令和相应的mongo shell helper rs.reconfig()
一些功能的包装器。 rs.conf()
wraps the replSetGetConfig
database command. rs.conf()
包装replSetGetConfig
数据库命令。 Note that the mongo
shell helpers may do some extra validation or manipulation of configs as they are intended to be used via the interactive mongo
shell. 请注意,
mongo
shell帮助程序可以对配置进行一些额外的验证或操作,因为它们旨在通过交互式mongo
shell使用。
You can confirm how any of the shell helpers are implemented by invoking the command in the shell without trailing parentheses, eg: 您可以通过在shell中调用命令而不使用尾部括号来确认如何实现任何shell助手,例如:
> rs.initiate
function (c) { return db._adminCommand({ replSetInitiate: c }); }
The equivalent logic can be implemented via the Node.js driver API using command()
: 可以使用
command()
通过Node.js驱动程序API实现等效逻辑:
// 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.
注意:它不必来自节点应用程序,如果有人知道完成同样事情的另一种方式,请分享您的想法。
Rather than reimplementing the replica set helpers in Node.js, you could invoke a mongo
shell with the --eval
command to run the shell helper (tip: include --quiet
to suppress unnecessary messages). 您可以使用
--eval
命令调用mongo
shell来运行shell帮助程序(tip:include --quiet
来抑制不必要的消息),而不是重新实现Node.js中的副本集帮助程序。
For example, calling from your Node app: 例如,从您的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);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.