Skip to content
Santi020k
Blog Post

Configuring MongoDB with Homebrew on macOS: Converting a Standalone Instance to a Replica Set

Setting up a MongoDB replica set in your local environment can be essential for testing and development purposes, especially when you need to mimic a production...

configuring mongodb with homebrew on macos converting a standalone instance to a replica set 482623476dcf

Setting up a MongoDB replica set in your local environment can be essential for testing and development purposes, especially when you need to mimic a production-like setup. In this guide, we’ll walk through the steps to configure a MongoDB replica set on your local machine.

Recipe

  1. Modify mongod.conf for Replication

Open the mongod.conf file and add the replication key to enable replication.

  • Location of mongod.conf:
  • /usr/local/etc/mongod.conf (on Intel processors)
  • /opt/homebrew/etc/mongod.conf (on Apple M1 processors)

Add replication:

mongod.conf
replication:
  replSetName: rs

Original Configuration:

mongod.conf (Original)
systemLog:
  destination: file
  path: /usr/local/var/log/mongodb/mongo.log
  logAppend: true
storage:
  dbPath: /usr/local/var/mongodb
net:
  bindIp: 127.0.0.1

Modified Configuration:

mongod.conf (Modified)
systemLog:
  destination: file
  path: /usr/local/var/log/mongodb/mongo.log
  logAppend: true
storage:
  dbPath: /usr/local/var/mongodb
net:
  bindIp: 127.0.0.1
replication:
  replSetName: rs
  1. Restart MongoDB Service

In the console, restart the MongoDB instance using:

terminal
brew services restart mongodb-community
  1. Connect to MongoDB Shell

In the console, connect to the MongoDB shell using the command:

terminal
mongosh
  1. Initialize Replica Set

Start the replica set with:

terminal
rs.initiate({_id: "rs", members: [{_id: 0, host: "127.0.0.1:27017"}] })
  1. Switch to Primary Instance (Optional)

Change to the primary instance using rs.status

  1. Check Replica Set Status

Verify the status of the replica set using rs.status(). If everything is working properly, this command will print a JSON with all the configurations.

  1. 🕺 That’s it! 😎

Conclusion

Configuring a MongoDB replica set in your local environment is a crucial step in replicating real-world scenarios for testing and development. By following these simple steps, you can set up a replica set seamlessly and efficiently, enabling you to work with MongoDB in a more versatile and robust manner.


Configuring MongoDB with Homebrew on macOS: Converting a Standalone Instance to a Replica Set was originally published in Towards Dev on Medium, where people are continuing the conversation by highlighting and responding to this story.

Keep reading

More writing in the same thread.

A few more posts that overlap in topic, tooling, or the engineering tradeoffs behind this article.