Logo

dev-resources.site

for different kinds of informations.

MongoDB: How to setup replica sets

Published at
12/31/2024
Categories
mongodb
nosql
database
Author
olawaleo
Categories
3 categories in total
mongodb
open
nosql
open
database
open
Author
8 person written this
olawaleo
open
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/

nosql Article's
30 articles in total
Favicon
O que é o Apache Cassandra e quando usar?
Favicon
Efficient Batch Writing to DynamoDB with Python: A Step-by-Step Guide
Favicon
SQL VS NoSQL
Favicon
MongoDB: How to setup replica sets
Favicon
Do you think schema flexibility justifies using NoSQL? Think twice.
Favicon
Series de tiempo en MongoDB
Favicon
What I Learned from the 'Amazon DynamoDB for Serverless Architectures' Course on AWS Skill Builder
Favicon
MongoDB Command Shortcuts: The Ultimate Guide
Favicon
MongoDB: Startup replica sets with a config file
Favicon
Azure Logs Analytics for CosmosDB
Favicon
Choosing the Right Database: A Simplified Guide
Favicon
Understanding the Differences Between NoSQL and SQL Databases
Favicon
Part 2 - CosmosDB Logical Partition and the Impact on Partition Key Choice
Favicon
Partitions in Azure Cosmos DB: A Common Discussion with Customers
Favicon
Database Sharding: Simplifying Data Scalability
Favicon
HTTP and GraphQL
Favicon
New possibilities with GraphQL
Favicon
NoSQL delivers quick value
Favicon
Navigating Databases: From SQL to NoSQL
Favicon
Selecting the Right Database for the Job
Favicon
NewSQL: Bridging the Gap Between SQL and NoSQL
Favicon
Weekly Updates - October 18, 2024
Favicon
Overcoming MongoDB Limitations with Fauna
Favicon
MongoDB Developer Day Manila 2024: A Recap - A Deep Dive into the Future of Data
Favicon
How to choose the right database?
Favicon
SQL vs. NoSQL: Key Differences, Use Cases, and Choosing the Right Database for Your Project
Favicon
Top 5 SQL questions asked in interviews
Favicon
Weekly Updates - Nov 8, 2024
Favicon
Plain Javascript Refresher for those feeling left behind or not knowing where to start w/ Functions, Arrays, Loops, JSON & NoSQL
Favicon
Mastering DynamoDB: Batch Operations Explained

Featured ones: