Creating Replica Set for local MongoDB server

A replica set in MongoDB is a group of mongod processes that maintain the same data set.

With multiple copies of data on different database servers, replication provides a level of fault tolerance against the loss of a single database server.

All databases hosted on MongoDB Atlas are configured as replica sets. Atlas makes it easy to add and remove replica set members in any region of your preferred cloud provider.

If you want to implement a transaction in your project, you must have a replica set in your local machine.

In this post, we will see how to create and configure a replica set in MongoDB.

//Following steps are for Ubuntu users

  1. Download MongoDB TGZ Package according to your Ubuntu version.
  2. Extract your TGZ package
  3. Rename it to mongodb and move it to home directory
  4. Create one empty folder in the same(home) directory and name it mongodb-data
  5. Now run the following command in your terminal.

/home/PCusername/mongodb/bin/mongod –port 27017 –dbpath /home/PCusername/mongodb-data –replSet rs0 –bind_ip 127.0.0.1,192.168.1.133(give IP addresses of the database server that you are using)

Run the above command every time you want to use a replica set

Open a different terminal and enter the command: mongo

Then apply command: rs.initiate()

You may also like

Leave a Reply