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 and the steps to Install MongoDB on Ubuntu, it will help you hone your skills further.

MongoDB Overview

MongoDB is an open-source, document-oriented NoSQL database. The term ‘NoSQL’ signifies that it is a non-relational database, meaning it stores data in a format distinct from relational tables, eliminating the need for a fixed schema. The data is stored in BSON (Binary JavaScript Object Notation), a binary structure that efficiently encodes information, enabling swift parsing. This characteristic facilitates a highly scalable and adaptable document structure.

Benefits of MongoDB Database

  • A developer data platform hosted on the cloud.
  • Feature adaptable document schemas.
  • Offers robust querying and analytics capabilities.
  • Facilitates easy horizontal scaling through sharding.
  • Easy installation on various platforms.
  • Provides a cost-effective and efficient solution.
Simplify MongoDB ETL with Hevo’s No-code Data Pipelines

Are you looking for an easy way to replicate data from MongoDB? Hevo Data, a No-code Data Pipeline helps to integrate data from MongoDB and 150+ other data sources (Including 40+ Free Data Sources) and load it in a Data Warehouse of your choice to visualize it in your desired BI tool.

Hevo’s fault-tolerant architecture ensures that the data is handled in a secure, consistent manner with zero data loss. It provides a consistent & reliable solution to manage data in real-time and always have analysis-ready data in your desired destination.

Sign up here for a 14-Day Free Trial!

Ubuntu Overview

Ubuntu is an operating system based on the Linux platform, designed to operate seamlessly on computers, smartphones, and network servers. Developed by Canonical Ltd, a UK-based company, Ubuntu adheres to the principles of open-source software development.

Key Features of Ubuntu

Ubuntu boasts several noteworthy features, including:

  • Full compatibility with software commonly associated with Windows, such as Firefox, Chrome, VLC, etc.
  • Inclusion of Thunderbird, an integrated email software facilitating access to Exchange, Gmail, Hotmail, and more.
  • Provision of complimentary applications for photo viewing and editing.
  • Video management applications should be included, enabling users to share videos.

Advantages of Using Ubuntu

Using Ubuntu in your system offers several advantages like,

  • Ubuntu is open-source and freely available.
  • Updates can be implemented without the need for system restarts.
  • High customizability allows users to tailor Ubuntu to their preferences.
  • Ubuntu is recognized for its robust security features compared to alternative operating systems.
  • Supported by an extensive open-source community, Ubuntu benefits from collaborative development and support.

Download MongoDB

Prerequisites

Certain prerequisite knowledge is essential for a smooth installation process, including:

  • Proficiency in MongoDB is required.
  • Install MongoDB on the host workstation.
  • Familiarity with operating command line/shell commands is recommended.
  • The host machine should have Ubuntu installed as the operating system.

By default, the MongoDB instance stores its data files in /var/lib/mongodb and its log files in /var/log/mongodb while operating under the MongoDB user account. If desired, alternative directories for log and data files can be specified in /etc/mongod.conf, with additional details available under systemLog.path and storage.dbPath.

In altering the user running the MongoDB process, adjusting the access control rights for the /var/lib/mongodb and /var/log/mongodb directories is imperative. 

This modification is necessary to grant the specified user access to these directories.

Following are the steps to run MongoDB

Step 1: Start MongoDB

Execute the subsequent command to initiate the mongod process

sudo service mongod start

Step 2: Check if MongoDB has started

Confirm if the mongod process has started successfully by examining the log file located at /var/log/mongodb/mongod.log. Ensure that the log file contains a line indicating the successful startup.

[initandlisten] waiting for connections on port <port>

Step 3: Stop MongoDB

You can stop executing the mongod process using the following 

sudo service mongod stop

Step 4: Restart MongoDB

To restart, use the following:

sudo service mongod restart

Step 5: Start using MongoDBUse the link to know more about getting started with MongoDB.

Packages

MongoDB officially offers supported packages that are available in their dedicated repository. The repository includes the following package components:

1. mongodb-org

A meta package that automatically installs the four component packages listed below.

2. mongodb-org-server

Includes the mongod daemon along with its associated configuration and init scripts.

3. mongodb-org-mongos

Contains the mongo daemon.

4. mongodb-org-shell

Contains the mongo shell.

5. mongodb-org-tools

Comprises of various MongoDB tools like mongofiles, mongodump, mongoimport bsondump, mongoexport, mongooplog, mongoperf, mongorestore, mongostat, and mongotop.

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: MongoDB Tuning

