Tutorials

How to install MongoDB 3.4 in replication on Windows Server 2016

In this guide I will show some simple steps of how to setup a mongodb installation in a replication on Windows Server.

This guide was tested with:
Windows Server 2016

I am using AWS for this setup and created three servers that have Windows Server 2016 installed.
Server Type: t2.large (T Server Type is probably not a good match for a production db however for this tutorial it is cheap and will do it’s part)
mongodb01: 172.31.22.128
mongodb02: 172.31.27.75
mongodb03: 54.154.36.169

As a pre requirement all servers in the same workgroup or in the same domain, if they are not then you have to add all the servers in your hosts file so mongodb knows how to connect to them.

So lets start!

Step 1: Download MongoDB (on all three servers)
MongoDB provides windows installer packages so simply download their msi file from their website https://www.mongodb.com/.
https://fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus-ssl-3.4.4-signed.msi

Even it does say Windows Server 2008 it does work perfect on Windows Server 2016!

Step 2: Install MongoDB
Just follow the pictures below to install MongoDB, you have to do this on all three servers.

Just press next on the picture above.


Read the license and then tick the box you accept the license and press next on the picture above.


Press you wish to install the “Complete” version on the picture above.


On the window above just press “Install” to start the installation.


You should now have finished the installation so simpely press “Finish”.

Remember you have to do this on all three your servers.

Step 3: Create a database and log directory (on all three servers)
Create the following directories:
C:\ulyaoth\mongodb\data\db
C:\ulyaoth\mongodb\data\log
C:\ulyaoth\mongodb\config

Step 4: Fix the Windows firewall (on all three servers).
Normally this step should be optional and the installer should have added this but it not always show up somehow so in case you require it here are the manual steps.

Go to your “Control Panel” and then click on “Network and Internet” once there click on “Network and Sharing Center” and then on the left side at the bottom click on “Windows Firewall”.

You should now see your Windows Firewall like this:

On this window click on “Allow an app or feature trough Windows Firewall” your window will change and click on this window on “Allow another app..” and browse to the mongodb folder and choice for path: C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe, it should look like this:

If everything looks as above press on Add to allow the app in the Firewall as you can see above.

If everything is as on the above picture then simply close the Firewall by pressing on the “OK” button.

Step 5: Create a MongoDB config file (on all three servers)
Open a notepad and add the following information:
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

Once added then save the file as “mongod.conf” in the directory:
"C:\ulyaoth\mongodb\config\"
You should end up with
"C:\ulyaoth\mongodb\config\mongod.conf"

Step 6: Create a service that will auto start MongoDB (on all three servers)
Open a Command Prompt and type the following:
sc.exe create MongoDB binPath= "\"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe\" --service --config=\"C:\ulyaoth\mongodb\config\mongod.conf\"" DisplayName= "MongoDB 3.4 Standard" start= "auto"

This should create a service for MongoDB if you did it correct, it should look like this:

If you ever wish to delete the service you can do so with the following command:
sc.exe delete MongoDB

Step 7: Start MongoDB (on all three servers)
Just restart the server and MongoDB should automatically start so it is a good test that the previous commands worked.
You can check if MongoDB started by right clicking the start menu and then choice “Run” and type “services.msc” and then press OK.

Now scroll to “MongoDB 3.4 Standard” and the Status should say “Running” as you see on the screenshot below.

Step 8: Go into the MongoDB shell (only on server one)
Go to “C:\Program Files\MongoDB\Server\3.4\bin” and double click on “mongo” a terminal window should open that looks like this:

Step 9: Create the replica set in the mongo shell. (only on server one)
While being in the mongo shell type 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)

Please be aware if you cannot reach the names “mongodb02” or “mongodb03” you have to use the full ip address of the server or add it to your hosts file see screenshot below.

Step 10: Test your configuration is working. (only on server one)
in the mongo shell still type:
rs.status()

You should see something like this if everything is correct:

Congratulations you now have successfully installed mongodb on windows and you have set it up in a replication :)! Now lets test that the replication works by creating a database on the master (mongodb01).

Step 11: Create a test collection with some data on the master (only on server one)
In the mongo shell type the following:
use ulyaoth
u = { name : "ulyaothtutorials" }
db.Data.insert( u )
show dbs
show collections
db.Data.find()

You should see something like this:

As you can see “show dbs” did show you have a database ulyaoth, “show collections” show you have the collection Data and “db.Data.find()” did show that the Data collections contains the information “ulyaothguides”.

Now if everything did work as intended all this should have been replicated to your servers mongodb02 and mongodb03 so lets test it!

Step 12: Check if the slaves have data (on server two or three)
Go to your server two or three and open a mongo shell by double clicking on the “mongo” file and run the following commands:
rs.slaveOk()
show dbs
use ulyaoth
show collections
db.Data.find()

If everything worked you should see the following:

The command “rs.slaveOk()” this is so you allow to read from the slave by default this is not enabled.
And if you wish to drop the “ulyaoth” database you can use the following two commands:
use ulyaoth
db.dropDatabase();

Well this was it everything worked and you can now use your MongoDB replica set for anything you like!

As always if you have any improvements or see any mistakes please let me know I am always open to hear your idea or to learn from you!

Related posts

Install Chromium from source on Mac OS X

Sjir Bagmeijer

How to install Nginx with Passenger 5.0 on Fedora

Sjir Bagmeijer

How to use a VHD image file with VirtualBox

Sjir Bagmeijer