Install MongoDB on Ubuntu: 5 Easy Steps

on Data Integration, Database, Engineering, Tutorial • August 29th, 2017 • Write for Hevo

Do you want to Install MongoDB on Ubuntu? Are you struggling to find an in-depth guide to help you set up your MongoDB database on your Ubuntu installation? If yes, then you’ve landed at the right place! Follow our easy step-by-step to seamlessly install and set your MongoDB database on any Ubuntu and Linux-powered system! This blog aims at making the installation process as smooth as possible!

Upon a complete walkthrough of the blog, you’ll be able to successfully Install MongoDB on Ubuntu with ease! Furthermore, through this article, you will get a deep understanding of the tools and techniques being mentioned & thus, it will help you hone your skills further.

Table of Contents

Introduction to MongoDB

MongoDB logo

It is a high-performance document-oriented database that is powered by a NoSQL structure. It makes use of collections (tables) each having multiple documents (records) & allows the user to store data in a non-relational format. 

MongoDB stores its data as objects which are commonly identified as documents. These documents are stored in collections, analogous to how tables work in relational databases. MongoDB is known for its scalability, ease of use, reliability & no compulsion for using a fixed schema among all stored documents, giving them the ability to have varying fields (columns). 

For further information on MongoDB, you can check the official site here.

Introduction to Ubuntu

Ubuntu Logo

Ubuntu is a robust Linux-based operating system, available as open-source software. Being open-source at its core, it’s available completely free of cost for all users. In addition, Ubuntu is backed by a best in the class community of developers that help ensure that the operating system remains up-to-date and provide a smooth experience to all its users. Ubuntu is available across a diverse set of platforms and is compatible with computers, smartphones and even servers.

For further information on Ubuntu, you can check the official website here.

Simplify MongoDB ETL with Hevo’s No-code Data Pipelines

Hevo Data, a No-code Data Pipeline helps to integrate data from MongoDB and 100+ other data sources (Including 30+ Free Data Sources) and load it in a Data Warehouse of your choice to visualize it in your desired BI tool. Hevo is fully managed and completely automates the process of not only loading data from your desired source but also enriching the data and transforming it into an analysis-ready form without having to write a single line of code. Its fault-tolerant architecture ensures that the data is handled in a secure, consistent manner with zero data loss.

Get Started with Hevo for Free

It provides a consistent & reliable solution to manage data in real-time and always have analysis-ready data in your desired destination. It allows you to focus on key business needs and perform insightful analysis using BI tools. 

Check out what makes Hevo amazing:

  • Secure: Hevo has a fault-tolerant architecture that ensures that the data is handled in a secure, consistent manner with zero data loss.
  • Schema Management: Hevo takes away the tedious task of schema management & automatically detects schema of incoming data and maps it to the destination schema.
  • Minimal Learning: Hevo with its simple and interactive UI, is extremely simple for new customers to work on and perform operations.
  • Hevo Is Built To Scale: As the number of sources and the volume of your data grows, Hevo scales horizontally, handling millions of records per minute with very little latency.
  • Incremental Data Load: Hevo allows the transfer of data that has been modified in real-time. This ensures efficient utilization of bandwidth on both ends.
  • Live Support: The Hevo team is available round the clock to extend exceptional support to its customers through chat, email, and support calls.
Sign up here for a 14-Day Free Trial!

Prerequisites

  • Working knowledge of MongoDB.
  • MongoDB installed on the host workstation.
  • A general idea of working with command line/shell commands.
  • Ubuntu installed as the Operating System on the host workstation.

Steps to Install MongoDB on Ubuntu

You can Install MongoDB on Ubuntu or your Linux installation using the following steps:

Step 1: Importing MongoDB Repositories

To begin the installation process for MongoDB, you first need to import the Public key leveraged by the Package Management system associated with your Ubuntu installation.

Ubuntu’s Package Management tools help ensure Package consistency and authenticity by cross verifying that these are signed using the GPG keys. You can import the MongoDB Public GPG key using the following line of code:

> sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10</span>

Once you’ve imported the GPGP key, you now need to create the Source list for your MongoDB installation. To do this, you can use the following line of code and create the “/etc/apt/sources.list.d/mongodb-org-3.4.list” list file as follows:

> echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list

With your list file now created, you can install the Local Package repository. To do this, you can use the following line of code:

> sudo apt-get update

