This reference provides an overview of replica set configuration options and settings.
Use rs.conf() in the mongo shell to retrieve this configuration. Note that default values are not explicitly displayed.
类型:字符串
值:<setname>
An _id field holding the name of the replica set. This reflects the set name configured with replSet or mongod --replSet.
类型:数组
Contains an array holding an embedded document for each member of the replica set. The members document contains a number of fields that describe the configuration of each member of the replica set.
The members field in the replica set configuration document is a zero-indexed array.
类型:序数
Provides the zero-indexed identifier of every member in the replica set.
注意
When updating the replica configuration object, address all members of the set using the index value in the array. The array index begins with 0. Do not confuse this index value with the value of the _id field in each document in the members array.
The _id rarely corresponds to the array index.
Type: <hostname><:port>
Identifies the host name of the set member with a hostname and port number. This name must be resolvable for every host in the replica set.
警告
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.
可选。
类型:布尔值
默认值:false
Identifies an arbiter. For arbiters, this value is true, and is automatically configured by rs.addArb()”.
可选。
类型:布尔值
默认值:true
Determines whether the mongod builds indexes on this member. Do not set to false if a replica set can become a primary, or if any clients ever issue queries against this instance.
Omitting index creation, and thus this setting, may be useful, if:
If set to false, secondaries configured with this option do build indexes on the _id field, to facilitate operations required for replication.
警告
You may only set this value when adding a member to a replica set. You may not reconfigure a replica set to change the value of the buildIndexes field after adding the member to the set.
Other secondaries cannot replicate from a members where buildIndexes is false.
可选。
类型:布尔值
默认值:false
When this value is true, the replica set hides this instance, and does not include the member in the output of db.isMaster() or isMaster. This prevents read operations (i.e. queries) from ever reaching this host by way of secondary read preference.
可选。
Type: Number, between 0 and 100.0 including decimals.
默认值:1
Specify higher values to make a member more eligible to become primary, and lower values to make the member less eligible to become primary. Priorities are only used in comparison to each other. Members of the set will veto election requests from members when another eligible member has a higher priority value. Changing the balance of priority in a replica set will trigger an election.
A priority of 0 makes it impossible for a member to become primary.
另见
可选。
类型: MongoDB 文档
默认值:无
Used to represent arbitrary values for describing or tagging members for the purposes of extending write concern to allow configurable data center awareness.
Use in conjunction with getLastErrorModes and getLastErrorDefaults and db.getLastError() (i.e. getLastError.)
For procedures on configuring tag sets, see Configure Replica Set Tag Sets.
可选。
类型:整数(秒数)
默认值:0
Describes the number of seconds “behind” the primary that this replica set member should “lag.” Use this option to create delayed members, that maintain a copy of the data that reflects the state of the data set at some amount of time in the past, specified in seconds. Typically such delayed members help protect against human error, and provide some measure of insurance against the unforeseen consequences of changes and updates.
可选。
类型:整数
默认值:1
Controls the number of votes a server will cast in a replica set election. The number of votes each member has can be any non-negative integer, but it is highly recommended each member has 1 or 0 votes.
If you need more than 7 members in one replica set, use this setting to add additional non-voting members with a votes value of 0.
For most deployments and most members, use the default value, 1, for votes.
可选。
类型: MongoDB 文档
The settings document configures options that apply to the whole replica set.
可选。
类型:布尔值
默认值:true
New in version 2.2.2.
When chainingAllowed is true, the replica set allows secondary members to replicate from other secondary members. When chainingAllowed is false, secondaries can replicate only from the primary.
When you run rs.config() to view a replica set’s configuration, the chainingAllowed field appears only when set to false. If not set, chainingAllowed is true.
可选。
类型: MongoDB 文档
Specify arguments to the getLastError that members of this replica set will use when no arguments to getLastError has no arguments. If you specify any arguments, getLastError , ignores these defaults.
可选。
类型: MongoDB 文档
Defines the names and combination of members for use by the application layer to guarantee write concern to database using the getLastError command to provide data-center awareness.
The following document provides a representation of a replica set configuration document. Angle brackets (e.g. < and >) enclose all optional fields.
{
_id : <setname>,
version: <int>,
members: [
{
_id : <ordinal>,
host : hostname<:port>,
<arbiterOnly : <boolean>,>
<buildIndexes : <boolean>,>
<hidden : <boolean>,>
<priority: <priority>,>
<tags: { <document> },>
<slaveDelay : <number>,>
<votes : <number>>
}
, ...
],
<settings: {
<getLastErrorDefaults : <lasterrdefaults>,>
<chainingAllowed : <boolean>,>
<getLastErrorModes : <modes>>
}>
}
Most modifications of replica set configuration use the mongo shell. Consider the following reconfiguration operation:
示例
指定以下副本集配置:
{
"_id" : "rs0",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "mongodb0.example.net:27017"
},
{
"_id" : 1,
"host" : "mongodb1.example.net:27017"
},
{
"_id" : 2,
"host" : "mongodb2.example.net:27017"
}
]
}
以及下面的重新配置操作:
cfg = rs.conf()
cfg.members[0].priority = 0.5
cfg.members[1].priority = 2
cfg.members[2].priority = 2
rs.reconfig(cfg)
This operation begins by saving the current replica set configuration to the local variable cfg using the rs.conf() method. Then it adds priority values to the cfg document where for the first three sub-documents in the members array. Finally, it calls the rs.reconfig() method with the argument of cfg to initialize this new configuration. The replica set configuration after this operation will resemble the following:
{
"_id" : "rs0",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "mongodb0.example.net:27017",
"priority" : 0.5
},
{
"_id" : 1,
"host" : "mongodb1.example.net:27017",
"priority" : 2
},
{
"_id" : 2,
"host" : "mongodb2.example.net:27017",
"priority" : 1
}
]
}
Using the “dot notation” demonstrated in the above example, you can modify any existing setting or specify any of optional replica set configuration variables. Until you run rs.reconfig(cfg) at the shell, no changes will take effect. You can issue cfg = rs.conf() at any time before using rs.reconfig() to undo your changes and start from the current configuration. If you issue cfg as an operation at any point, the mongo shell at any point will output the complete document with modifications for your review.
The rs.reconfig() operation has a “force” option, to make it possible to reconfigure a replica set if a majority of the replica set is not visible, and there is no primary member of the set. use the following form:
rs.reconfig(cfg, { force: true } )
警告
Forcing a rs.reconfig() can lead to rollback situations and other difficult to recover from situations. Exercise caution when using this option.
注意
The rs.reconfig() shell method can force the current primary to step down and triggers an election in some situations. When the primary steps down, all clients will disconnect. This is by design. Since this typically takes 10-20 seconds, attempt to make such changes during scheduled maintenance periods.