发布于 2015-09-14 14:57:49 | 204 次阅读 | 评论: 0 | 来源: 网络整理
This tutorial explains how to add an additional member to an existing replica set.
Before adding a new member, see the Adding Members topic in the 副本集的操作和管理 document.
For background on replication deployment patterns, see the 副本集的架构和部署模式 document.
If neither of these conditions are satisfied, please use the MongoDB installation tutorial and the 部署一个副本集 tutorial instead.
The examples in this procedure use the following configuration:
port = 27017
bind_ip = 10.8.0.10
dbpath = /srv/mongodb/
logpath = /var/log/mongodb.log
fork = true
replSet = rs0
For more information on configuration options, see 配置文件选项.
This procedure uses the above example configuration.
Deploy a new mongod instance, specifying the name of the replica set. You can do this one of two ways:
Using the mongodb.conf file. On the primary, issue a command that resembles the following:
mongod --config /etc/mongodb.conf
Using command line arguments. On the primary, issue command that resembles the following:
mongod --replSet rs0
Take note of the host name and port information for the new mongod instance.
Open a mongo shell connected to the replica set’s primary:
mongo
注解
The primary is the only member that can add or remove members from the replica set. If you do not know which member is the primary, log into any member of the replica set using mongo and issue the db.isMaster() command to determine which member is in the isMaster.primary field. For example:
mongo mongodb0.example.net
db.isMaster()
If you are not connected to the primary, disconnect from the current client and reconnect to the primary.
In the mongo shell, issue the following command to add the new member to the replica set.
rs.add("mongodb3.example.net")
注解
You can also include the port number, depending on your setup:
rs.add("mongodb3.example.net:27017")
Verify that the member is now part of the replica set by calling the rs.conf() method, which displays the replica set configuration:
rs.conf()
You can use the rs.status() function to provide an overview of replica set status.
Alternately, you can add a member to a replica set by specifying an entire configuration document with some or all of the fields in a members sub-documents. For example:
rs.add({_id: 1, host: "mongodb3.example.net:27017", priority: 0, hidden: true})
This configures a hidden member that is accessible at mongodb3.example.net:27017. See host, priority, and hidden for more information about these settings. When you specify a full configuration object with rs.add(), you must declare the _id field, which is not automatically populated in this case.