Setting Up A MongoDB Server On Windows VM In Azure

In this post, we will see how to setup a MongoDB server on Windows VM in Azure and connect from any other remote machine using public IP.

Introduction

 
MongoDB is a general-purpose, document-based, distributed database built for modern application developers and for the cloud era. We will create a Windows VM in Azure and then we will install MongoDB server in that VM and configure firewall rules to access MongoDB from outside the VM. We can see all these actions step by step.
 

Create a Windows 2016 server VM in Azure

 
Open Azure portal and choose Virtual Machine blade and click “add” button. Give a valid name and choose the required region for VM. You must also choose the image type. Here, we are creating Windows 2016 datacenter. Choose the appropriate VM size also. We have chosen Standard D2s v3. Each type has different costs per month. Carefully consider that also.
 
Setting Up A MongoDB Server On Windows VM In Azure
 
We can click “Review + create” button to start VM deployment after reviewing all the properties.
 
Our virtual machine will be ready after a few moments.
 
We can add an “inbound” port rule for remote desktop connections.
 
Choose networking blade and click “Add inbound port rule” button.
 
Setting Up A MongoDB Server On Windows VM In Azure
 
Enter destination port ranges as “3389”. This is the port used for remote desktop connections.
 
Setting Up A MongoDB Server On Windows VM In Azure
 
We can add one more inbound port rule for MongoDB (though we have not yet installed MongoDB server in VM). MongoDB usually works in default port “27017”. Hence, we can create a rule for this port also.
 
Setting Up A MongoDB Server On Windows VM In Azure
 
We have completed all the required setup for VM. We can connect to this VM using a remote desktop connection from our local machine and install and configure the MongoDB server.
 
We can download any version of MongoDB from their official site. I have chosen the MongoDB community server version for testing purposes.
 
Setting Up A MongoDB Server On Windows VM In Azure
 
After downloading the setup file, install the MongoDB server. I am installing it as a service. One windows service will be created after a successful installation.
 
Setting Up A MongoDB Server On Windows VM In Azure
 
We must change the MongoDB server config file after installation.
 
Open below “bin” folder of MongoDB server and edit the “mongod.cfg” file with any text editor.
 
Setting Up A MongoDB Server On Windows VM In Azure
 
You must change the “bindIp” to “10.0.0.4” from the default “127.0.0.1”. Please note, this is the NIC private IP for our VM. Most cases, this private IP is common for all Windows VMs.
 
Setting Up A MongoDB Server On Windows VM In Azure
 
After saving the above configuration file, you must restart the below MongoDB server service. Then only the configuration changes will be affected in the MongoDB Server.
 
Setting Up A MongoDB Server On Windows VM In Azure
 
As a final step, we must add a port number “27017” in the Windows Firewall setting also.
 
Choose “Windows Firewall with Advanced Security” and choose the “Inbound Rules” tab and add a new rule.
 
Setting Up A MongoDB Server On Windows VM In Azure
 
We can choose the rule type as “Port”.
 
Setting Up A MongoDB Server On Windows VM In Azure
 
Add port “27017” for MongoDB Server.
 
Setting Up A MongoDB Server On Windows VM In Azure
 
Finally, you can give a valid name for this rule and save.
 
We have completed the entire configuration for the MongoDB server. We can connect this server from any other remote machine using MongoDB Compass or any other client tool.
 
I am using MongoDB compass to connect server. Please give your VM’s public IP as the hostname and click the "Connect" button.
 
Setting Up A MongoDB Server On Windows VM In Azure
 
MongoDB Server is successfully connected with another machine. We can see the default databases and collections.
 
Setting Up A MongoDB Server On Windows VM In Azure
 

Conclusion

 
In this post, we have created a Windows VM in Azure and we have connected this VM using remote desktop and installed MongoDB server. Later we have changed the MongoDB server configuration and created a firewall inbound rule to the MongoDB default port 27017.
 
Finally, we have connected the MongoDB server from another machine using MongoDB Compass with VM’s public IP.