Logo

dev-resources.site

for different kinds of informations.

MongoDB: Startup replica sets with a config file

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: Startup replica sets with a config file

Hi Devs,
In the previous post, we talked about setting up replica sets. Kindly go through the post for setting up replica set.
What we will be discussing today will be starting up each member of our replica sets with a config file

This post will be as brief as possible.

In our previous post, we create replica sets with 3 members. We will only work with one of them while you replicate the steps below for other members.

Create a config file

Change to the directory where we created our replica sets. In our case it was in our home directory

cd replicas
Enter fullscreen mode Exit fullscreen mode

Terminal

cd db1
Enter fullscreen mode Exit fullscreen mode

What the command does is change our working directory to first member of the replica set. We will create the config file for this member here

touch mongo.conf
Enter fullscreen mode Exit fullscreen mode

What the command does is to create a filename mongo with extension .conf.
Kindly note the name of the file is up to you. However, the extension must be .conf
Past the following into the file

replication:
  replSetName: "rs0"
net:
  bindIp: 127.0.0.1
  port: 27018
systemLog:
  destination: file
  path: "./logs/log1.log"
  logAppend: true
storage:
  dbPath: "./data/db1"
  journal:
    enabled: true
security:
  authorization: "disabled"
Enter fullscreen mode Exit fullscreen mode

Note
Make sure what is pasted follows yaml convention

If you followed the previous post you would notice the replica set id we used was rs0 that explains the replSetName in the config file
port: the port this member listens from. In this case it is 27018 for the first member
systemLog: The path value where you want to write the logs to. You can disable it if you don't to write it to a log. It will output it on your console
storage: The dbPath value is the data directory of the member in the replica sets. For this member it resides in the specified path above

Kindly check the previous post for how we created the logs and data directory for each member in the set

Repeat the step above for other members in the set

Connect to the member's instance

Open 3 tabs on your terminal
make sure the working directory on the terminal is the directory where all these members were created. In our case it is replicas
First Terminal

mongod --config db1/mongo.conf
Enter fullscreen mode Exit fullscreen mode

The command above will start the mongo instance for the first member of the set. You will not see any logs on the console. This is because the logs are written to its log file.

Second terminal

mongod --config db2/mongo.conf
Enter fullscreen mode Exit fullscreen mode

Third Terminal

mongod --config db3/mongo.conf
Enter fullscreen mode Exit fullscreen mode

Open a fourth terminal to connect to mongo interactive shell for the first memeber

  mongosh --port 27018
Enter fullscreen mode Exit fullscreen mode

If you followed the previous post you would notice that is the port we listened to the first member of the set

Feel to ask any questions.

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: