How to install MongoDB on Ubuntu16
MongoDB is a developer and operations-friendly NoSQL database. Its schemaless nature makes getting started simple, as there is no need to establish a data model before persisting documents. Use of a modified JSON dialect speaks a language with which most web developers are familiar. When it is time to scale out, a few simple commands are all that is necessary to create a database cluster, and to distribute documents using a variety of heuristics.
Getting Started
To complete this guide, you will need the following:
• 1 Node (Cloud Server or Dedicated Server) running Ubuntu 16.
• Root Access to your server
When complete, you’ll have a single-node Mongo instance that can either be used directly, or integrated into a broader cluster.
Tutorial
Start by updating the list of packages, and by applying all current bugfixes and security updates. This step should be performed regularly to keep your system running smoothly and securely.
apt-get update && apt-get upgrade
MongoDB is shipped in its own official package repository. To install it, we’ll integrate this repository into our system in several steps. We begin by fetching the official GPG keys, which are necessary to ensure that the downloaded packages are correctly signed.
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
Now we add the repository itself directly into sources.list.
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
The Ubuntu package system does not yet know what packages are available in the repository we’ve just added. In order to fix this, we’ll once again update the packages list.
apt-get update
Having fetched the list of available packages, we’ll install the MongoDB package itself.
apt-get install -y --allow-unauthenticated mongodb-org
Ubuntu uses Systemd, and we’ll need to create a script so that the init system integrates with MongoDB.
nano /etc/systemd/system/mongodb.service
Add this block of text.
[Unit]
Description=High-performance, 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
With the unit in place, MongoDB must now be started. It should also be enabled to start on boot, so it is available when the server restarts.
systemctl start mongodb.service
systemctl enable mongodb.service
Let’s ensure that Mongo is in fact running. Use this command to check its status. If it failed to start, you should see any relevant failure messages in the displayed logs.
systemctl status mongodb
? mongodb.service - High-performance, schema-free document-oriented database
Loaded: loaded (/etc/systemd/system/mongodb.service; disabled; vendor preset: enabled)
Active: active (running) since Fri 2016-06-24 09:11:53 UTC; 9min ago
Main PID: 15300 (mongod)
Tasks: 16
Memory: 86.6M
CPU: 3.441s
CGroup: /system.slice/mongodb.service
??15300 /usr/bin/mongod --quiet --config /etc/mongod.conf
Now we’ll query Mongo itself for statistics. This will ensure that the database is operating normally.
Mongostat
insert query update delete getmore command % dirty % used flushes vsize res qr|qw ar|aw netIn netOut conn time
*0 *0 *0 *0 0 1|0 0.0 0.0 0 250.0M 87.0M 0|0 0|0 79b 18k 1 2016-06-24T09:23:57Z
*0 *0 *0 *0 0 1|0 0.0 0.0 0 250.0M 87.0M 0|0 0|0 79b 18k 1 2016-06-24T09:23:58Z
*0 *0 *0 *0 0 1|0 0.0 0.0 0 250.0M 87.0M 0|0 0|0 79b 18k 1 2016-06-24T09:23:59Z
*0 *0 *0 *0 0 1|0 0.0 0.0 0 250.0M 87.0M 0|0 0|0 79b 18k 1 2016-06-24T09:24:00Z
*0 *0 *0 *0 0 1|0 0.0 0.0 0 250.0M 87.0M 0|0 0|0 79b 18k 1 2016-06-24T09:24:01Z
*0 *0 *0 *0 0 1|0 0.0 0.0 0 250.0M 87.0M 0|0 0|0 79b 18k 1 2016-06-24T09:24:02Z
*0 *0 *0 *0 0 1|0 0.0 0.0 0 250.0M 87.0M 0|0 0|0 79b 18k 1 2016-06-24T09:24:03Z
*0 *0 *0 *0 0 1|0 0.0 0.0 0 250.0M 87.0M 0|0 0|0 79b 18k 1 2016-06-24T09:24:04Z
*0 *0 *0 *0 0 1|0 0.0 0.0 0 250.0M 87.0M 0|0 0|0 79b 18k 1 2016-06-24T09:24:05Z
*0 *0 *0 *0 0 1|0 0.0 0.0 0 250.0M 87.0M 0|0 0|0 79b 18k 1 2016-06-24T09:24:06Z
insert query update delete getmore command % dirty % used flushes vsize res qr|qw ar|aw netIn netOut conn time
Conclusion
Mongo is now installed, running, and configured to launch on boot. The package repository is also integrated, making it easy to install future upgrades. With MongoDB in place, you can now develop against it directly, or perform this guide on another server to build a cluster. If this guide was helpful to you, kindly share it with others who may also be interested.