Scaling MongoDB is easy and can be done in both ways, horizontally and vertically. It is essential to ensure the optimal performance of the Database. Horizontal scaling is adding server resources such as RAM and CPUs while vertical scaling is adding servers to the configuration. 

The performance of the MongoDB Database depends on several factors including Memory use, Number of concurrent connections, and WiredTiger Cache among others. The default storage engine of MongoDB is WiredTiger which preserves 50% memory. This means 8GB of RAM will have a 0.5*(8-1) memory preserver for WiredTiger.

For checking the usage stats and determining if changes are required use the following command given below.

> db.serverStatus().wiredTiger.cache
{
  'application threads page read from disk to cache count': 6,
  'application threads page read from disk to cache time (usecs)': 46,
  'application threads page write from cache to disk count': 184,
  'application threads page write from cache to disk time (usecs)': 10501,
  'bytes allocated for updates': 65768,
  'bytes belonging to page images in the cache': 30285,
  'bytes belonging to the history store table in the cache': 571,
  'bytes currently in the cache': 104652,
  'bytes dirty in the cache cumulative': 2813442,
  'bytes not belonging to page images in the cache': 74366,
  'bytes read into cache': 28042,
  'bytes written from cache': 1283385,
  'cache overflow score': 0,
  'checkpoint blocked page eviction': 0,
  'checkpoint of history store file blocked non-history store page eviction': 0,
  'eviction calls to get a page': 2,
  'eviction calls to get a page found queue empty': 2,
  'eviction calls to get a page found queue empty after locking': 0,
  'eviction currently operating in aggressive mode': 0,
  'eviction empty score': 0,
  'eviction passes of a file': 0,
  'eviction server candidate queue empty when topping up': 0,
  'eviction server candidate queue not empty when topping up': 0,
  'eviction server evicting pages': 0,
  'eviction server slept, because we did not make progress with eviction': 0,
  'eviction server unable to reach eviction goal': 0,
  'eviction server waiting for a leaf page': 0,
  'eviction state': 64,
  'eviction walk target pages histogram - 0-9': 0,
  'eviction walk target pages histogram - 10-31': 0,
  'eviction walk target pages histogram - 128 and higher': 0,
  'eviction walk target pages histogram - 32-63': 0,
  'eviction walk target pages histogram - 64-128': 0,
  'eviction walk target pages reduced due to history store cache pressure': 0,
  'eviction walk target strategy both clean and dirty pages': 0,
  'eviction walk target strategy only clean pages': 0,
  'eviction walk target strategy only dirty pages': 0,
  'eviction walks abandoned': 0,
..............

From the above result, some of the key points to note are listed below.

  • wiredTiger.cache.maximum bytes configure
  • wiredTiger.cache.bytes currently in the cache
  • wiredTiger.cache.pages read into cache
  • wiredTiger.cache.pages written from cache
  • wiredTiger.cache.tracked dirty bytes in the cache

To check the usage of WiredTiger Concurrency Read and Write Ticket, follow the command given below.

> db.serverStatus().wiredTiger.concurrentTransactions
{
  write: { out: 0, available: 128, totalTickets: 128 },
  read: { out: 1, available: 127, totalTickets: 128 }
}
Download the Guide on How to Set Up a Data Analytics Stack
Download the Guide on How to Set Up a Data Analytics Stack
Download the Guide on How to Set Up a Data Analytics Stack
Learn how to build a self-service data analytics stack for your use case.

Step 6: 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.

Also check,

  1. How to Install Kafka on Ubuntu
  2. How to Install Postgres on Ubuntu
  3. How to Install NodeJS with MongoDB on Ubuntu

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!

Hevo is the only real-time ELT No-code Data Pipeline platform that cost-effectively automates data pipelines that are flexible to your needs. With integration with 150+ Data Sources (40+ free sources), we help you not only export data from sources & load data to the destinations but also transform & enrich your data, & make it analysis-ready.

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 Hevo 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!

mm
Former Director of Product Management, Hevo Data

Vivek Sinha has extensive experience in real-time analytics and cloud-native technologies. With a focus on Apache Pinot, he was a driving force in shaping innovation and defensible differentiators, including enhanced query processing, data mutability support, and cost-effective tiered storage solutions at Hevo. He also demonstrates a passion for exploring and implementing innovative trends within the dynamic data industry landscape.

No-code Data Pipeline for MongoDB

Get Started with Hevo