Archive: This content is maintained for historical reference. Please note that the specific versions and commands may no longer be compatible with modern systems.
In this guide, I will show you the simple steps required to set up a MongoDB installation with replication on Windows Server.
Environment Details
This guide was tested using Windows Server 2012 R2 Standard. For this setup, I used the following three servers:
- mongodb01: 192.168.1.42
- mongodb02: 192.168.1.43
- mongodb03: 192.168.1.44
All servers must be in the same Workgroup or Domain. If they are not, you must add all server names and IPs to your hosts file so that MongoDB knows how to connect to them.
Let’s get started!
Step 1: Download MongoDB (On all three servers)
MongoDB provides Windows installer packages (.msi). Download the file from their website at http://www.mongodb.org/.
Direct link: https://fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus-ssl-3.0.0-signed.msi
Note: Even though the installer mentions Windows 2008, it works perfectly on Windows 2012!
Step 2: Install MongoDB
Follow the images below to install MongoDB. You must perform these steps on all three servers.

Click Next on the screen shown above.

Read the license agreement, check the box to accept the terms, and click Next.

Select the Complete installation version.

Click Install to begin the installation process.

Once the installation is finished, click Finish.
Remember: Complete this on all three servers before proceeding.
Step 3: Create database and log directories (On all three servers)
Create the following directory structure:
C:\ulyaoth\mongodb\data\dbC:\ulyaoth\mongodb\data\logC:\ulyaoth\mongodb\config
Step 4: Configure the Windows Firewall (On all three servers)
Go to the Control Panel, click on Network and Internet, then Network and Sharing Center. In the bottom left, click on Windows Firewall.
Your Windows Firewall screen should look like this:

Click on Allow an app or feature through Windows Firewall. In the next window, click Allow another app… and enter the details as shown below.

Once configured, click OK to close the firewall settings.
Step 5: Create a MongoDB configuration file (On all three servers)
Open Notepad and add the following configuration:
systemLog:
destination: file
path: "C:/ulyaoth/mongodb/data/log/mongod.log"
logAppend: true
storage:
dbPath: "C:/ulyaoth/mongodb/data/db"
journal:
enabled: true
net:
bindIp: 0.0.0.0
port: 27017
replication:
replSetName: ulyaoth
Save this file as mongod.yaml in the directory: C:\ulyaoth\mongodb\config\
Ensure the final path is: C:\ulyaoth\mongodb\config\mongod.yaml
Step 6: Create a service to auto-start MongoDB (On all three servers)
Open a Command Prompt as Administrator and run the following command:
sc.exe create MongoDB binPath= "\"C:\Program Files\MongoDB\Server\3.0\bin\mongod.exe\" --service --config=\"C:\ulyaoth\mongodb\config\mongod.yaml\"" DisplayName= "MongoDB 3.0 Standard" start= "auto"
If successful, the output should look like this:

Note: If you ever need to remove the service, use the command: sc.exe delete MongoDB
Step 7: Start MongoDB (On all three servers)
Restart the servers. MongoDB should start automatically, which serves as a good test to ensure your previous configurations are correct.
Step 8: Access the MongoDB Shell (Only on server mongodb01)
Navigate to C:\Program Files\MongoDB\Server\3.0\bin and double-click mongo.exe. A terminal window will open:

Step 9: Create the Replica Set in the Mongo Shell (Only on server mongodb01)
Inside the Mongo shell, run the following commands:
rs.initiate()
rs.add("mongodb02:27017")
rs.add("mongodb03:27017")
cfg = rs.conf()
cfg.members[0].priority = 100
cfg.members[1].priority = 50
cfg.members[2].priority = 50
rs.reconfig(cfg)
(Note: If you cannot resolve the hostnames “mongodb02” or “mongodb03,” use the server’s full IP address instead.)

Step 10: Verify the configuration (Only on server mongodb01)
While still in the Mongo shell, type:
rs.status()
f everything is correct, the output should look like this:

Congratulations! You have successfully installed MongoDB on Windows and configured it for replication. Now, let’s test the replication by creating a database on the Primary (mongodb01).
Step 11: Create a test collection on the Primary (Only on server mongodb01)
In the Mongo shell, run the following:
use ulyaoth
u = { name : "ulyaothguides" }
db.Data.insert( u )
show dbs
show collections
db.Data.find()
You should see output similar to this:

As shown, show dbs displays the “ulyaoth” database, show collections displays the “Data” collection, and db.Data.find() confirms the entry “ulyaothguides” exists.
Step 12: Check replication on the Slaves (On server mongodb02 or mongodb03)
Go to server two or three, open the Mongo shell, and run the following commands:
rs.slaveOk()
show dbs
use ulyaoth
show collections
db.Data.find()
If replication is working, you will see this:

Note: The rs.slaveOk() command is required to allow read operations from a slave/secondary node, as this is disabled by default.
Everything is now working! You can now use your MongoDB replica set for your projects.