How to Install MongoDB on Ubuntu 16.04
MongoDB is an open source database that uses a document-oriented data model.
MongoDB is one of several database types to arise in the mid-2000s under the NoSQL banner. Instead of using tables and rows as in relational databases, MongoDB is built on an architecture of collections and documents. Documents comprise sets of key-value pairs and are the basic unit of data in MongoDB. Collections contain sets of documents and function as the equivalent of relational database tables.
Step — 1 Import the public key using package management system
Ubuntu ensures package consistency and authenticity by requiring the distributors sign packages with GPG keys. Importing MongoDB public GPG Key
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
Output:
Executing: /tmp/tmp.1mexQp9rvt/gpg.1.sh --keyserver
hkp://keyserver.ubuntu.com:80
--recv
EA312927
gpg: requesting key EA312927 from hkp server keyserver.ubuntu.com
gpg: key EA312927: public key "MongoDB 3.2 Release Signing Key <packaging@mongodb.com>" imported
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)
Step — 2 Adding MongoDB Repository details
Create the /etc/apt/sources.list.d/mongodb-org-3.2.list list file using following command
echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
Updating the packages list
sudo apt-get update
Step — 3 Installing the MongoDB packages
sudo apt-get install -y mongodb-org
Output:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
The following NEW packages will be installed:
mongodb-org mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
0 upgraded, 5 newly installed, 0 to remove and 218 not upgraded.
Need to get 51.9 MB of archives.
After this operation, 232 MB of additional disk space will be used...........................................
Adding group `mongodb' (GID 130) ...
Done.
Adding user `mongodb' to group `mongodb' ...
Adding user mongodb to group mongodb
Done.
Setting up mongodb-org-mongos (3.2.7) ...
Setting up mongodb-org-tools (3.2.7) ...
Setting up mongodb-org (3.2.7) ...
Processing triggers for ureadahead (0.100.0-19) ...
Step — 4 Configuring MongoDB for Ubuntu 16.04
In order to launch MongoDB as a service on Ubuntu 16.04, we need to create a unit file.
sudo nano /etc/systemd/system/mongodb.service
Paste the below code and save it
[Unit]
Description=MongoDB, schema-free document-oriented database
After=network.target
[Service]
User=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
[Install]
WantedBy=multi-user.target
Now starting the newly created service
sudo systemctl start mongodb
There is no output of above command so checking status of service using below command
sudo systemctl status mongodb
Output:
● mongodb.service - MongoDB, schema-free document-oriented database
Loaded: loaded (/etc/systemd/system/mongodb.service; disabled; vendor preset: enabled)
Active: active (running) since Sat 2016-07-09 19:40:13 IST; 12s ago
Main PID: 7462 (mongod)
CGroup: /system.slice/mongodb.service
└─7462 /usr/bin/mongod --quiet --config /etc/mongod.conf
Jul 09 19:40:13 linux systemd[1]: Started MongoDB, schema-free document-oriented database.
Above output shows that the MongoDB service has started successfully.
With above settings, MongoDB does’nt start on system startup, so enabling it on system startup by below command
sudo systemctl enable mongodb
Output:
Created symlink from /etc/systemd/system/multi-user.target.wants/mongodb.service to /etc/systemd/system/mongodb.service.
The MongoDB server is installed and running.
To manage MongoDB server use below commands
sudo systemctl mongodb start
sudo systemctl mongodb stop