我可以使用MongoDb驱动程序从node.js调用rs.initiate()和rs.Add()吗? [英] Can I call rs.initiate() and rs.Add() from node.js using the MongoDb driver?

查看:92
本文介绍了我可以使用MongoDb驱动程序从node.js调用rs.initiate()和rs.Add()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在使用Docker和Kubernetes时通过侧面设置一个MongoDb副本集的过程进行自动化。



以上设置isn'非常重要的,它归结为我需要能够调用mongo replica set命令(例如 rs.initiate() rs.add('anotherserver') rs.conf() rs.reconfig() 等)从node.js应用程序。



注意:它不一定是从节点应用程序,如果有人知道完成相同的事情的另一种方式,请分享您的想法。 >

更新:我能够获得这项工作,并为其他人提供了边界开源。




解决方案

如何实现副本设置管理员助手?



mongo中的手动/参考/方法/ js-replication /rel =noreferrer> rs。* 副本集管理员帮助 shell是MongoDB命令的包装器,可以从任何驱动程序发送/ p>

通过参考MongoDB文档,您可以看到每个shell助手包含哪个命令:





请注意, mongo shell helpers可能会对配置进行一些额外的验证或操作,因为它们旨在通过交互式 mongo shell。



您可以通过调用外壳中的命令来确认shell中的任何一个shell助手是否可以实现括号,例如:

 > rs.initiate 
function(c){return db._adminCommand({replSetInitiate:c});



从Node.js调用副本集数据库命令



等效逻辑可以通过Node.js驱动程序API使用 command()

  //粗略等价的rs.initiate()
var MongoClient = require('mongodb')。MongoClient;

MongoClient.connect('mongodb:// localhost:27017 / test',function(err,db){

//使用admin数据库命令
var adminDb = db.admin();

//默认副本集conf
var conf = {};

adminDb.command({replSetInitiate:conf },function(err,info){
console.log(info);
});
});




注意:它不一定是来自节点应用程序,如果有人知道完成相同操作的另一种方式,请分享您的想法。


而不是重新实现Node中的副本集.js,你可以使用 - eval 命令调用一个 mongo shell来运行shell helper(tip:include



例如,从您的Node应用程序调用:

  var exec = require('child_process')。 
var rsAdmin = exec('mongo --evalvar res = rs.initiate(); printjson(res)--quiet',function(error,stdout,stderr){
// output在stdout
console.log(stdout);
});


I'm looking to automate the process of setting up a MongoDb replica set via a sidecar when using Docker and Kubernetes.

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 (e.g. rs.initiate(), rs.add('anotherserver'), rs.conf(), rs.reconfig(), etc) from a node.js application.

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.

解决方案

How are the replica set admin helpers implemented?

The rs.* replica set admin helpers in the mongo shell are wrappers for MongoDB commands which you can send from any driver.

You can see which command(s) each shell helper wraps by referring to the MongoDB documentation:

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.

You can confirm how any of the shell helpers are implemented by invoking the command in the shell without trailing parentheses, eg:

> rs.initiate
function (c) { return db._adminCommand({ replSetInitiate: c }); }

Calling replica set database commands from Node.js

The equivalent logic can be implemented via the Node.js driver API using 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.

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).

For example, calling from your Node app:

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);
});

这篇关于我可以使用MongoDb驱动程序从node.js调用rs.initiate()和rs.Add()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