MongoDB database converts a single-host instance to a replica set.
This article describes how to convert an independent mongo instance to a replica set containing three members. Independent instances are used for development and testing, and replica sets are used for production. How to install an independent mongo instance is not described in this article.
If the mongo instance is not installed when the replica set is deployed, you can view the replica set;
Procedure
Stop independent mongo instance services. Restart the instance. Use the -- replSet option to configure the replica set name. For example, the following command starts an independent instance as a member of the replica set rs0. It uses an independent database path/srv/mongodb/db0, which must exist in advance; otherwise, an error is returned.
mongod --port 27017 --dbpath /srv/mongodb/db0 --replSet rs0
If the program connects more than one replica set, each replica set must have a unique name. Some drivers connect to the replica set group based on the replica set name.
3. Connect to the mongo instance.
4. Use rs. initiate () to initialize a new replica set.
rs.initiate()
Now you can operate on the replica set.
You can use rs. conf () to view the replica set configuration and use rs. status () to check the replica set status.
Extended replica set
To add a replica integrator, follow these steps:
1. Start two independent mongo instances in two different systems.
2. When connecting to the original mongo instance (an independent instance), run the following command to add the new instance to the replica set:
rs.add("
<:port>")
Replace host names and ports that can be distinguished by mongo instances And To the replica set.
Partition considerations
If a part of the sharded cluster is added to a replica set, follow these steps:
1. Connect to one of the mongos instances of the sharded cluster and run the following command:
db.getSiblingDB("config").shards.save( {_id: "
", host: "
/
<...>" } )
Replace part names , Replace with the replica Set Name , Replace with the replica Integrator <>.
2. Restart all mongos instances. If possible, restart all components of the replica set.