翻译或纠错本页面

Replica Set Configuration

You can access the configuration of a replica set using the rs.conf() method or the replSetGetConfig command.

To modify the configuration for a replica set, use the rs.reconfig() method, passing a configuration document to the method. See rs.reconfig() for more information.

Replica Set Configuration Document Example

The following document provides a representation of a replica set configuration document. The configuration of your replica set may include only a subset of these settings:

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

Replica Set Configuration Fields

_id

Type: string

The name of the replica set. Once set, you cannot change the name of a replica set.

_id must be identical to the replication.replSetName or the value of –replSet specified to mongod on the command line.

See

replSetName or --replSet for information on setting the replica set name.

version

Type: int

An incrementing number used to distinguish revisions of the replica set configuration object from previous iterations of the configuration.

configsvr

3.2 新版功能.

Type: boolean

Default: false

Indicates whether the replica set is used for a sharded cluster’s config servers. Set to true if the replica set is for a sharded cluster’s config servers.

protocolVersion

3.2 新版功能.

Type: number

Default: 1 for new replica sets

Version of the replica set election protocol.

By default, new replica sets in MongoDB 3.2 use protocolVersion: 1. Previous versions of MongoDB use version 0 of the protocol and cannot run as members of a replica set configuration that specifies protocolVersion 1.

writeConcernMajorityJournalDefault

3.4 新版功能.

Type: boolean

Default: true if protocolVersion is 1 and false if protocolVersion is 0

Determines the behavior of { w: "majority" } write concern if the write concern does not explicitly specify the journal option j.

The following table lists the writeConcernMajorityJournalDefault values and the associated { w: "majority" } behavior:

Value { w: "majority" } Behavior
true

MongoDB acknowledges the write operation after a majority of the voting members have written to the on-disk journal.

重要

All voting members of the replica set must run with journaling when writeConcernMajorityJournalDefault is true.

If any voting member of a replica set runs without journaling 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 mongo shell methods that by default use "majority" write concern, such as user management methods and role management methods.

false

MongoDB acknowledges the write operation after a majority of the voting members have applied the operation in memory.

警告

If any voting member of a replica set runs without journaling (i.e. either runs an in-memory storage engine or runs with journaling disabled), you must set writeConcernMajorityJournalDefault to false.

members

members

Type: array

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

Each member-specific configuration document can contain the following fields:

members[n]._id

Type: integer

An integer identifier of every member in the replica set. Values must be between 0 and 255 inclusive. Each replica set member must have a unique _id<members[n]._id>. 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

Type: string

The hostname and, if specified, the port number, of the set member.

The hostname name must be resolvable for every host in the replica set.

警告

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

Optional.

Type: boolean

Default: false

A boolean that identifies an arbiter. A value of true indicates that the member is an arbiter.

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

Optional.

Type: boolean

Default: 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().

Do not set to false for mongod instances that receive queries from clients.

Setting buildIndexes to false may be useful if all the following conditions are true:

  • you are only using this instance to perform backups using mongodump, and
  • this member will receive no queries, and
  • index creation and maintenance overburdens the host system.

Even if set to false, secondaries will build indexes on the _id field in order to facilitate operations required for replication.

警告

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.

To ensure the member receives no queries, you should make all instances that do not build indexes hidden.

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

members[n].hidden

Optional.

Type: boolean

Default: 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.

members[n].priority

Optional.

Type: Number, between 0 and 1000.

Default: 1.0

A number that indicates the relative eligibility of a member to become a primary.

Specify higher values to make a member more eligible to become primary, and lower values to make the member less eligible. A member with a members[n].priority of 0 is ineligible to become primary.

Changing the balance of priority in a replica set will trigger one or more elections. If a lower priority secondary is elected over a higher priority secondary, replica set members will continue to call elections until the highest priority available member becomes primary.

members[n].tags

Optional.

Type: document

Default: none

A tag set document containing mappings of arbitrary keys and values. These documents describe replica set members in order to customize write concern and read preference and thereby allow configurable data center awareness.

This field is only present if there are tags assigned to the member. See 配置复制集标签设置 for more information.

Use replicaset.members[n].tags to configure write concerns in conjunction with settings.getLastErrorModes and settings.getLastErrorDefaults.

重要

In tag sets, all tag values must be strings.

For more information on configuring tag sets for read preference and write concern, see 配置复制集标签设置.

members[n].slaveDelay

Optional.

Type: integer

Default: 0

The number of seconds “behind” the primary that this replica set member should “lag”.

Use this option to create delayed members. Delayed members maintain a copy of the data that reflects the state of the data at some time in the past.

参见

延时节点

members[n].votes

Optional.

Type: integer

Default: 1

The number of votes a server will cast in a replica set election. The number of votes each member has is either 1 or 0, and arbiters always have exactly 1 vote.

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.

在 3.0.0 版更改: Members cannot have members[n].votes greater than 1. For details, see Replica Set Configuration Validation.

settings

settings

Optional.

Type: document

A document that contains configuration options that apply to the whole replica set.

The settings document contain the following fields:

settings.chainingAllowed

Optional.

Type: boolean

Default: true

When settings.chainingAllowed is true, the replica set allows secondary members to replicate from other secondary members. When settings.chainingAllowed is false, secondaries can replicate only from the primary.

settings.getLastErrorDefaults

Optional.

Type: document

A document that specifies the write concern for the replica set. The replica set will use this write concern only when write operations or getLastError specify no other write concern.

If settings.getLastErrorDefaults is not set, the default write concern for the replica set only requires confirmation from the primary.

settings.getLastErrorModes

Optional.

Type: document

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

For example, the following document defines an extended write concern named eastCoast and associates with a write to a member that has the east tag.

{ getLastErrorModes: { eastCoast: { "east": 1 } } }

Write operations to the replica set can use the extended write concern, e.g. { w: "eastCoast" }.

See 配置复制集标签设置 for more information and example.

settings.heartbeatTimeoutSecs

Optional.

Type: int

Default: 10

Number of seconds that the replica set members wait for a successful heartbeat from each other. If a member does not respond in time, other members mark the delinquent member as inaccessible.

The setting only applies when using protocolVersion: 0. When using protocolVersion: 1 the relevant setting is settings.electionTimeoutMillis.

settings.electionTimeoutMillis

3.2 新版功能.

Optional.

Type: int

Default: 10000 (10 seconds)

The time limit in milliseconds for detecting when a replica set’s primary is unreachable:

  • Higher values result in slower failovers but decreased sensitivity to primary node or network slowness or spottiness.
  • Lower values result in faster failover, but increased sensitivity to primary node or network slowness or spottiness.

The setting only applies when using protocolVersion: 1.

settings.catchUpTimeoutMillis

3.4 新版功能.

Optional.

Type: int

Default: 2000 (2 seconds)

Time limit in milliseconds for a newly elected primary to sync (catch up) with the other replica set members that may have more recent writes. Specifying a higher time limit may reduce the amount of data that the other members would need to roll back after an election but may increase the failover time.

The newly elected primary ends the catchup period early once it is fully caught up with other members of the set. During the catchup period, the newly elected primary is unavailable for writes from clients.

The setting only applies when using protocolVersion: 1.

settings.heartbeatIntervalMillis

3.2 新版功能.

Internal use only.

The frequency in milliseconds of the heartbeats.

settings.replicaSetId

3.2 新版功能.

Type: ObjectId

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