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
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
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
mkdir db2
mkdir db3
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
cd data
What the commands above does is create the data directory and make it the working directory.
mkdir db1
mkdir db2
mkdir db3
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 ..
what the command above does is change the working directory to replicas
mkdir logs
cd logs
What the command above does is create the logs directory and make it the working directory.
touch log1.log
touch log2.log
touch log3.log
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
Second terminal:
mongod --replSet rs0 --port 27019 --bind_ip localhost --dbpath db2
Third terminal:
mongod --replSet rs0 --port 27010 --bind_ip localhost --dbpath db3
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
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"},
]
}
Hit enter
rs.initiate(rsConf)
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()
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)