Hey there! Welcome to my first blog post, and I’m excited to dive into an important topic: MongoDB replica sets. As we know, a MongoDB replica set ensures high availability with automatic failover to secondary nodes, provides data redundancy through multiple copies of your data, enables load balancing by distributing read operations for improved performance, and simplifies backup and recovery by allowing backups from secondary nodes without disrupting the primary node’s performance.
Now, if you’re using a MongoDB Atlas URI, the good news is that you don’t have to worry about all this setup—it’s taken care of for you by default, allowing you to enjoy one of my favorite features: MongoDB Transactions. However, this can be quite time-consuming and inefficient for a development environment, which is a common need among developers.
After months of research and experimentation, I’m thrilled to share the steps I’ve implemented. You can follow this guide to effortlessly set up a MongoDB replica set on your localhost Windows machine and start enjoying its benefits!
For more in-depth information, you can check out the official MongoDB documentation on replica sets, which provides comprehensive insights and guidance to enhance your understanding
If you're looking to set up a MongoDB 8.0 replica set on your localhost, be sure to read ahead. For additional context, you can also check out this older YouTube video on MongoDB Version 6.0.
First things first! Download the MongoDB zip file and extract it directly to your C Drive. Once extracted, make sure you have the following folder structure in place: C:\mongodata\db\db1
and C:\mongodata\db\db2
. This organization is crucial for ensuring your MongoDB setup operates smoothly and efficiently.
Next, we need to open the Command Prompt with administrative privileges. Just type "CMD" in the Windows search bar, right-click it, and select "Run as administrator." Let’s do this!
Now, let's set up the MongoDB services! Run the following commands in an elevated Command Prompt to create the services for your replica set:
Shell
sc.exe create MongoDB(1) binPath= "\"C:\Program Files\MongoDB\Server\8.0\bin\mongod.exe\" --service --config=\"C:\mongodata\db\db1\mongo.cfg\"" DisplayName= "MongoDB(1)" start= "auto"
sc.exe create MongoDB(2) binPath= "\"C:\Program Files\MongoDB\Server\8.0\bin\mongod.exe\" --service --config=\"C:\mongodata\db\db2\mongo.cfg\"" DisplayName= "MongoDB(2)" start= "auto"
You should see a success message: "[SC] CreateService SUCCESS." 🎉
Let’s get those services up and running! Start by typing 'services' in the Windows search bar and open the Services application. Locate MongoDB1 and MongoDB2 in the list, and start both services to proceed.
Note: It’s crucial to stop any other running MongoDB services or instances first; otherwise, this setup won’t work properly. Ensuring only MongoDB1 and MongoDB2 are active will prevent conflicts and guarantee smooth operation.
Now it’s time to add the replica set to your primary node! First, ensure you have the Mongo shell installed and open it as an administrator. Once the Mongo shell is up and running, enter the following commands to set up your replica sets (note that you’ll need to type these commands manually, as pasting isn’t supported in the terminal):
rs.initiate()
rs.add({ host: "localhost:27018" })
Congratulations! If you’ve followed these steps, you’ve successfully enabled MongoDB replication. You can verify this by replacing your Atlas URI with the local address: mongodb://127.0.0.1:27017/
. Now you can dive into the exciting world of MongoDB transactions, build something amazing, and don’t forget to share your creations with me!
If you ever need to delete the MongoDB services, simply run the following commands in an elevated Command Prompt:
Shell
sc.exe delete MongoDB(1)
sc.exe delete MongoDB(2)
This will remove the specified services from your system. Always ensure that the services are stopped before executing these commands for a smooth deletion process.