Replica sets in MongoDB provide redundancy and high availability, and are the basis for all production deployments
Here are the steps how to start MongoDb in Replicated mode:
1. Identify your replica set members:
While physically the members of a replica set can be located anywhere, but for the best performance the majority of the members in a replica set must be located within the same data center. Further, for the purposes of allocating votes, every member should be accessible by all other members in the set.
1. Set Up the First Member of the Replica Set:
Start or connect to the first member of the set. Be sure to specify the —replSet
For example, the following command starts a standalone MongoDB instance as a member of a replica set named repl:
```
mongod —port 27017 —dbpath /var/lib/mongo/data/db —replSet repl
```
Use appropriate value for —dbpath based on your system
1. Connect a mongo Shell to the First Member:
Start the mongo shell and connect to the first replica set member. Use the following command to connect to the MongoDB instance that you started in the previous step:
```
mongo —port 27017
```
1. Initiate the Replica Set:
Use rs.initiate() to initiate the replica set:
```
rs.initiate()
```
1. Check the Status of the Replica Set:
Use rs.status() to confirm that the replica set has been initiated:
```
rs.status()
```
1. Add Members to the Replica Set:
\`\`\`mongo
rs.add(“hostanme:port”)
\`\`\`
Note: Additional members impose additional costs, including replication costs and maintenance. It’s not recommended to add extra replica set members.