MongoDB 中重新配置 replSet 问题

Sun*_*Sun 3 replication mongodb

我曾经在同一个数据库文件夹中尝试过副本集演示。副本集名称为“test”。然后我尝试使用不同的名称“repl”重新构建 repl 集。重新配置后,我收到以下错误消息:

{
        "ok" : 0,
        "errmsg" : "replSetReconfig should only be run on PRIMARY, but my state is REMOVED; use the \"force\" argument to override",
        "code" : 10107,
        "codeName" : "NotMaster"
}
Run Code Online (Sandbox Code Playgroud)

所以我按照 errmsg 操作,并尝试运行以下命令:

rs.reconfig(config,{"force":true})
Run Code Online (Sandbox Code Playgroud)

现在我收到以下错误消息:

{
        "ok" : 0,
        "errmsg" : "New and old configurations differ in replica set name; old was test, and new is repl",
        "code" : 103,
        "codeName" : "NewReplicaSetConfigurationIncompatible"
}
Run Code Online (Sandbox Code Playgroud)

太糟糕了!所以我使用以前的名称“test”。但我再次收到错误。

{                                                                                                                       
        "ok" : 0,                                                                                                       
        "errmsg" : "New and old configurations differ in the setting of the arbiterOnly field for member 127.0.0.1:27020
; to make this change, remove then re-add the member",                                                                  
        "code" : 103,                                                                                                   
        "codeName" : "NewReplicaSetConfigurationIncompatible"                                                           
}  
Run Code Online (Sandbox Code Playgroud)

我实在不明白,如果每一个配置信息都必须和之前一致,为什么还要重新配置呢?

有人说可以drop本地数据库,但是这个操作还是不行。

 {
            "ok" : 0,
            "errmsg" : "Cannot drop 'local' database while replication is active",
            "code" : 20,
            "codeName" : "IllegalOperation"
    } 
Run Code Online (Sandbox Code Playgroud)

哦,MGD!我只是不明白为什么在当前数据库文件夹中重新启动 replset 如此困难。

Md *_*han 5

根据 MongoDB BOL Here To 重新配置现有副本集,覆盖现有副本集配置。要运行该方法,您必须连接到副本集的主副本。

\n\n
rs.reconfig(configuration, force)\n
Run Code Online (Sandbox Code Playgroud)\n\n

要重新配置现有副本集,请首先使用 检索当前配置rs.conf(),根据需要修改配置文档,然后将修改后的文档传递给rs.reconfig()

\n\n

rs.reconfig() 提供了replSetReconfig命令的包装器。

\n\n

Force 参数允许向非主节点发出重新配置命令。

\n\n
\n

警告:避免重新配置包含不同 MongoDB 版本成员的副本集,因为验证规则可能因 MongoDB 版本而异。

\n
\n\n

shellrs.reconfig()方法在某些情况下可以触发当前的主节点下台。当主节点宕机时,它会强制关闭所有客户端连接。这是设计使然。由于可能需要一段时间才能选择新的主数据库,因此请在维护期间安排重新配置更改,以最大程度地减少写入可用性的损失。

\n\n
{ force: true }\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n

警告: \n 将 rs.reconfig() 与 {force: true } 一起使用可能会导致\n 已提交的写入回滚。使用此选项时请务必小心。

\n
\n\n

成员优先级和投票\n已更改MongoDB version 3.2

\n\n

优先级大于0不能拥有的成员0 votes

\n\n

Non-voting会员必须拥有 的优先权0

\n\n

例如,名为rs0的副本集具有以下配置:

