How to Install MongoDB on an Alibaba Cloud ECS Instance

Image for post
Image for post

By Ghulam Qadir, Alibaba Cloud Community Blog author.

A Bit about MongoDB

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

  • 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

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

Prerequisites

  1. You should setup your server’s hostname.
  2. You have access to the VNC console in your Alibaba Cloud or the SSH client installed in your PC.

Procedure

Logging on to Your Console

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

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:

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

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:

[Unit]Description=High-performance, schema-free document-oriented databaseAfter=network.targetDocumentation=https://docs.mongodb.org/manual[Service]User=mongodbGroup=mongodbExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf[Install]WantedBy=multi-user.target

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:

Output  ● 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 mongodbcommand. 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 mongodbcommand, and to stop MongoDB, run sudo systemctl stop mongodb, and last to restart MongoDB, use sudo systemctl restart mongodb.

Configuring and Connecting MongoDB

Opening Mongo Shell

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 mango

After you are in the MongoDB Shell, switch to the database named admin by running the > use admincommand, 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

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

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

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

Troubleshooting MongoDB

gqadir@testserv~$ sudo apt-get install mongodb-orgReading package lists... DoneBuilding dependency tree Reading state information... DonePackage 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 --versionMongoDB shell version v4.0.5git version: 3739429dd92b92d1b0ab120911a23d50bf03c412OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016allocator: tcmallocmodules: nonebuild 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

> 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.

Summary

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.

Original Source

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store