发布于 2015-09-14 14:57:08 | 133 次阅读 | 评论: 0 | 来源: 网络整理
This reference collects documentation for all JavaScript methods for the mongo shell that support replica set functionality, as well as all database commands related to replication function.
See 副本集, for a list of all replica set documentation.
The following methods apply to replica sets. For a complete list of all methods, see JavaScript 方法.
返回: | A document with status information. |
---|
This output reflects the current status of the replica set, using data derived from the heartbeat packets sent by the other members of the replica set.
This method provides a wrapper around the replSetGetStatus database command.
也可以参考
“副本集状态参考” for documentation of this output.
Returns a status document with fields that includes the ismaster field that reports if the current node is the primary node, as well as a report of a subset of current replica set configuration.
This function provides a wrapper around the database command isMaster
参数: |
|
---|
Initiates a replica set. Optionally takes a configuration argument in the form of a document that holds the configuration of a replica set. Consider the following model of the most basic configuration for a 3-member replica set:
{
_id : <setname>,
members : [
{_id : 0, host : <host0>},
{_id : 1, host : <host1>},
{_id : 2, host : <host2>},
]
}
This function provides a wrapper around the “replSetInitiate” database command.
返回: | a document that contains the current replica set configuration object. |
---|
rs.config() is an alias of rs.conf().
参数: |
|
---|
Initializes a new replica set configuration. This function will disconnect the shell briefly and forces a reconnection as the replica set renegotiates which node will be primary. As a result, the shell will display an error even if this command succeeds.
rs.reconfig() provides a wrapper around the “replSetReconfig” database command.
rs.reconfig() overwrites the existing replica set configuration. Retrieve the current configuration object with rs.conf(), modify the configuration as needed and then use rs.reconfig() to submit the modified configuration object.
To reconfigure a replica set, use the following sequence of operations:
conf = rs.conf()
// modify conf to change configuration
rs.reconfig(conf)
If you want to force the reconfiguration if a majority of the set isn’t connected to the current member, or you’re issuing the command against a secondary, use the following form:
conf = rs.conf()
// modify conf to change configuration
rs.reconfig(conf, { force: true } )
警告
Forcing a rs.reconfig() can lead to rollback situations and other difficult to recover from situations. Exercise caution when using this option.
Specify one of the following forms:
参数: |
|
---|
Provides a simple method to add a member to an existing replica set. You can specify new hosts in one of two ways:
This function will disconnect the shell briefly and forces a reconnection as the replica set renegotiates which node will be primary. As a result, the shell will display an error even if this command succeeds.
rs.add() provides a wrapper around some of the functionality of the “replSetReconfig” database command and the corresponding shell helper rs.reconfig(). See the 副本集配置 document for full documentation of all replica set configuration options.
Example
To add a mongod accessible on the default port 27017 running on the host mongodb3.example.net, use the following rs.add() invocation:
rs.add('mongodb3.example.net:27017')
If mongodb3.example.net is an arbiter, use the following form:
rs.add('mongodb3.example.net:27017', true)
To add mongodb3.example.net as a secondary-only member of set, use the following form of rs.add():
rs.add( { "host": "mongodbd3.example.net:27017", "priority": 0 } )
参数: |
|
---|
Adds a new arbiter to an existing replica set.
This function will disconnect the shell briefly and forces a reconnection as the replica set renegotiates which node will be primary. As a result, the shell will display an error even if this command succeeds.
参数: |
|
---|---|
返回: | disconnects shell. |
Forces the current replica set member to step down as primary and then attempt to avoid election as primary for the designated number of seconds. Produces an error if the current node is not primary.
This function will disconnect the shell briefly and forces a reconnection as the replica set renegotiates which node will be primary. As a result, the shell will display an error even if this command succeeds.
rs.stepDown() provides a wrapper around the database command replSetStepDown.
参数: |
|
---|
Forces the current node to become ineligible to become primary for the period specified.
rs.freeze() provides a wrapper around the database command replSetFreeze.
参数: |
|
---|
Removes the node described by the hostname parameter from the current replica set. This function will disconnect the shell briefly and forces a reconnection as the replica set renegotiates negotiates which node will be primary. As a result, the shell will display an error even if this command succeeds.
注解
Before running the rs.remove() operation, you must shut down the replica set member that you’re removing.
在 2.2 版更改: This procedure is no longer required when using rs.remove(), but it remains good practice.
Provides a shorthand for the following operation:
db.getMongo().setSlaveOk()
This allows the current connection to allow read operations to run on secondary nodes. See the readPref() method for more fine-grained control over read preference in the mongo shell.
Returns a status document with fields that includes the ismaster field that reports if the current node is the primary node, as well as a report of a subset of current replica set configuration.
This function provides a wrapper around the database command isMaster
Returns a basic help text for all of the replication related shell functions.
2.2 新版功能.
Provides a wrapper around the replSetSyncFrom, which allows administrators to configure the member of a replica set that the current member will pull data from. Specify the name of the member you want to replicate from in the form of [hostname]:[port].
See replSetSyncFrom for more details.
The following commands apply to replica sets. For a complete list of all commands, see 数据库命令快参.
The isMaster command provides a basic overview of the current replication configuration. MongoDB drivers and clients use this command to determine what kind of member they’re connected to and to discover additional members of a replica set. The db.isMaster() method provides a wrapper around this database command.
The command takes the following form:
{ isMaster: 1 }
This command returns a document containing the following fields:
The name of the current replica set, if applicable.
A boolean value that reports when this node is writable. If true, then the current node is either a primary node in a replica set, a master node in a master-slave configuration, of a standalone mongod.
A boolean value that, when true, indicates that the current node is a secondary member of a replica set.
An array of strings in the format of “[hostname]:[port]” listing all nodes in the replica set that are not “hidden”.
The [hostname]:[port] for the current replica set primary, if applicable.
The [hostname]:[port] of the node responding to this command.
The resync command forces an out-of-date slave mongod instance to re-synchronize itself. Note that this command is relevant to master-slave replication only. It does not apply to replica sets.
警告
This command obtains a global write lock and will block other operations until it has completed.
The replSetFreeze command prevents a replica set member from seeking election for the specified number of seconds. Use this command in conjunction with the replSetStepDown command to make a different node in the replica set a primary.
The replSetFreeze command uses the following syntax:
{ replSetFreeze: <seconds> }
If you want to unfreeze a replica set member before the specified number of seconds has elapsed, you can issue the command with a seconds value of 0:
{ replSetFreeze: 0 }
Restarting the mongod process also unfreezes a replica set member.
replSetFreeze is an administrative command, and you must issue the it against the admin database.
The replSetGetStatus command returns the status of the replica set from the point of view of the current server. You must run the command against the admin database. The command has the following prototype format:
{ replSetGetStatus: 1 }
However, you can also run this command from the shell like so:
rs.status()
The replSetInitiate command initializes a new replica set. Use the following syntax:
{ replSetInitiate : <config_document> }
The <config_document> is a document that specifies the replica set’s configuration. For instance, here’s a config document for creating a simple 3-member replica set:
{
_id : <setname>,
members : [
{_id : 0, host : <host0>},
{_id : 1, host : <host1>},
{_id : 2, host : <host2>},
]
}
A typical way of running this command is to assign the config document to a variable and then to pass the document to the rs.initiate() helper:
config = {
_id : "my_replica_set",
members : [
{_id : 0, host : "rs1.example.net:27017"},
{_id : 1, host : "rs2.example.net:27017"},
{_id : 2, host : "rs3.example.net", arbiterOnly: true},
]
}
rs.initiate(config)
Notice that omitting the port cause the host to use the default port
of 27017. Notice also that you can specify other options in the config
documents such as the ``arbiterOnly`` setting in this example.
也可以参考
“副本集配置,” “副本集的操作和管理,” and “Replica Set Reconfiguration.”
The replSetMaintenance admin command enables or disables the maintenance mode for a secondary member of a replica set.
The command has the following prototype form:
{ replSetMaintenance: <boolean> }
Consider the following behavior when running the replSetMaintenance command:
The replSetReconfig command modifies the configuration of an existing replica set. You can use this command to add and remove members, and to alter the options set on existing members. Use the following syntax:
{ replSetReconfig: <new_config_document>, force: false }
You may also run the command using the shell’s rs.reconfig() method.
Be aware of the following replSetReconfig behaviors:
You must issue this command against the admin database of the current primary member of the replica set.
You can optionally force the replica set to accept the new configuration by specifying force: true. Use this option if the current member is not primary or if a majority of the members of the set are not accessible.
警告
Forcing the replSetReconfig command can lead to a rollback situation. Use with caution.
Use the force option to restore a replica set to new servers with different hostnames. This works even if the set members already have a copy of the data.
A majority of the set’s members must be operational for the changes to propagate properly.
This command can cause downtime as the set renegotiates primary-status. Typically this is 10-20 seconds, but could be as long as a minute or more. Therefore, you should attempt to reconfigure only during scheduled maintenance periods.
In some cases, replSetReconfig forces the current primary to step down, initiating an election for primary among the members of the replica set. When this happens, the set will drop all current connections.
注解
replSetReconfig obtains a special mutually exclusive lock to prevent more than one :dbcommand`replSetReconfig` operation from occurring at the same time.
2.2 新版功能.
Options: |
|
---|
replSetSyncFrom allows you to explicitly configure which host the current mongod will poll oplog entries from. This operation may be useful for testing different patterns and in situations where a set member is not replicating from the host you want. The member to replicate from must be a valid source for data in the set.
A member cannot replicate from:
If you attempt to replicate from a member that is more than 10 seconds behind the current member, mongod will return and log a warning, but it still will replicate from the member that is behind.
If you run rs.syncFrom() during initial sync, MongoDB produces no error messages, but the sync target will not change until after the initial sync operation.
The command has the following prototype form:
{ replSetSyncFrom: "[hostname]:[port]" }
To run the command in the mongo shell, use the following invocation:
db.adminCommand( { replSetSyncFrom: "[hostname]:[port]" } )
You may also use the rs.syncFrom() helper in the mongo shell, in an operation with the following form:
rs.syncFrom("[hostname]:[port]")
注解
replSetSyncFrom and rs.syncFrom() provide a temporary override of default behavior. If:
the mongod instance restarts,
the connection to the sync target closes, or
在 2.4 版更改: The sync target falls more than 30 seconds behind another member of the replica set;
then, the mongod instant will revert to the default sync logic and target.