简体   繁体   English

通过.Net驱动程序配置Mongo副本集

[英]Configure Mongo Replica Set via the .Net Driver

Is it possible using the .Net driver to: 使用.Net驱动程序可以:

  • Check whether a certain Mongo instance is already a part of a replica 检查某个Mongo实例是否已经是副本的一部分
  • Create a replica if not 如果不创建副本
  • Add \\ Remove a node from an existing replica 添加\\从现有副本中删除节点

Thanks! 谢谢!

Yes. 是。 But it's much easier to do these things in the mongo shell because it has helper functions defined already. 但是在mongo shell中执行这些操作要容易得多,因为它已经定义了辅助函数。 Check out the replica set tutorials . 查看副本集教程 For each step using some function in the mongo shell, for example rs.initiate() or rs.add() , you can see the code for the function by entering the function name with parentheses at the shell prompt: 对于在mongo shell中使用某个函数rs.initiate()例如rs.initiate()rs.add()每个步骤,您都可以通过在shell提示符下输入带有括号的函数名称来查看该函数的代码:

> rs.initiate
function (c) { return db._adminCommand({ replSetInitiate: c }); }
> rs.add
function (hostport, arb) {
    var cfg = hostport;

    var local = db.getSisterDB("local");
    assert(local.system.replset.count() <= 1, "error: local.system.replset has unexpected contents");
    var c = local.system.replset.findOne();
    assert(c, "no config object retrievable from local.system.replset");

    c.version++;

    var max = 0;
    for (var i in c.members)
        if (c.members[i]._id > max) max = c.members[i]._id;
    if (isString(hostport)) {
        cfg = { _id: max + 1, host: hostport };
        if (arb)
            cfg.arbiterOnly = true;
    }
    if (cfg._id == null){
        cfg._id = max+1;
    }
    c.members.push(cfg);
    return this._runCmd({ replSetReconfig: c });
}

The code may be slightly different in your shell because of shell version; 由于shell版本不同,您的shell中的代码可能略有不同。 my shell is 3.0.1. 我的外壳是3.0.1。 You can use this code as a guide for writing your own versions of these functions in C#. 您可以将此代码用作在C#中编写这些函数自己的版本的指南。 You will use the RunCommand method to run the commands like replSetReconfig and replSetInitiate on the server(s). 您将使用RunCommand方法在服务器上运行诸如replSetReconfigreplSetInitiate类的命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM