DEV Community

omoogun olawale
omoogun olawale

Posted on

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 free to ask any ask questions.

Top comments (0)