Share via


How to install and configure MongoDB on a Linux VM

MongoDB is a popular open-source, high-performance NoSQL database. This article shows you how to install and configure MongoDB on a Linux VM with the Azure CLI. Examples are shown that detail how to:

Manually install and configure MongoDB on a VM

MongoDB provide installation instructions for Linux distros including Red Hat / CentOS, SUSE, Ubuntu, and Debian. The following example creates a CentOS VM. To create this environment, you need the latest Azure CLI installed and logged in to an Azure account using az login.

Create a resource group with az group create. The following example creates a resource group named myResourceGroup in the eastus location:

az group create --name myResourceGroup --location eastus

Create a VM with az vm create. The following example creates a VM named myVM with a user named azureuser using SSH public key authentication

az vm create \
    --resource-group myResourceGroup \
    --name myVM \
    --image CentOS \
    --admin-username azureuser \
    --generate-ssh-keys

SSH to the VM using your own username and the publicIpAddress listed in the output from the previous step:

ssh azureuser@<publicIpAddress>

To add the installation sources for MongoDB, create a yum repository file as follows:

sudo touch /etc/yum.repos.d/mongodb-org-3.6.repo

Open the MongoDB repo file for editing, such as with vi or nano. Add the following lines:

[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc

Install MongoDB using yum as follows:

sudo yum install -y mongodb-org

By default, SELinux is enforced on CentOS images that prevents you from accessing MongoDB. Install policy management tools and configure SELinux to allow MongoDB to operate on its default TCP port 27017 as follows:

sudo yum install -y policycoreutils-python
sudo semanage port -a -t mongod_port_t -p tcp 27017

Start the MongoDB service as follows:

sudo service mongod start

Verify the MongoDB installation by connecting using the local mongo client:

mongo

Now test the MongoDB instance by adding some data and then searching:

> db
test
> db.foo.insert( { a : 1 } )  
> db.foo.find()  
{ "_id" : ObjectId("57ec477cd639891710b90727"), "a" : 1 }
> exit

If desired, configure MongoDB to start automatically during a system reboot:

sudo chkconfig mongod on

Create basic MongoDB instance on CentOS using a template

You can create a basic MongoDB instance on a single CentOS VM using the following Azure quickstart template from GitHub. This template uses the Custom Script extension for Linux to add a yum repository to your newly created CentOS VM and then install MongoDB.

To create this environment, you need the latest Azure CLI installed and logged in to an Azure account using az login. First, create a resource group with az group create. The following example creates a resource group named myResourceGroup in the eastus location:

az group create --name myResourceGroup --location eastus

Next, deploy the MongoDB template with az deployment group create. When prompted, enter your own unique values for newStorageAccountName, dnsNameForPublicIP, and admin username and password:

az deployment group create --resource-group myResourceGroup \
  --template-uri https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/mongodb-on-centos/azuredeploy.json

Log on to the VM using the public DNS address of your VM. You can view the public DNS address with az vm show:

az vm show -g myResourceGroup -n myLinuxVM -d --query [fqdns] -o tsv

SSH to your VM using your own username and public DNS address:

ssh azureuser@mypublicdns.eastus.cloudapp.azure.com

Verify the MongoDB installation by connecting using the local mongo client as follows:

mongo

Now test the instance by adding some data and searching as follows:

> db
test
> db.foo.insert( { a : 1 } )  
> db.foo.find()  
{ "_id" : ObjectId("57ec477cd639891710b90727"), "a" : 1 }
> exit

Create a complex MongoDB Sharded Cluster on CentOS using a template

You can create a complex MongoDB sharded cluster using the following Azure quickstart template from GitHub. This template follows the MongoDB sharded cluster best practices to provide redundancy and high availability. The template creates two shards, with three nodes in each replica set. One config server replica set with three nodes is also created, plus two mongos router servers to provide consistency to applications from across the shards.

Warning

Deploying this complex MongoDB sharded cluster requires more than 20 cores, which is typically the default core count per region for a subscription. Open an Azure support request to increase your core count.

To create this environment, you need the latest Azure CLI installed and logged in to an Azure account using az login. First, create a resource group with az group create. The following example creates a resource group named myResourceGroup in the eastus location:

az group create --name myResourceGroup --location eastus

Next, deploy the MongoDB template with az deployment group create. Define your own resource names and sizes where needed such as for mongoAdminUsername, sizeOfDataDiskInGB, and configNodeVmSize:

az deployment group create --resource-group myResourceGroup \
  --parameters '{"adminUsername": {"value": "azureuser"},
    "adminPassword": {"value": "P@ssw0rd!"},
    "mongoAdminUsername": {"value": "mongoadmin"},
    "mongoAdminPassword": {"value": "P@ssw0rd!"},
    "dnsNamePrefix": {"value": "mypublicdns"},
    "environment": {"value": "AzureCloud"},
    "numDataDisks": {"value": "4"},
    "sizeOfDataDiskInGB": {"value": 20},
    "centOsVersion": {"value": "7.0"},
    "routerNodeVmSize": {"value": "Standard_DS3_v2"},
    "configNodeVmSize": {"value": "Standard_DS3_v2"},
    "replicaNodeVmSize": {"value": "Standard_DS3_v2"},
    "zabbixServerIPAddress": {"value": "Null"}}' \
  --template-uri https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/mongodb-sharding-centos/azuredeploy.json \
  --name myMongoDBCluster \
  --no-wait

This deployment can take over an hour to deploy and configure all the VM instances. The --no-wait flag is used at the end of the preceding command to return control to the command prompt once the template deployment has been accepted by the Azure platform. You can then view the deployment status with az deployment group show. The following example views the status for the myMongoDBCluster deployment in the myResourceGroup resource group:

az deployment group show \
    --resource-group myResourceGroup \
    --name myMongoDBCluster \
    --query [properties.provisioningState] \
    --output tsv

Next steps

In these examples, you connect to the MongoDB instance locally from the VM. If you want to connect to the MongoDB instance from another VM or network, ensure the appropriate Network Security Group rules are created.

These examples deploy the core MongoDB environment for development purposes. Apply the required security configuration options for your environment. For more information, see the MongoDB security docs.

For more information about creating using templates, see the Azure Resource Manager overview.

The Azure Resource Manager templates use the Custom Script Extension to download and execute scripts on your VMs. For more information, see Using the Azure Custom Script Extension with Linux Virtual Machines.