Community Blog How to Install MongoDB on an Alibaba Cloud ECS Instance

How to Install MongoDB on an Alibaba Cloud ECS Instance

In this tutorial, you will learn a bit about MongoDB and how you can install and use MongoDB on an Alibaba Cloud ECS instance installed with Ubuntu 16.

By Ghulam Qadir, Alibaba Cloud Community Blog author.

A Bit about MongoDB

MongoDB is a NoSQL database that promises better performance and availability than the SQL databases, and also offers the automatic scaling capabilities for enterprise-level databases.

Because MongoDB is a NoSQL database, you do not use SQL (Structured Query Language) to insert or retrieve data into the MongoDB databases, and MongoDB also does not store data in tables like MySQL or Postgres. Rather, data is stored in a document structure in the JSON format (which for MongoDB is usually BSON). MongoDB was first introduced in 2009 and is continuously developed by the company MongoDB, Inc.

Generally speaking, MongoDB offers organizations great scalability and performance for building modern applications along with powerful and mission-critical databases.

Some of the Advantages of MongoDB

  • Best way to work with data: MongoDB's flexible document data model makes working with data more intuitive, no matter whether you're building an app from scratch or updating an existing one.
  • Put data where you need it: MongoDB lets you control where data is placed globally in a premise way so that you can ensure fast performance anywhere as well as compliance with regulations more easily.
  • Run anywhere: Complete deployment flexibility with seamless data migrations, and also build a ubiquitous data layer from mainframes to the public cloud and everywhere in between.

Using MongoDB on Alibaba Cloud

MongoDB is compatible and works well with Alibaba Cloud Elastic Compute Service (ECS) instances. As a developer myself, I prefer using the services of Alibaba Cloud together with MongoDB given ECS's flexibility and the ability to offers you to upgrade your hardware resources anytime.

Now let's proceed to how you can get MongoDB working on your ECS instance, installed with Ubuntu 16.04 LTS.


  1. You must have an Alibaba Cloud Elastic Compute Service (ECS) activated. If you are a new user or new to ECS, you can get Free Credits in your Alibaba Cloud account. If you don't know about how to setup your ECS instance, you can refer to this tutorial or check quick-start guide.
  2. You should setup your server's hostname.
  3. You have access to the VNC console in your Alibaba Cloud or the SSH client installed in your PC.


Follow the steps outlined below in the next sections to install and use MongoDB on your Alibaba Cloud instance installed with Ubuntu 16.04 LTS.

Logging on to Your Console

Log on as the root user with your root username and password by using an SSH client (such as Putty) or the VNC console available in your Alibaba Cloud account dashboard.

To complete this tutorial, again, you'll need:

  • An ECS instance installed with Ubuntu 16.04 LTS.
  • Root Privileges

Installing MongoDB

Adding the MongoDB Repository and its Public Key

For this, you're going to install the latest version of MongoDB, so you'll need to add the official MongoDB repository, which has the latest package version. Ubuntu ensures the authenticity of software packages by verifying that they are signed with GPG keys, so we first have to import they key for the official MongoDB repository.

Run this command to import MongoDB keys to your server. First, open the file in your text editor:

 sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927

After importing the key, you will see the following output:


 gpg: Total number processed: 1

 gpg:  imported: 1  (RSA: 1)

Now, you'll have to add a file containing the MongoDB repository details so that it'll know where to download the packages from.

Issue the following command to create the /etc/apt/sources.list.d/mongodb-org-3.2.list file for MongoDB. That is, enter the following:

 echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

After adding the repository details, we need to update the packages list. You can do so by running the sudo apt-get update command.

Installing and Verifying MongoDB

Now you can install the MongoDB package by entering the sudo apt-get install -y mongodb-org command. This command installs several packages, which happen to contain the latest stable version of MongoDB along with helpful management tools for the MongoDB server.

If you want to install a specific release of MongoDB then must specify each component package specifically with their version number, check the following example:

 sudo apt-get install -y mongodb-org=3.4 mongodb-org-server=3.4 mongodb-org-shell=3.4 mongodb-org-mongos=3.4 mongodb-org-tools=3.4

Now you have to create a new MongoDB systemd service file in the /lib/systemd/system directory. Go to that directory and create the new MongoDB service file mongod.service with vim editor.

 cd /lob/system/system

 vim mongod.service

Copy the following code script shown here:


Description=High-performance, schema-free document-oriented database






ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf



Save this file and exit.

In the above, the sections Unit, Service, and Install have the following functions:

  • Unit: Contains a description of the service and dependencies that need to be started for MongoDB service to work as required. The dependency here is the network.target.
  • Service: Specifies how the system should start a service depending on the directives provided. The User directive specifies user to run the MongoDB server and ExecStart directive specifies command to start MongoDB service.
  • Install: Directs systemd to start the service at boot time when required.

Now you can update the system service with the sudo systemctl daemon-reload command, and then start MongoDB with systemctl by entering the following:

 sudo systemctl start mongod

 sudo systemctl enable mongod

You can also use systemctl to check that the service has started properly with the sudo systemctl status mongod command. The output of which should be as follows:


  ● mongodb.service - High-performance, schema-free document-oriented database

 Loaded: loaded (/etc/systemd/system/mongodb.service; enabled; vendor preset: enabled)

 Active: active (running) since Mon 2018-10-23 14:57:20 EDT; 1min 30s ago

 Main PID: 4093 (mongod)

 Tasks: 16 (limit: 512)

 Memory: 47.1M

 CPU: 1.224s

 CGroup: /system.slice/mongodb.service

 └─4093 /usr/bin/mongod --quiet --config /etc/mongod.conf

