Install MongoDB on a virtual machine running CentOS Linux 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 CentOS Linux from the Image Gallery. You can then install and configure a MongoDB database on the virtual machine.
You will learn:
Create a virtual machine running CentOS Linux
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.
- Sign in to the Windows Azure Management Portal using your Windows Azure account.
-
In the Management Portal, at the bottom left of the web page, click +New, click Virtual Machine, and then click From Gallery.
-
Select a CentOS virtual machine image from Platform Images, and then click the next arrow at the bottom right of the page.
-
On the Virtual machine configuration page, provide the following information:
- Provide a Virtual Machine Name, such as "testlinuxvm".
- Specify a New User Name, such as "newuser", which will be added to the Sudoers list file.
- 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.
-
On the Virtual machine mode page, provide the following information:
- Select Standalone Virtual Machine.
- In the DNS Name box, type a valid DNS address. For example, "testlinuxvm"
- In the Storage Account box, select Use an automatically generated storage account.
- In the Region/Affinity Group/Virtual Network box, select a region where this virtual image will be hosted.
Click the next arrow to continue.
-
On the Virtual machine options page, select (none) in the Availability Set box. Click the check mark to continue.
-
Wait while Windows Azure prepares your virtual machine.
Configure Endpoints
After the virtual machine is created, configure endpoints in order to remotely connect.
-
In the Management Portal, click Virtual Machines, click the name of your new virtual machine, then click Endpoints.
-
Click Edit Endpoint at the bottom of the page, and edit the SSH endpoint so that its Public Port is 22.
Connect to the Virtual Machine
When the virtual machine has been provisioned and the endpoints configured you can connect to it using SSH or PuTTY.
Connecting Using SSH
If you are using Linux, connect to the virtual machine using SSH. At the command prompt, run:
$ ssh newuser@testlinuxvm.cloudapp.net -o ServerAliveInterval=180
Enter the user's password.
Connecting using PuTTY
If you are using a Windows computer, connect to the VM using PuTTY. PuTTY can be downloaded from the PuTTY Download Page.
-
Download and save putty.exe to a directory on your computer. Open a command prompt, navigate to that folder, and execute putty.exe.
-
Enter "testlinuxvm.cloudapp.net" for the Host Name and "22" for the Port. 
Update the Virtual Machine (optional)
Once you've connected to the virtual machine, you can optionally install updates. Run:
$ sudo yum update
Enter the password again. Wait while updates install.
Attach a data disk
- In the Windows Azure (Preview) Management Portal, click Virtual Machines and then select the virtual machine you just created (testlinuxvm).
-
On the command bar click Attach and then click Attach Empty Disk.
The Attach Empty Disk dialog box appears.
-
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.
Note: All disks are created from a VHD file in Windows Azure storage. You can provide a name for the VHD file that is added to storage, but Windows Azure generates the name of the disk automatically.
-
Click the check mark to attach the data disk to the virtual machine.
-
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.
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 SSH or PuTTY and Complete Setup
The data disk that you just attached to the virtual machine is offline and not initialized after you add it. You must log on to the machine and initialize the disk to use it for storing data.
-
After the virtual machine is provisioned connect using SSH or PuTTY and login as newuser (as described in the steps above).
-
In the SSH or PuTTY window type the following command and then enter the account password:
$ sudo grep SCSI /var/log/messages
You can find the identifier of the last data disk that was added in the messages that are displayed (sdc, in this example).
-
In the SSH or PuTTY window, enter the following command to partition the disk /dev/sdc:
$ sudo fdisk /dev/sdc
-
Enter n to create a new partition.
-
Type p to make the partition the primary partition, type 1 to make it the first partition, and then type enter to accept the default value (1) for the cylinder.
-
Type p to see the details about the disk that is being partitioned.
-
Type w to write the settings for the disk.
-
Format the new disk using the mkfs.ext3 command:
$ sudo mkfs.ext3 /dev/sdc1
-
Create a directory to make a mount point for the drive:
$ sudo mkdir /mnt/datadrive
-
Mount the drive:
$ sudo mount /dev/sdc1 /mnt/datadrive
-
Open the /etc/fstab file and append the following line:
/dev/sdc1 /mnt/datadrive ext3 defaults 1 2
-
Save and close the /etc/fstab file.
-
Label the partition using e2label:
$ sudo e2label /dev/sdc1 /mnt/datadrive
Install and run MongoDB on the virtual machine
Follow these steps to install and run MongoDB on a virtual machine running CentOS Linux.
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.
-
Configure the Package Management System (YUM) so that you can install MongoDB. Create a /etc/yum.repos.d/10gen.repo file to hold information about your repository and add the following:
[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64
gpgcheck=0
enabled=1
-
Save the repo file and then run the following command to update the local package database:
$ sudo yum update
-
To install the package, run the following command to install the latest stable version of MongoDB and the associated tools:
$ sudo yum install mongo-10gen mongo-10gen-server
Wait while MongoDB downloads and installs.
-
Create a data directory. By default MongoDB stores data in the /data/db directory, but you must create that directory. To create it, run:
$ sudo mkdir -p /mnt/datadrive/data
$ sudo chown `id -u` /mnt/datadrive/data
For more information on installing MongoDB on Linux, see Quickstart Unix.
-
To start the database, run:
$ mongod --dbpath /mnt/datadrive/data --logpath /mnt/datadrive/data/mongod.log
All log messages will be directed to the /mnt/datadrive/data/mongod.log file as MongoDB server starts and preallocates journal files. It may take several minutes for MongoDB to preallocate the journal files and start listening for connections.
-
To start the MongoDB administrative shell, open a separate SSH or PuTTY window and run:
$ mongo
> db.foo.save ( { a:1 } )
> db.foo.find()
{ _id : ..., a : 1 }
> show dbs
...
> show collections
...
> help The database is created by the insert.
-
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.
-
Click Add Endpoint at the bottom of the page.
-
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.
Summary
In this tutorial you learned how to create a Linux virtual machine and remotely connect to it using SSH or PuTTY. You also learned how to install and configure MongoDB on the Linux virtual machine. For more information on MongoDB, see the MongoDB Documentation.