您可以使用 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必须与命令行上指定给mongod的replication.replSetName或--replSet的值相同。提示
replSetName或--replSet,了解如何设置副本集名称。
version类型:int
一个递增的数字,区分副本集配置文档的修订版本和配置的先前迭代。
Replica set members use
termandversionto achieve consensus on the "newest" replica configuration. When members compare replica configuration documents, the configuration document with a largertermis considered the "newest". Iftermis the same or absent, the configuration document with the largerversionis 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
termof 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 thetermfield if set explicitly in thereplSetReconfigoperation.Issuing a force reconfiguration removes the
termfield. When the primary next issuesreplSetReconfigwithout force, it sets thetermto its own term.Replica set members use
termandversionto achieve consensus on the "newest" replica configuration. When members compare replica configuration documents, the configuration document with a largertermis considered the "newest". Iftermis the same or absent, the configuration document with the largerversionis considered "newest".
writeConcernMajorityJournalDefault类型:布尔值
默认值:true
如果
{ w: "majority" }写关注未显式指定日志选项 j,则确定该写关注的行为。下表列出了
writeConcernMajorityJournalDefault值和关联的{ w: "majority" }行为:值{ w: "majority" }行为true
当大多数投票节点写入磁盘日志后,MongoDB 会确认该写入操作。
重要提示:当
writeConcernMajorityJournalDefault为true时,副本集的所有投票节点都必须使用日志记录功能运行。If any voting member of a replica set uses the in-memory storage engine, you must set
writeConcernMajorityJournalDefaulttofalse.If any voting member of a replica set uses the in-memory storage engine and
writeConcernMajorityJournalDefaultistrue,"majority"write operations may fail. These include operations that inherently use"majority"write concern, such as thereplSetStepDowncommand, or variousmongoshmethods 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
writeConcernMajorityJournalDefaultset 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
writeConcernMajorityJournalDefaulttofalse.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
writeConcernMajorityJournalDefaultset to true, the replica set member logs a startup warning.You cannot run transactions on a sharded cluster that has a shard with
writeConcernMajorityJournalDefaultset tofalse(such as a shard with a voting member that uses the in-memory storage engine).提示
members
members类型:数组
An array of member configuration documents, one for each member of the replica set. The
membersarray is a zero-indexed array.每份节点专用配置文档可包含以下字段:
members[n]._id类型:整型
副本集中节点的整数标识符,在所有节点中具有唯一性。
从 MongoDB 5.0 开始,该值可以是大于或等于
0的任何整数值。以前,该值仅限于0和255(含)之间的整数。Each replica set member must have a unique
_id. Avoid re-using_idvalues even if nomembers[n]entry is using that_idin the current configuration.Once set, you cannot change the
_idof a member.注意
When updating the replica configuration object, access the replica set members in the
membersarray with the array index. The array index begins with0. Do not confuse this index value with the value of themembers[n]._idfield in each document in themembersarray.
members[n].host类型:字符串
主主机名以及副本集节点的端口号(如果端口号经过设置)。
副本集中每个托管的托管名称必须可解析。
警告
members[n].hostcannot hold a value that resolves tolocalhostor the local interface unless all members of the set are on hosts that resolve tolocalhost.
members[n].arbiterOnly可选。
类型:布尔值
默认:false
标识仲裁节点的布尔值。值为
true表示该节点是仲裁节点。When using the
rs.addArb()method to add an arbiter, the method automatically setsmembers[n].arbiterOnlytotruefor the added member.
members[n].buildIndexes可选。
类型:布尔值
默认值:true
A boolean that indicates whether the
mongodbuilds indexes on this member. You can only set this value when adding a member to a replica set. You cannot changemembers[n].buildIndexesfield after the member has been added to the set. To add a member, seers.add()andrs.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].buildIndexestofalse, you must also setmembers[n].priorityto0. Ifmembers[n].priorityis not0, MongoDB will return an error when attempting to add a member withmembers[n].buildIndexesequal tofalse.为确保成员不接收任何查询,应隐藏所有未构建索引的实例。
Other secondaries cannot replicate from a member where
members[n].buildIndexesis 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,votesis greater than0).提示
members[n].priority可选。
类型:主节点/从节点的数字在 0 到 1000 之间,或仲裁节点的数字为 0 或 1。
默认:主节点/从节点为 1.0,仲裁节点为 0。
一个数字,表示副本集节点成为主节点的相对可能性。
要提高某个成员成为从节点的可能性,请为该成员指定较高的
priority值。要降低某个节点成为主节点的可能性,请为该节点指定一个较低的
priority值。
更改节点的优先级会触发一次或多次选举。选举算法尽最大努力将优先级最高的节点选为主节点。然而,即使有较高优先级的从节点可用,较低优先级的节点也可能成为主节点。
如果优先级较低的节点成为节点,服务器将继续定期进行选举,直到优先级最高的副本集节点成为主节点。选举频率取决于当选节点和最高优先级节点之间的优先级差异。
优先级为
0的成员无法成为主节点。Non-voting members (meaning members that have
votesset to0) must have a priority of0.
members[n].tags可选。
类型:文档
默认:无
一份
tags文档包含用户定义的副本集节点标签字段和值对。{ "<tag1>": "<string1>", "<tag2>": "<string2>",... } 对于读取操作,您可以在读取偏好中指定标签集,以将操作定向到具有指定标签的副本集节点员。
For write operations, you can create a customize write concern using
settings.getLastErrorModesandsettings.getLastErrorDefaults.
有关更多信息,请参阅配置副本集标签集。
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,votesis greater than0).提示
members[n].votes可选。
类型:整型
默认值:1
服务器在副本集选举中将投的票数。每个节点拥有的票数为
1或0,仲裁节点始终恰好拥有1票。Members with
prioritygreater than 0 cannot have 0votes.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].votesto0for the additional non-voting members.Non-voting (i.e.
votesis0) members must havepriorityof 0.从 MongoDB 5.0 开始,新添加的从节点不计为投票节点,并且在达到
SECONDARY状态之前无法当选。无投票权的成员无法确认带有
"majority"写关注的写入操作。
settings
settings可选。
类型:文档
一份包含应用于整个副本集的配置选项的文档。
The
settingsdocument contains the following fields:settings.chainingAllowed可选。
类型:布尔值
默认值:true
In MongoDB 5.0.1 and earlier, if
settings.chainingAllowedis:从 MongoDB 5.0.2 开始:
With an override enabled, replica set secondary members can replicate data from other secondary members even if
settings.chainingAllowedisfalse.To override
settings.chainingAllowedand allow replication from a secondary member, set theenableOverrideClusterChainingSettingserver parameter totrue.enableOverrideClusterChainingSetting的默认值为false。
提示
settings.getLastErrorDefaults可选。
类型:文档
(从 MongoDB 5.0 开始提供)
重要
Starting in MongoDB 5.0, you cannot specify a default write concern with
settings.getLastErrorDefaultsother than the default of{ w: 1, wtimeout: 0 }. Instead, use thesetDefaultRWConcerncommand 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 followingsettings.getLastErrorModesdefines a write concern nameddatacenterthat requires the write to propagate to two members whosedctag values differ.{ getLastErrorModes: { datacenter: { "dc": 2 } } } 要使用自定义写关注(write concern),请将写关注名称传递给
w选项,示例:{ w: "datacenter" } 有关更多信息和示例,请参阅配置副本集标记集。
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 ofelectionTimeoutMillis.选择值时,请考虑以下事项:
较高的值会导致故障转移速度较慢,但会减少对主节点或网络缓慢或不稳定的敏感度。
较低的值会加快故障转移,但会增加对主节点或网络缓慢或不稳定的敏感度。
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 秒)
某节点在确定其领先于当前主节点后,等待启动追赶接管的时间(以毫秒为单位)。在追赶接管期间,当前主节点之前的节点发起选举以成为副本集的新主节点。
启动接管的节点确定其领先于当前主节点后,会等待指定的毫秒数,然后验证:
它仍然领先于当前主节点,
它是所有可用节点中最新的节点,
当前主节点正在追赶它。
一旦确定所有这些条件都得到满足,启动接管的节点将立即竞选。
有关副本集选举的更多信息,请参阅副本集选举。
注意
Setting
catchUpTakeoverDelayMillisto-1disables catchup takeover. SettingcatchUpTimeoutMillisto0disables primary catchup and consequently also catchup takeover.
settings.replicaSetId类型:对象标识符
The ObjectId associated with the replica set and automatically created during
rs.initiate()orreplSetInitiate. You cannot change thereplicaSetId.