对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

自管理副本集配置

您可以使用 rs.conf() 方法或 replSetGetConfig 命令访问副本集的配置。

要修改副本集的配置,请使用 rs.reconfig() 方法,并向该方法传递配置文档。

警告

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

以下文档提供了副本集配置文档的表示。副本集的配置可能仅包含以下设置的子集:

{
_id: <string>,
version: <int>,
term: <int>,
protocolVersion: <number>,
writeConcernMajorityJournalDefault: <boolean>,
configsvr: <boolean>,
members: [
{
_id: <int>,
host: <string>,
arbiterOnly: <boolean>,
buildIndexes: <boolean>,
hidden: <boolean>,
priority: <number>,
tags: <document>,
secondaryDelaySecs: <int>,
votes: <number>
},
...
],
settings: {
chainingAllowed : <boolean>,
heartbeatIntervalMillis : <int>,
heartbeatTimeoutSecs: <int>,
electionTimeoutMillis : <int>,
catchUpTimeoutMillis : <int>,
getLastErrorModes : <document>,
getLastErrorDefaults : <document>,
replicaSetId: <ObjectId>
}
}
_id

类型:字符串

副本集的名称。

_id 必须与命令行上指定给 mongodreplication.replSetName--replSet 的值相同。

提示

replSetName--replSet,了解如何设置副本集名称。

version

类型:int

一个递增的数字,区分副本集配置文档的修订版本和配置的先前迭代。

Replica set members use term and version to achieve consensus on the "newest" replica configuration. When members compare replica configuration documents, the configuration document with a larger term is considered the "newest". If term is the same or absent, the configuration document with the larger version is considered "newest".

term

类型:int

仅适用于 featureCompatibilityVersion (FCV) "4.4" 或更高版本。

An incrementing number used to distinguish revisions of the replica set configuration document from previous iterations of the configuration. The term of a configuration document matches the term of the replica set primary which performed the reconfiguration. The primary increments its term each time it steps up after winning an election. The primary ignores the term field if set explicitly in the replSetReconfig operation.

Issuing a force reconfiguration removes the term field. When the primary next issues replSetReconfig without force, it sets the term to its own term.

Replica set members use term and version to achieve consensus on the "newest" replica configuration. When members compare replica configuration documents, the configuration document with a larger term is considered the "newest". If term is the same or absent, the configuration document with the larger version is considered "newest".

configsvr

类型:布尔值

默认:false

指示副本集是否用于分片集群的配置服务器。如果副本集用于分片集群的配置服务器,则设置为 true

protocolVersion

类型:数字

默认值:1

MongoDB 仅支持 protocolVersion: 1,不再支持 protocolVersion: 0

writeConcernMajorityJournalDefault

类型:布尔值

默认值:true

如果 { w: "majority" } 写关注未显式指定日志选项 j,则确定该写关注的行为。

下表列出了 writeConcernMajorityJournalDefault 值和关联的 { w: "majority" } 行为:

{ w: "majority" } 行为

true

当大多数投票节点写入磁盘日志后,MongoDB 会确认该写入操作。

重要提示:writeConcernMajorityJournalDefaulttrue 时,副本集的所有投票节点都必须使用日志记录功能运行。

If any voting member of a replica set uses the in-memory storage engine, you must set writeConcernMajorityJournalDefault to false.

If any voting member of a replica set uses the in-memory storage engine and writeConcernMajorityJournalDefault is true, "majority" write operations may fail. These include operations that inherently use "majority" write concern, such as the replSetStepDown command, or various mongosh methods that by default use "majority" write concern, such as user management methods and role management methods.

Starting in version 4.2 (and 4.0.13 and 3.6.14 ), if a replica set member uses the in-memory storage engine (voting or non-voting) but the replica set has writeConcernMajorityJournalDefault set to true, the replica set member logs a startup warning.

false

当大多数投票节点在内存中应用该操作后,MongoDB 会确认该写入操作。

警告:

If any voting member of a replica set uses the in-memory storage engine, you must set writeConcernMajorityJournalDefault to false.

Starting in version 4.2 (and 4.0.13 and 3.6.14 ), if a replica set member uses the in-memory storage engine (voting or non-voting) but the replica set has writeConcernMajorityJournalDefault set to true, the replica set member logs a startup warning.

You cannot run transactions on a sharded cluster that has a shard with writeConcernMajorityJournalDefault set to false (such as a shard with a voting member that uses the in-memory storage engine).

members

类型:数组

An array of member configuration documents, one for each member of the replica set. The members array is a zero-indexed array.

