Install MongoDB on a Windows VM in Azure


Azure has two different deployment models for creating and working with resources: Resource Manager and classic. This article covers using the classic deployment model. Microsoft recommends that most new deployments use the Resource Manager model. To install and configure MongoDB using the Resource Manager deployment model, see this article.

MongoDB is a popular open-source, high-performance NoSQL database. This article guides you through creating a Windows Server virtual machine (VM) using the Azure classic portal. You then create and attach a data disk to the VM before installing and configuring MongoDB. If you have an existing VM in Azure that you would like to use, you can jump straight to installing and configuring MongoDB.

Create a virtual machine running Windows Server

Follow these instructions to create a virtual machine.

  1. Sign in to the Azure portal.

  2. Starting in the upper left, click New > Compute > Windows Server 2016 Datacenter.

    Navigate to the Azure VM images in the portal

  3. On the Windows Server 2016 Datacenter, select the Classic deployment model. Click Create.

    Screenshot that shows the Azure VM images available in the portal

1. Basics blade

The Basics blade requests administrative information for the virtual machine.

  1. Enter a Name for the virtual machine. In the example, HeroVM is the name of the virtual machine. The name must be 1-15 characters long and it cannot contain special characters.

  2. Enter a User name and a strong Password that are used to create a local account on the VM. The local account is used to sign in to and manage the VM. In the example, azureuser is the user name.

    The password must be 8-123 characters long and meet three out of the four following complexity requirements: one lower case character, one upper case character, one number, and one special character. See more about username and password requirements.

  3. The Subscription is optional. One common setting is "Pay-As-You-Go".

  4. Select an existing Resource group or type the name for a new one. In the example, HeroVMRG is the name of the resource group.

  5. Select an Azure datacenter Location where you want the VM to run. In the example, East US is the location.

  6. When you are done, click Next to continue to the next blade.

    Screenshot that shows the settings on the Basics blade for configuring an Azure VM

2. Size blade

The Size blade identifies the configuration details of the VM, and lists various choices that include OS, number of processors, disk storage type, and estimated monthly usage costs.

Choose a VM size, and then click Select to continue. In this example, DS1_V2 Standard is the VM size.

Screenshot of the Size blade that shows the Azure VM sizes that you can select

3. Settings blade

The Settings blade requests storage and network options. You can accept the default settings. Azure creates appropriate entries where necessary.

If you selected a virtual machine size that supports it, you can try Azure Premium Storage by selecting Premium (SSD) in Disk type.

When you're done making changes, click OK.

4. Summary blade

The Summary blade lists the settings specified in the previous blades. Click OK when you're ready to make the image.

Summary blade report giving specified settings of the virtual machine

After the virtual machine is created, the portal lists the new virtual machine under All resources, and displays a tile of the virtual machine on the dashboard. The corresponding cloud service and storage account also are created and listed. Both the virtual machine and cloud service are started automatically and their status is listed as Running.

Configure VM Agent and the endpoints of the virtual machine


You can add an endpoint for MongoDB while creating the virtual machine, and configure it as follows: name it as Mongo, use TCP as the protocol, and set both the public and private ports to 27017.

Attach a data disk

To provide storage for the virtual machine, attach a data disk and then initialize it so that Windows can use it. If you already have a data disk, you can attach that existing disk, or you can attach an empty disk.

Attach an empty disk

Attaching an empty disk is a simple way to add a data disk, because Azure creates the .vhd file for you and stores it in the storage account.

  1. Click Virtual Machines, and then select the appropriate VM.
  2. On the command bar, click Attach, and then click Attach Empty Disk.

    Attach an empty disk

  3. The Attach an Empty Disk dialog box appears.

    Attach a new empty disk

    Perform the following steps:

    • In File Name, accept the default name or type another one for the .vhd file. The data disk uses an automatically generated name, even if you type another name for the .vhd file.
    • Type the Size (GB) of the data disk.
    • Click the check mark to finish.
  4. After the data disk is created and attached, it's listed in the dashboard of the VM.

    Empty data disk successfully attached


After you add a data disk, you need to log on to the VM and initialize the disk so that it can be used.

How to: Attach an existing disk

Attaching an existing disk requires that you have a .vhd available in a storage account. Use the Add-AzureVhd cmdlet to upload the .vhd file to the storage account. After you've created and uploaded the .vhd file, you can attach it to a VM.

  1. Click Virtual Machines, and then select the appropriate virtual machine.
  2. On the command bar, click Attach, and then select Attach Disk.

    Attach data disk

  3. Select the data disk and then click the check mark to attach the data disk.

    Enter data disk details

  4. After the data disk is attached, it's listed in the dashboard of the VM.

    Data disk successfully attached

