Free trial *Internet Service Required

Install MongoDB on a virtual machine running Windows Server in Windows Azure

MongoDB is a popular open source, high performance NoSQL database. Using the Windows Azure Management Portal, you can create a virtual machine running Windows Server from the Image Gallery. You can then install and configure a MongoDB database on the virtual machine.

In this tutorial, you will learn:

  • How to use the Management Portal to create a Windows Server virtual machine from the gallery.
  • How to connect to the virtual machine using Remote Desktop.
  • How to install MongoDB on the virtual machine.

Create a virtual machine running Windows Server 2008 R2

Note: This article creates a virtual machine that is not connected to a virtual network. If you want your virtual machine to use a virtual network so you can connect to your virtual machines directly by hostname or set up cross-premises connections, use the From Gallery method instead and specify the virtual network when you create the virtual machine. For more information about virtual networks, see Windows Azure Virtual Network Overview.

Follow these steps to create a virtual machine:

  1. Login to the Windows Azure Management Portal using your Windows Azure account.

  2. In the Management Portal, at the bottom left of the web page, click +New, click Virtual Machine, and then click From Gallery. Create a New Virtual Machine

  3. Select a Windows Server 2008 R2 SP1 virtual machine image, and then click the next arrow at the bottom right of the page.

  4. On the Virtual machine configuration page, provide the following information:

    • Provide a Virtual Machine Name, such as "testwinvm".
    • In the New User Name box, type "Administrator".
    • In the New Password box, type a strong password.
    • In the Confirm Password box, retype the password.
    • Select the appropriate Size from the drop down list.

    Click the next arrow to continue.

  5. On the Virtual machine mode page, provide the following information:

    • Select Standalone Virtual Machine.
    • In the DNS Name box, type a valid sub-domain in the format testwinvm.cloudapp.net
    • In the Region/Affinity Group/Virtual Network box, select a region where this virtual image will be hosted.

    Click the next arrow to continue.

  6. On the Virtual machine options page, select (none) in the Availability Set box. Click the check mark to continue.

  7. Wait while Windows Azure prepares your virtual machine.

Attach a data disk

Follow these steps to attach a data disk:

  1. In the Windows Azure Management Portal, click Virtual Machines and then select the virtual machine you just created (testwinvm).

  2. On the command bar click Attach and then click Attach Empty Disk.

    The Attach empty disk to virtual machine dialog box appears.

  3. The Virtual Machine Name, Storage Location, and File Name are already defined for you. All you have to do is enter the size that you want for the disk. Type 5 in the Size field.

    Attach Empty Disk

    Note: All disks are created from a VHD file in Windows Azure storage. You can provide a name for the VHD file that you add to storage, but Windows Azure generates the name of the disk automatically.

  4. Click the check mark to attach the data disk to the virtual machine.

  5. Click the name of the virtual machine to display the dashboard; this lets you verify that the data disk was successfully attached to the virtual machine.

    The number of disks is now 2 for the virtual machine. The disk that you attached is listed in the Disks table.

    Attach Empty Disk

    After you attach the data disk to the virtual machine, the disk is offline and not initialized. You have to log on to the virtual machine and initialize the disk before you can use it to store data.

Connect to the Virtual Machine Using Remote Desktop and Complete Setup

  1. After the virtual machine is provisioned, on the Management Portal, click Virtual Machines, and then click your new virtual machine. Information about your virtual machine is presented.

  2. At the bottom of the page, click Connect. Open the .rpd file using the Windows Remote Desktop program (%windir%\system32\mstsc.exe).

  3. At the Windows Security dialog box, provide the password for the Administrator account. (You might be asked to verify the credentials of the virtual machine.) The first time you log on to this virtual machine, several processes may need to complete, including setup of your desktop, Windows updates, and completion of the Windows initial configuration tasks. Once you are connected to the virtual machine with Windows Remote Desktop, the virtual machine works like any other computer.

  4. After you log on to the virtual machine, open Server Manager. In the left pane, expand Storage, and then click Disk Management.

    Server Manager

  5. The Initalize Disk window appears. Click OK.

    Initialize Disk

  6. Right-click the space allocation area for Disk 2, click New Simple Volume, and then finish the wizard with the default values.

    New Simple Volume

    The disk is now online and ready to use with a new drive letter.

    Initialize Success

Install and run MongoDB on the virtual machine

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

Warning

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. See Security and Authentication for more information.

  1. After you've connected to the virtual machine using Remote Desktop, open Internet Explorer from the Start menu.
  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 http://*.mongodb.org to the list of trusted sites.
  3. Go to Downloads- MongoDB.
  4. Find the most recent release in the Production Release (Recommended) section and click the *2008+ link in the Windows 64-bit column. Click Save As and save the zip file to the desktop.
  5. Right-click on the zip file and select Extract All... Specify "C:\" and click Extract. After the files have been extracted, you may wish to rename the install folder to something simpler. "MongoDB", for example.
  6. Create MongoDB data and log directories in the data disk (drive F:, for example) you created in the steps above. From Start, select Command Prompt to open a command prompt window. Enter:

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

    F:\> C:
    C:\> cd \MongoDB\bin
    C:\my_mongo_dir\bin> mongod --dbpath F:\MongoData\ --logpath F:\MongoLogs\mongolog.log

    All log messages will be 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.

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

    C:\> cd \my_mongo_dir\bin  
    C:\my_mongo_dir\bin> mongo  
    >db  
    test      
    > db.foo.insert( { a : 1 } )  
    > db.foo.find()  
    { _id : ..., a : 1 }  
    > show dbs  
    ...  
    > show collections  
    ...  
    > help

    The database is created by the insert.

  9. (Optional) mongod.exe has support for installing and running as a Windows service. To install mongod.exe as a service, run the following from the command prompt:

    C:\mongodb\bin>mongod --logpath "c:\mongodb\logs\logfile.log" --logappend --dbpath "c:\data" --install

    This creates a service named "Mongo DB" with a description of "Mongo DB". The --logpath option must be used to specify a log file, since the running service will not have a command window to display output. The --logpath option specifies that a restart of the service will cause 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.

  10. Now that MongoDB is installed and running you must open a port in Windows Firewall in order to remotely connect to MongoDB. From the Start menu, select Administrator Tools and then Windows Firewall with Advanced Security.

  11. In the left pane, select Inbound Rules. In the Actions pane on the right, select New Rule....

    Windows Firewall

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

    Windows Firewall

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

    Windows Firewall

    Select Allow the connection and click Next.

    Windows Firewall

    Click Next again.

    Windows Firewall

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

    Windows Firewall

  12. Once MongoDB is installed you must configure an endpoint so that MongoDB can be accessed remotely. In the Management Portal, click Virtual Machines, then click the name of your new virtual machine, then click Endpoints.

    Endpoints

  13. Click Add Endpoint at the bottom of the page. Select Add Endpoint and click Next.

    Endpoints

  14. Add an endpoint with name "Mongo", protocol TCP, and both Public and Private ports set to "27017". This will allow MongoDB to be accessed remotely.

    Endpoints

Summary

In this tutorial you learned how to create a Windows Server virtual machine and remotely connect to it. You also learned how to install and configure MongoDB on the Windows virtual machine. For more information on MongoDB, see the MongoDB Documentation.

Rss Newsletter