每份节点专用配置文档可包含以下字段:

members[n]._id

类型:整型

副本集中节点的整数标识符,在所有节点中具有唯一性。

从 MongoDB 5.0 开始,该值可以是大于或等于 0 的任何整数值。以前,该值仅限于 0255(含)之间的整数。

Each replica set member must have a unique _id. Avoid re-using _id values even if no members[n] entry is using that _id in the current configuration.

Once set, you cannot change the _id of a member.

注意

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.

members[n].host

类型:字符串

主主机名以及副本集节点的端口号(如果端口号经过设置)。

副本集中每个托管的托管名称必须可解析。

警告

members[n].host cannot hold a value that resolves to localhost or the local interface unless all members of the set are on hosts that resolve to localhost.

members[n].arbiterOnly

可选

类型:布尔值

默认:false

标识仲裁节点的布尔值。值为 true 表示该节点是仲裁节点。

When using the rs.addArb() method to add an arbiter, the method automatically sets members[n].arbiterOnly to true for the added member.

members[n].buildIndexes

可选

类型:布尔值

默认值:true

A boolean that indicates whether the mongod builds indexes on this member. You can only set this value when adding a member to a replica set. You cannot change members[n].buildIndexes field after the member has been added to the set. To add a member, see rs.add() and rs.reconfig().

对于接收来自客户端的查询的 mongod 实例,请勿将其设置为 false

如果以下所有条件为 true,则将buildIndexes设置为 false可能会很有用:

  • you are only using this instance to perform backups using mongodump, and

  • 此成员将不会收到任何查询,

  • 索引创建和维护使主机系统负担过重。

即使设置为 false,从节点(secondary node from replica set)也会_id 字段构建索引,以便执行复制所需的操作。

警告

If you set members[n].buildIndexes to false, you must also set members[n].priority to 0. If members[n].priority is not 0, MongoDB will return an error when attempting to add a member with members[n].buildIndexes equal to false.

为确保成员不接收任何查询,应隐藏所有未构建索引的实例。

Other secondaries cannot replicate from a member where members[n].buildIndexes is false.

members[n].hidden

可选

类型:布尔值

默认:false

当该值为 true 时,副本集会隐藏该实例,不会将该节点包含在 db.hello()hello 的输出中。这可防止读取操作(即查询)通过从节点读取偏好到达此主机。

Hidden members can acknowledge write operations issued with Write Concern. For write operations issued with "majority" write concern, the member must also be a voting member (that is, votes is greater than 0).

members[n].priority

可选

类型:主节点/从节点的数字在 0 到 1000 之间,或仲裁节点的数字为 0 或 1。

默认:主节点/从节点为 1.0,仲裁节点为 0。

一个数字,表示副本集节点成为主节点的相对可能性。

  • 要提高某个成员成为从节点的可能性,请为该成员指定较高的 priority 值。

  • 要降低某个节点成为主节点的可能性,请为该节点指定一个较低的 priority 值。

更改节点的优先级会触发一次或多次选举。选举算法尽最大努力将优先级最高的节点选为主节点。然而,即使有较高优先级的从节点可用,较低优先级的节点也可能成为主节点。

如果优先级较低的节点成为节点,服务器将继续定期进行选举,直到优先级最高的副本集节点成为主节点。选举频率取决于当选节点和最高优先级节点之间的优先级差异。

优先级为 0 的成员无法成为主节点。

Non-voting members (meaning members that have votes set to 0) must have a priority of 0.

members[n].tags

可选

类型:文档

默认:无

一份 tags 文档包含用户定义的副本集节点标签字段和值对。

{ "<tag1>": "<string1>", "<tag2>": "<string2>",... }

有关更多信息,请参阅配置副本集标签集。

members[n].secondaryDelaySecs

可选

类型:整型

默认值:0

该副本集节点应“滞后”于主节点的秒数。

使用此选项创建延迟节点。延迟节点维护反映过去某个时间点数据状态的数据副本。

Delayed members can contribute to acknowledging write operations issued with Write Concern. However, they return write acknowledgment no earlier than the configured delay value. For write operations issued with "majority" write concern, the member must also be a voting member (that is, votes is greater than 0).

members[n].votes

可选

类型:整型

默认值:1

服务器在副本集选举中将投的票数。每个节点拥有的票数为 10仲裁节点始终恰好拥有 1 票。

Members with priority greater than 0 cannot have 0 votes.

A replica set can have up to 50 members but only 7 voting members. If you need more than 7 members in one replica set, set members[n].votes to 0 for the additional non-voting members.

