翻译或纠错本页面

配置一个隐藏节点

隐藏节点也是 replica set 的一员,但是将不会成为 primary 同时也对应用程序不可见。隐藏节点可以在 elections 中参与投票。有关隐藏节点的更多信息和其用途,请参见 隐藏节点

注意事项

很多情况下将节点设置为隐藏节点是用来协助 delayed members 的。如果我们仅仅需要防止该节点成为主节点,我们可以通过 priority 0 member 来实现。

If the settings.chainingAllowed setting allows secondary members to sync from other secondaries, MongoDB by default prefers non-hidden members over hidden members when selecting a sync target. MongoDB will only choose hidden members as a last resort. If you want a secondary to sync from a hidden member, use the replSetSyncFrom database command to override the default sync target. See the documentation for replSetSyncFrom before using the command.

例子

节点配置信息

To configure a secondary member as hidden, set its members[n].priority value to 0 and set its members[n].hidden value to true in its member configuration:

{
  "_id" : <num>
  "host" : <hostname:port>,
  "priority" : 0,
  "hidden" : true
}

配置步骤

The following example hides the secondary member currently at the index 0 in the members array. To configure a hidden member, use the following sequence of operations in a mongo shell connected to the primary, specifying the member to configure by its array index in the members array:

cfg = rs.conf()
cfg.members[0].priority = 0
cfg.members[0].hidden = true
rs.reconfig(cfg)

设置完毕后,该从节点的优先级将变为 0 来防止其升职为主节点,同时其也是对应用程序不可见的。在其他节点上执行 isMaster 或是 db.isMaster() 将不会显示隐藏节点。

When updating the replica configuration object, access the replica set members in the members array with the array index. The array index begins with 0. Do not confuse this index value with the value of the members[n]._id field in each document in the members array.

警告

  • The rs.reconfig() shell method can force the current primary to step down, which causes an election. When the primary steps down, the mongod closes all client connections. While this typically takes 10-20 seconds, try to make these changes during scheduled maintenance periods.
  • 为了能让复制集的配置修改成功应用,我们需要确保复制集的多数节点是可用的。如果我们的复制集是由偶数个节点组成的,那就需要新增一个 arbiter 来确保选举的正常进行。