\n\n
{\n   "_id" : "rs0",\n   "version" : 1,\n   "protocolVersion" : NumberLong(1),\n   "members" : [\n      {\n         "_id" : 0,\n         "host" : "mongodb0.example.net:27017",\n         "arbiterOnly" : false,\n         "buildIndexes" : true,\n         "hidden" : false,\n         "priority" : 1,\n         "tags" : {\n\n         },\n         "slaveDelay" : NumberLong(0),\n         "votes" : 1\n      },\n      {\n         "_id" : 1,\n         "host" : "mongodb1.example.net:27017",\n         "arbiterOnly" : false,\n         "buildIndexes" : true,\n         "hidden" : false,\n         "priority" : 1,\n         "tags" : {\n\n         },\n         "slaveDelay" : NumberLong(0),\n         "votes" : 1\n      },\n      {\n         "_id" : 2,\n         "host" : "mongodb2.example.net:27017",\n         "arbiterOnly" : false,\n         "buildIndexes" : true,\n         "hidden" : false,\n         "priority" : 1,\n         "tags" : {\n\n         },\n         "slaveDelay" : NumberLong(0),\n         "votes" : 1\n      }\n   ],\n   "settings" : {\n      "chainingAllowed" : true,\n      "heartbeatIntervalMillis" : 2000,\n      "heartbeatTimeoutSecs" : 10,\n      "electionTimeoutMillis" : 10000,\n      "catchUpTimeoutMillis" : 2000,\n      "getLastErrorModes" : {\n\n      },\n      "getLastErrorDefaults" : {\n         "w" : 1,\n         "wtimeout" : 0\n      },\n      "replicaSetId" : ObjectId("58858acc1f5609ed986b641b")\n   }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

以下操作序列更新第二个成员的members[n].priority 。这些操作是通过连接到主数据库的mongo shell 发出的。

\n\n
cfg = rs.conf();\ncfg.members[1].priority = 2;\nrs.reconfig(cfg);\n
Run Code Online (Sandbox Code Playgroud)\n\n

正如上述声明适用于诸如

\n\n
    \n
  1. 第一个语句使用 rs.conf() 方法检索包含副本集当前配置的文档,并将该文档设置为本地变量 cfg。

  2. \n
  3. 第二条语句members[n].priority为成员数组中的第二个文档设置一个值。有关其他设置,请参阅副本集配置设置。

    \n\n
    To access the member configuration document in the array, the\nstatement uses the array index and not the replica set member\xe2\x80\x99s\n`members[n]._id` field.\n
    Run Code Online (Sandbox Code Playgroud)
  4. \n
  5. 最后一条语句rs.reconfig()使用修改后的 cfg 调用该方法来初始化此新配置。成功重新配置后,副本集配置将类似于以下内容:

    \n\n

    {\n "_id" : "rs0",\n "版本" : 2,\n "协议版本" : NumberLong(1),\n "成员" : [\n {\n "_id" : 0,\n "host" : "mongodb0.example.net:27017",\n "arbiterOnly" : false,\n "buildIndexes" : true,\n "hidden" : false,\n "priority" : 1,\n "tags ”:{

    \n\n
         },\n     "slaveDelay" : NumberLong(0),\n     "votes" : 1\n  },\n  {\n     "_id" : 1,\n     "host" : "mongodb1.example.net:27017",\n     "arbiterOnly" : false,\n     "buildIndexes" : true,\n     "hidden" : false,\n     "priority" : 2,\n     "tags" : {\n\n     },\n     "slaveDelay" : NumberLong(0),\n     "votes" : 1\n  },\n  {\n     "_id" : 2,\n     "host" : "mongodb2.example.net:27017",\n     "arbiterOnly" : false,\n     "buildIndexes" : true,\n     "hidden" : false,\n     "priority" : 1,\n     "tags" : {\n\n     },\n     "slaveDelay" : NumberLong(0),\n     "votes" : 1\n  }\n
    Run Code Online (Sandbox Code Playgroud)\n\n

    ],\n“设置”:{\n“chainingAllowed”:true,\n“heartbeatIntervalMillis”:2000,\n“heartbeatTimeoutSecs”:10,\n“electionTimeoutMillis”:10000,\n“catchUpTimeoutMillis”:2000,\ n“getLastErrorModes”:{

    \n\n
      },\n  "getLastErrorDefaults" : {\n     "w" : 1,\n     "wtimeout" : 0\n  },\n  "replicaSetId" : ObjectId("58858acc1f5609ed986b641b")\n
    Run Code Online (Sandbox Code Playgroud)\n\n

    }\n}

  6. \n
\n\n
\n

注意:更新副本配置对象时,请使用数组索引访问members数组中的副本集成员。数组索引从 0 开始。请勿将此索引值与members[n]._idmembers 数组中每个文档中的字段值混淆。

\n
\n\n

如需帮助,您可以输入mongoshell rs.help()

\n\n
> rs.help()\n        rs.status()                                { replSetGetStatus : 1 } checks repl set status\n        rs.initiate()                              { replSetInitiate : null } initiates set with default settings\n        rs.initiate(cfg)                           { replSetInitiate : cfg } initiates set with configuration cfg\n        rs.conf()                                  get the current configuration object from local.system.replset\n        rs.reconfig(cfg)                           updates the configuration of a running replica set with cfg (disconnects)\n        rs.add(hostportstr)                        add a new member to the set with default attributes (disconnects)\n        rs.add(membercfgobj)                       add a new member to the set with extra attributes (disconnects)\n        rs.addArb(hostportstr)                     add a new member which is arbiterOnly:true (disconnects)\n        rs.stepDown([stepdownSecs, catchUpSecs])   step down as primary (disconnects)\n        rs.syncFrom(hostportstr)                   make a secondary sync from the given member\n        rs.freeze(secs)                            make a node ineligible to become primary for the time specified\n        rs.remove(hostportstr)                     remove a host from the replica set (disconnects)\n        rs.slaveOk()                               allow queries on secondary nodes\n\n        rs.printReplicationInfo()                  check oplog size and time range\n        rs.printSlaveReplicationInfo()             check replica set members and replication lag\n        db.isMaster()                              check who is primary\n\n        reconfiguration helpers disconnect from the database so the shell will display\n        an error, even if the command succeeds.\n
Run Code Online (Sandbox Code Playgroud)\n\n

供您进一步参考这里这里

\n