This completes the first step to Install MongoDB on Ubuntu.

Step 2: Installing MongoDB Packages

You now need to install the latest stable version of MongoDB on your system. You can use the following command for the same:

> sudo apt-get install -y mongodb-org
Install a specific release of MongoDB:

In case you want to install a specific version of MongoDB on your system, you’ll need to specify the version for each component package while installing them. You can refer the following example to implement this:

> 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

Step 3: Launching MongoDB as a Service on Ubuntu

With MongoDB up and running, you now need to create a Unit file, that can help your system understand the process of managing resources. For example, the most commonly leveraged Unit file helps determine how to start, stop or auto-manage a service.

To do this, you can create a configuration file, “mongodb.service in /etc/systemd/system”, that will help manage the MongoDB system.

> sudo vim /etc/systemd/system/mongodb.service

Now, copy the following information in your configuration file:

#Unit contains the dependencies to be satisfied before the service is started.
[Unit]
Description=MongoDB Database
After=network.target
Documentation=https://docs.mongodb.org/manual
# Service tells systemd, how the service should be started.
# Key `User` specifies that the server will run under the mongodb user and
# `ExecStart` defines the startup command for MongoDB server.
[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
# Install tells systemd when the service should be automatically started.
# `multi-user.target` means the server will be automatically started during boot.
[Install]
WantedBy=multi-user.target

Once you’ve created your configuration file, you now need to update the system service using the following command:

> systemctl daemon-reload

Now, start/enable the updated systemd service for your MongoDB instance:

> sudo systemctl start mongodb

With your MongoDB instance now up, you now need to verify if MongoDB started on port 27017. To do this, you can use the “netstat” command as follows:

> netstat -plntu

To verify if your MongoDB instance started correctly, you can use the status command as follows:

> sudo systemctl status mongodb

You can now enable the auto-start functionality for your system as follows:

> sudo systemctl enable mongodb

In case you want to stop or restart your MongoDB instance running on your Ubuntu installation, you can use the following lines of code:

> sudo systemctl stop mongodb

> sudo systemctl restart mongodb

This is how you can launch your MongoDB service on Ubuntu and complete another step to successfully Install MongoDB on Ubuntu.

Step 4: Configuring and Connecting MongoDB

Once you’ve set up MongoDB as a service, you now need to launch your MongoDB installation. To do this, open the Mongo Shell and switch to the database admin mode using the following command:

> mongo
> use admin

Now, create a root user for your MongoDB installation and exit the Mongo Shell as follows:

> db.createUser({user:"admin", pwd:”password", roles:[{role:"root", db:"admin"}]})

You can now connect with your MongoDB, by first restarting MongoDB and then using the following line of code:

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

You’ll now be able to see MongoDB set up a connection. You can use the “show dbs” command as follow to open a list of all available databases:

> show dbs

This is how you can successfully Install MongoDB on Ubuntu and successfully launch it.

Step 5: Uninstall MongoDB on Ubuntu (Optional)

Warning: All databases and their respective configurations would be removed after this process is put in place. Ensure that you back up all your data and configuration information before proceeding with this process, as it’s irreversible.

To uninstall MongoDB on Ubuntu, you first need to remove the MongoDB packages. To do this, you can stop the MongoDB service and execute the following command to remove the installed packages:

> sudo apt-get purge mongodb-org*

You can remove your created databases, log files and directories using the following command:

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

This is how you can uninstall MongoDB on Ubuntu.

If you have ever faced issues installing Kafka on Ubuntu, here’s a step wise guide.

Conclusion

This article teaches you how to Install MongoDB on Ubuntu with ease. It also provides in-depth knowledge about the concepts behind every step to help you understand and implement them efficiently. Extracting complex data from a diverse set of data sources can be a challenging task and this is where Hevo saves the day!

Visit our Website to Explore Hevo

Hevo offers a faster way to move data from 100+ data sources such as SaaS applications or Databases such as MongoDB into your Data Warehouse to be visualized in a BI tool. Hevo is fully automated and hence does not require you to code.

Want to take Hevo for a spin? Sign Up for a 14-day free trial and experience the feature-rich Hevo suite first hand. You can also have a look at the unbeatable pricing that will help you choose the right plan for your business needs.

Share your experience of learning about how to Install MongoDB on Ubuntu! Let us know in the comments section below!

No-code Data Pipeline for MongoDB