By Ghulam Qadir, Alibaba Cloud Community Blog author.
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.
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.
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.
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:
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:
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.
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:
[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual
[Service]
User=mongodb
Group=mongodb
ExecStart=/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:
network.target
.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 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
.
When the MongoDB packages are installed you can configure username and password for the database server:
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
mango
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.
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
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.
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
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.
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.
How to Install and Configure VNC on an Alibaba Cloud ECS Instance
2,599 posts | 762 followers
FollowAlibaba Clouder - February 25, 2020
Hiteshjethva - December 12, 2019
Alibaba Clouder - January 4, 2019
ApsaraDB - November 1, 2021
ApsaraDB - December 15, 2021
ApsaraDB - October 28, 2021
2,599 posts | 762 followers
FollowElastic and secure virtual cloud servers to cater all your cloud hosting needs.
Learn MoreCloud-based and lightweight servers that are easy to set up and manage
Learn MoreLearn More
More Posts by Alibaba Clouder