Configure Replica Set
Once the MongoDB service is up and running, perform the below steps to configure the replica set for MongoDB.
- Go to one of the servers that is a part of the replica set.
- Open the Command Prompt as Administrator and execute the command below to
open the
terminal.
mongosh --host <IP> --port <in which MongoDBMongoDB service is running. The one written in config > for e.g. - mongosh --host x.x.xx.x1 --port 27014 - In the same terminal, execute the following
commands:
MongoDBMongoDB > rs.initiate( { _id: "MyReplicaSet", members: [ {_id: 0, host: "<IP>:<PORT> “}, {_id: 1, host: "<IP>:<PORT> “}, {_id: 2, host: "<IP>:<PORT> “} ] }) - Here, add the hosts that are part of the replica set and mention the respective host IP
and port on which MongoDB service is running. Ensure that the
ports are open for communication and ports are different in each
server.
MongoDB > rs.initiate( { _id: "MyReplicaSet", members: [ {_id: 0, host: "10.1. 1xx.x1:27014”}, {_id: 1, host: "10.1. 1xx.x21:27015”}, {_id: 2, host: "10.1. 1xx.x3:27016”} ] }) - Enter the command below to identify the Primary and Secondary server:
MongoDBMongoDB >rs.status()