发布于 2015-09-14 14:53:55 | 123 次阅读 | 评论: 0 | 来源: 网络整理
While standalone MongoDB instances are useful for testing, development and trivial deployments, for production use, replica sets provide required robustness and disaster recovery. This tutorial describes how to convert an existing standalone instance into a three-member replica set. If you’re deploying a replica set “fresh,” without any existing MongoDB data or instance, see 部署一个副本集.
For more information on replica sets, their use, and administration, see:
注解
If you’re converting a standalone instance into a replica set that is a shard in a sharded cluster you must change the shard host information in the config database. While connected to a mongos instance with a mongo shell, issue a command in the following form:
db.getSiblingDB("config").shards.save( {_id: "<name>", host: "<replica-set>/<member,><member,><...>" } )
Replace <name> with the name of the shard, replace <replica-set> with the name of the replica set, and replace <member,><member,><> with the list of the members of the replica set.
After completing this operation you must restart all mongos instances. When possible you should restart all components of the replica sets (i.e. all mongos and all shard mongod instances.)
This procedure assumes you have a standalone instance of MongoDB installed. If you have not already installed MongoDB, see the installation tutorials.
Shut down the your MongoDB instance and then restart using the --replSet option and the name of the replica set, which is rs0 in the example below.
Use a command similar to the following:
mongod --port 27017 --replSet rs0
This starts the instance as a member of a replica set named rs0. For more information on configuration options, see 配置文件选项 and the mongod.
Open a mongo shell and connect to the mongod instance. In a new system shell session, use the following command to start a mongo shell:
mongo
Use rs.initiate() to initiate the replica set:
rs.initiate()
The set is now operational. To return the replica set configuration, call the rs.conf() method. To check the status of the replica set, use rs.status().
Now add additional replica set members. On two distinct systems, start two new standalone mongod instances. Then, in the mongo shell instance connected to the first mongod instance, issue a command in the following form:
rs.add("<hostname>:<port>")
Replace <hostname> and <port> with the resolvable hostname and port of the mongod instance you want to add to the set. Repeat this operation for each mongod that you want to add to the set.
For more information on adding hosts to a replica set, see the 将成员添加到一个副本集 document.