For instructions on initializing the disk, see "How to: Initialize a new data disk in Windows Server" in How to attach a data disk to a Windows virtual machine.

Install and run MongoDB on the virtual machine

Follow these steps to install and run MongoDB on a virtual machine running Windows Server.


MongoDB security features, such as authentication and IP address binding, are not enabled by default. Security features should be enabled before deploying MongoDB to a production environment. For more information, see Security and Authentication.

  1. After you've connected to the virtual machine using Remote Desktop, open Internet Explorer from the Start menu on the virtual machine.
  2. Select the Tools button in the upper right corner. In Internet Options, select the Security tab, and then select the Trusted Sites icon, and finally click the Sites button. Add https://\* to the list of trusted sites.
  3. Go to Downloads - MongoDB.
  4. Find the Current Stable Release of Community Server, select the latest 64-bit version in the Windows column. Download, then run the MSI installer.
  5. MongoDB is typically installed in C:\Program Files\MongoDB. Search for Environment Variables on the desktop and add the MongoDB binaries path to the PATH variable. For example, you might find the binaries at C:\Program Files\MongoDB\Server\3.2\bin on your machine.
  6. Create MongoDB data and log directories in the data disk (drive F:, for example) you created in the preceding steps. From Start, select Command Prompt to open a command prompt window. Type:

     C:\> F:
     F:\> mkdir \MongoData
     F:\> mkdir \MongoLogs
  7. To run the database, run:

     F:\> C:
     C:\> mongod --dbpath F:\MongoData\ --logpath F:\MongoLogs\mongolog.log

    All log messages are directed to the F:\MongoLogs\mongolog.log file as mongod.exe server starts and preallocates journal files. It may take several minutes for MongoDB to preallocate the journal files and start listening for connections. The command prompt stays focused on this task while your MongoDB instance is running.

  8. To start the MongoDB administrative shell, open another command window from Start and type the following commands:

     C:\> cd \my_mongo_dir\bin  
     C:\my_mongo_dir\bin> mongo  
     > { a : 1 } )  
     { _id : ..., a : 1 }  
     > show dbs  
     > show collections  
     > help  

    The database is created by the insert.

  9. Alternatively, you can install mongod.exe as a service:

     C:\> mongod --dbpath F:\MongoData\ --logpath F:\MongoLogs\mongolog.log --logappend  --install

    A service is installed named MongoDB with a description of "Mongo DB". The --logpath option must be used to specify a log file, since the running service does not have a command window to display output. The --logappend option specifies that a restart of the service causes output to append to the existing log file. The --dbpath option specifies the location of the data directory. For more service-related command line options, see Service-related command line options.

    To start the service, run this command:

     C:\> net start MongoDB
  10. Now that MongoDB is installed and running, you need to open a port in Windows Firewall so you can remotely connect to MongoDB. From the Start menu, select Administrative Tools and then Windows Firewall with Advanced Security.
  11. a) In the left pane, select Inbound Rules. In the Actions pane on the right, select New Rule....

    Windows Firewall

    b) In the New Inbound Rule Wizard, select Port and then click Next.

    Windows Firewall

    c) Select TCP and then Specific local ports. Specify a port of "27017" (the default port MongoDB listens on) and click Next.

    Windows Firewall

    d) Select Allow the connection and click Next.

    Windows Firewall

    e) Click Next again.

    Windows Firewall

    f) Specify a name for the rule, such as "MongoPort", and click Finish.

    Windows Firewall

  12. If you didn't configure an endpoint for MongoDB when you created the virtual machine, you can do it now. You need both the firewall rule and the endpoint to be able to connect to MongoDB remotely. In the Management Portal, click Virtual Machines, click the name of your new virtual machine, and then click Endpoints.


  13. Click Add at the bottom of the page. Select Add a Stand-Alone Endpoint and click Next.


  14. Add an endpoint with name "Mongo", protocol TCP, and both Public and Private ports set to "27017". Opening this port allows MongoDB to be accessed remotely.



The port 27017 is the default port used by MongoDB. You can change this default port by specifying the --port parameter when starting the mongod.exe server. Make sure to give the same port number in the firewall and the "Mongo" endpoint in the preceding instructions.


In this tutorial, you learned how to create a virtual machine running Windows Server, remotely connect to it, and attach a data disk. You also learned how to install and configure MongoDB on the Windows-based virtual machine. You can now access MongoDB on the Windows-based virtual machine, by following the advanced topics in the MongoDB documentation.