DEV Community

omoogun olawale
omoogun olawale

Posted on

MongoDB: How to setup replica sets

Hi devs
Today I am going to write about an interesting topic in mongodb which is How to setup replica sets on your localhost machine for development only

Setup replica sets folder

mkdir replicas
Enter fullscreen mode Exit fullscreen mode

What the command above does is create a directory/folder named replicas in my home directory. This directory will hold the number of replica sets we want to have
Note: I am using mac for this tutorial

cd replicas
Enter fullscreen mode Exit fullscreen mode

What the command above does is change your working directory to replicas. Inside this directory is where we will create directories for our replica sets

mkdir db1
Enter fullscreen mode Exit fullscreen mode
mkdir db2
Enter fullscreen mode Exit fullscreen mode
mkdir db3
Enter fullscreen mode Exit fullscreen mode

What the command above does is create 3 different directories for the members of our replica sets. 3 members will suffice for this tutorial. Feel free to create as many as possible.
Kindly note replica sets must have at least 2 members

Setup data folders for replica sets

The next phase is create the data directory for each member of the sets

mkdir data
Enter fullscreen mode Exit fullscreen mode
cd data
Enter fullscreen mode Exit fullscreen mode

What the commands above does is create the data directory and make it the working directory.

mkdir db1
Enter fullscreen mode Exit fullscreen mode
mkdir db2
Enter fullscreen mode Exit fullscreen mode
mkdir db3
Enter fullscreen mode Exit fullscreen mode

What the command above does is to create 3 directories for each member of the sets. We are creating 3 because we have 3 members in our case.
Kindly note the name of the folder/directory is totally up to you

Setup replica sets log folders

The next phase is create the logs directory for each member of the sets

cd ..
Enter fullscreen mode Exit fullscreen mode

what the command above does is change the working directory to replicas

mkdir logs
Enter fullscreen mode Exit fullscreen mode
cd logs
Enter fullscreen mode Exit fullscreen mode

What the command above does is create the logs directory and make it the working directory.

touch log1.log
Enter fullscreen mode Exit fullscreen mode
touch log2.log
Enter fullscreen mode Exit fullscreen mode
touch log3.log
Enter fullscreen mode Exit fullscreen mode

What the command above does is create 3 log files for each member of the sets. We are creating 3 because of we have 3 members in our case.
Kindly note the name of the files is totally up to you

Start instance of each member

What we want to do at this stage is start the instance of the replica set members

Open 3 tabs on your MacOS terminal or on windows open 3 command prompts

Make sure the working directories of these terminals are replicas

First terminal:

mongod --replSet rs0 --port 27018 --bind_ip localhost --dbpath  db1
Enter fullscreen mode Exit fullscreen mode

Second terminal:

mongod --replSet rs0 --port 27019 --bind_ip localhost --dbpath  db2
Enter fullscreen mode Exit fullscreen mode

Third terminal:

mongod --replSet rs0 --port 27010 --bind_ip localhost --dbpath  db3
Enter fullscreen mode Exit fullscreen mode

rs0: replica set id

Initiate members

What we did above is startup instance of each member of the replica sets
Open a fourth terminal

bash
mongosh --port 27018
Enter fullscreen mode Exit fullscreen mode

what the command above does is connect to interactive mongo shell of the first member of the replica sets. This is where we will initiate the members

rsConf = {
  _id: "rs0",
  members: [
     {_id: 0, host: "localhost:27018"},
     {_id: 0, host: "localhost:27018"},
     {_id: 0, host: "localhost:27018"},
   ]
 }
Enter fullscreen mode Exit fullscreen mode

Hit enter

 rs.initiate(rsConf)
Enter fullscreen mode Exit fullscreen mode

Hit enter

Voila!!!!. You have successfully initiated replica sets.

To verify if the initiation was successful. Enter the command below on your terminal

rs.status()
Enter fullscreen mode Exit fullscreen mode

How to start replica sets with config file

You should see an object of the replica sets with its members

Feel free to ask questions.

References

https://vishalrana9915.medium.com/how-to-create-replica-sets-in-mongodb-eaaa7b967b43
https://hevodata.com/learn/mongodb-replica-set-3-easy-methods/

Top comments (0)