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...
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
- 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:
replication:
replSetName: rsOriginal Configuration:
systemLog:
destination: file
path: /usr/local/var/log/mongodb/mongo.log
logAppend: true
storage:
dbPath: /usr/local/var/mongodb
net:
bindIp: 127.0.0.1Modified Configuration:
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- Restart MongoDB Service
In the console, restart the MongoDB instance using:
brew services restart mongodb-community- Connect to MongoDB Shell
In the console, connect to the MongoDB shell using the command:
mongosh- Initialize Replica Set
Start the replica set with:
rs.initiate({_id: "rs", members: [{_id: 0, host: "127.0.0.1:27017"}] })- Switch to Primary Instance (Optional)
Change to the primary instance using rs.status
- 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.
- 🕺 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.