The MongoDB server is now configured and running, and you can manage the MongoDB service using systemctl commands, such as sudo systemctl stop mongod to stop MongoDB and sudo systemctl start mongod to start MongoDB.

In terms of management and maintenance, you can update the systemd service with the sudo systemctl daemon-reload command. Next, you can check if mongodb has been started on port 27017 with the netstat –plntu command, and check if the service has started properly with the sudo systemctl status mongodb command. The output to this command should show active (running) status along with the PID and Memory/CPU usage.

To enable auto start MongoDB when system starts, you can run the sudo systemctl enable mongodb command, and to stop MongoDB, run sudo systemctl stop mongodb, and last to restart MongoDB, use sudo systemctl restart mongodb.

Configuring and Connecting MongoDB

When the MongoDB packages are installed you can configure username and password for the database server:

Opening Mongo Shell

Before you setup a username and password for MongoDB, you need to open MongoDB sheel on your server. Open MongoDB shell on your server by entering the > mango command.

If you get error Failed global initialization: BadValue Invalid or no user locale set. Please ensure LANG and/or LC_* environment variables are set correctly, try entering this instead:

 export LC_ALL=C


After you are in the MongoDB Shell, switch to the database named admin by running the > use admin command, and then create the root user by running this following command:

 > db.createUser({user:"admin", pwd:"admin123", roles:[{role:"root", db:"admin"}]})

Now you can exit from the MongoDB shell using exit command. See the explanation below.

Enabling mongodb authentication

Edit the mongodb service file /lib/systemd/system/mongod.service with your editor by running this command:

 vim /lib/system/system/mangod.service

On the ExecStart line 9, add the new option --auth by entering the following:

 ExecStart=/usr/bin/mongod --quiet --auth --config /etc/mongod.conf

Save the service file and exit vim, then reload the systemd service by running the following:

 systemd daemon-reload

Connecting MongoDB

Restart MongoDB and connect with user created with this command:

 > mongo -u admin -p admin123 --authenticationDatabase admin

You can see the mongo connecting. Check the databases using the > Show dbs command.

Troubleshooting MongoDB

If you are facing any issue while installing MongoDB such as apt-get fails, or you receive an error message like the one shown below:

gqadir@testserv~$ sudo apt-get install mongodb-org

Reading package lists... Done

Building dependency tree 

Reading state information... Done

Package mongodb-org is not available, but is referred to by another package.

This may mean that the package is missing, has been obsoleted, or is only available from another source E: Package 'mongodb-org' has no installation candidate

You can follow the steps below to no longer receive these error messages:

1.  Remove any existing repository file for MongoDB.

 sudo rm /etc/apt/sources.list.d/mongodb*.list

2.  Add the key because without the key, the repository will not load.

 sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv E52529D4

3.  Create a new MongoDB repository list file:

 sudo bash -c 'echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse" > /etc/apt/sources.list.d/mongodb-org-4.0.list'

4.  Complete the installation with update of repositories then install:

 sudo apt update

 sudo apt install mongodb-org

5.  Enable the mongod service and start it up:

 systemctl enable mongod.service

 systemctl start mongod.service

6.  Check your mongodb version:

 ~$ mongo --version

MongoDB shell version v4.0.5

git version: 3739429dd92b92d1b0ab120911a23d50bf03c412

OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016

allocator: tcmalloc

modules: none

build environment:

 distmod: ubuntu1604

 distarch: x86_64

 target_arch: x86_64

7.  Check if the service is running:

 ~$ systemctl status mongod.service

● mongod.service - MongoDB Database Server

 Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)

 Active: active (running) since Mon 2019-01-21 00:33:51 MST; 7s ago

 Docs: https://docs.mongodb.org/manual

 Main PID: 2906 (mongod)

 CGroup: /system.slice/mongod.service

 └─2906 /usr/bin/mongod --config /etc/mongod.conf

Uninstall MongoDB

Warning: All of your configurations and databases will be completely removed from the system when you uninstall MongoDB. The process is irreversible, so ensure that all of your configurations and data is backed up before proceeding with the steps given below.

Remove packages

Stop the service using the sudo systemctl stop mongodb command and run the sudo apt-get purge mongodb-org command to remove packages. Next, you can also use the following commands to remove data database directories and log files:

 > sudo rm -r /var/log/mongodb

 > sudo rm -r /var/lib/mongodb

All is now set for you to start using MongoDB for developing databases for your applications. Really, that's it. You have now learned how to install (and even uninstall) MongoDB on an ECS instance installed Ubuntu 16.04 LTS.


MongoDB can be summarized as a well-known NoSQL database that offers high performance, high availability, and automatic scaling. It is different from the RDBMS such as MySQL, PostgreSQL, and SQLite as it does not use SQL to set and retrieve data.

MongoDB stores data in documents called BSON (which is a binary representation of JSON with additional types of information). MongoDB is only available for 64-bit Long Term Support Ubuntu Release.

As an experienced developer in my opinion, Alibaba Cloud Elastic Compute Service (ECS) works well with MongoDB and can be a reliable and viable option for you to dabble with using MongoDB on the cloud.

0 0 0
Share on

Alibaba Clouder

2,600 posts | 750 followers

You may also like