How to Install MongoDB on an Alibaba Cloud ECS Instance

A Bit about MongoDB

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

Prerequisites

  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.

Procedure

Logging on to Your Console

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

Installing MongoDB

Adding the MongoDB Repository and its Public Key

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
Output gpg: Total number processed: 1 gpg:  imported: 1  (RSA: 1)
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

Installing and Verifying MongoDB

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
cd /lob/system/system vim mongod.service
[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
  • 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.
sudo systemctl start mongod sudo systemctl enable mongod
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

Configuring and Connecting MongoDB

Opening Mongo Shell

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

Enabling mongodb authentication

vim /lib/system/system/mangod.service
ExecStart=/usr/bin/mongod --quiet --auth --config /etc/mongod.conf
systemd daemon-reload

Connecting MongoDB

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

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
sudo rm /etc/apt/sources.list.d/mongodb*.list
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv E52529D4
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'
sudo apt update sudo apt install mongodb-org
systemctl enable mongod.service systemctl start mongod.service
~$ 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
~$ 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

Remove packages

> sudo rm -r /var/log/mongodb > sudo rm -r /var/lib/mongodb

Summary

Original Source

--

--

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
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com