Non-voting (i.e. votes is 0) members must have priority of 0.

从 MongoDB 5.0 开始,新添加的从节点不计为投票节点,并且在达到 SECONDARY 状态之前无法当选。

无投票权的成员无法确认带有"majority"写关注的写入操作。

settings

可选

类型:文档

一份包含应用于整个副本集的配置选项的文档。

The settings document contains the following fields:

settings.chainingAllowed

可选

类型:布尔值

默认值:true

In MongoDB 5.0.1 and earlier, if settings.chainingAllowed is:

  • true副本集的从节点可以从其他从节点复制数据。

  • false,从节点只能从主节点复制数据。

从 MongoDB 5.0.2 开始:

settings.getLastErrorDefaults

可选

类型:文档

(从 MongoDB 5.0 开始提供)

重要

Starting in MongoDB 5.0, you cannot specify a default write concern with settings.getLastErrorDefaults other than the default of { w: 1, wtimeout: 0 } . Instead, use the setDefaultRWConcern command to set the default read or write concern configuration for a replica set or sharded cluster.

settings.getLastErrorModes

可选

类型:文档

A document used to define a custom write concern through the use of members[n].tags. The custom write concern can provide data-center awareness.

{ getLastErrorModes: {
<name of write concern> : { <tag1>: <number>, .... },
...
} }

The <number> refers to the number of different tag values required to satisfy the write concern. For example, the following settings.getLastErrorModes defines a write concern named datacenter that requires the write to propagate to two members whose dc tag values differ.

{ getLastErrorModes: { datacenter: { "dc": 2 } } }

要使用自定义写关注(write concern),请将写关注名称传递给 w 选项,示例:

{ w: "datacenter" }

有关更多信息和示例,请参阅配置副本集标记集

settings.heartbeatTimeoutSecs

可选

类型:int

默认值:10

副本集节点等待彼此成功心跳的秒数。如果某个节点没有及时回复,其他节点会将节点标记为不可访问。

settings.electionTimeoutMillis

可选

类型:int

默认值:10000(10 秒)

The time limit in milliseconds for detecting when a replica set's primary is unreachable. This setting controls failover sensitivity when using protocolVersion: 1. You can expect the failover timeout to not exceed the value of electionTimeoutMillis.

选择值时,请考虑以下事项:

  • 较高的值会导致故障转移速度较慢,但会减少对主节点或网络缓慢或不稳定的敏感度。

  • 较低的值会加快故障转移,但会增加对主节点或网络缓慢或不稳定的敏感度。

The setting only applies when using protocolVersion: 1.

注意

当您使用rs.stepDown()replSetStepDown退出主节点而未将force字段设置为true时,退出的主节点会指定一个符合条件的从节点来立即进行选举。

settings.catchUpTimeoutMillis

可选

类型:int

默认:-1,无限追赶时间。

新选出的主节点与可能有更多近期写入操作的其他副本集成员进行同步(赶上)的时间限制(以毫秒为单位)。无限或较高的时间限制可能会减少其他成员在选举后需要回滚的数据量,但可能会延长故障转移时间。

新当选的主节点一旦被其他节点完全赶上,则会提前结束追赶期。在追赶期间,新当选的主节点不可用于客户端的写入操作。使用 replSetAbortPrimaryCatchUp 中止追赶,然后完成向主节点的过渡。

The setting only applies when using protocolVersion: 1.

settings.catchUpTakeoverDelayMillis

可选

类型:int

默认:30000(30 秒)

某节点在确定其领先于当前主节点后,等待启动追赶接管的时间(以毫秒为单位)。在追赶接管期间,当前主节点之前的节点发起选举以成为副本集的新主节点。

启动接管的节点确定其领先于当前主节点后,会等待指定的毫秒数,然后验证:

  1. 它仍然领先于当前主节点,

  2. 它是所有可用节点中最新的节点,

  3. 当前主节点正在追赶它。

一旦确定所有这些条件都得到满足,启动接管的节点将立即竞选。

有关副本集选举的更多信息,请参阅副本集选举。

注意

Setting catchUpTakeoverDelayMillis to -1 disables catchup takeover. Setting catchUpTimeoutMillis to 0 disables primary catchup and consequently also catchup takeover.

settings.heartbeatIntervalMillis

仅供内部使用

心跳频率(以毫秒为单位)。

settings.replicaSetId

类型:对象标识符

The ObjectId associated with the replica set and automatically created during rs.initiate() or replSetInitiate. You cannot change the replicaSetId.