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.
Table of Contents
Introduction to MongoDB
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. Also, check MongoDB Replica Set.
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.
Features of MongoDB Database
A few features of MongoDB Database are listed below:
- It supports ad hoc queries for real-time and optimized Data Analytics. Users can search by range query, field, and regular expression.
- Efficient Indexing in MongoDB Database allows better query executions. It improves search speed and performance by offering a broad range of indices and features with language-specific sort orders.
- MongoDB employs multiple replica sets for better data availability and stability. The primary server in MongoDB proceeds all the write operations and applies the same operations on all the secondary servers or nodes.
Advantages of MongoDB Database
A few pros of using MongoDB Database are listed below:
- MongoDB is designed to be a distributed Database that can easily scale out horizontally. It can create clusters in real-time replication and shard high-throughput across clusters to maintain performance.
- MongoDB supports Schema changes without any downtime of the server. Users can write new data to the MongoDB Database without disrupting other operations.
- MongoDB stores data in a document format that allows developers to access data using any language and use it in Data Structures.
Introduction to Ubuntu
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.
Features of Ubuntu
A few features of Ubuntu are listed below:
- Ubuntu is a free open-source operating system that is backed by huge community support.
- Ubuntu comes with pre-installed E-mail software called Thunderbird that allows users access to multiple E-Mails services such as Exchange, Gmail, Hotmail, etc.
- Ubuntu has a huge software repository that allows users to install thousands of software with a single click.
Advantages of Ubuntu
A few features of Ubuntu are listed below:
- Ubuntu offers a user-friendly interface test with a high level of customization. Users have the freedom to customize the desktop environment, and it is easy to install.
- Ubuntu works in a wide range of devices or Computing platforms including Windows and Mac computers. It supports easy installation on network servers, IoT devices, and other platforms.
- Ubuntu is better suited for the development environment as it comes with an office suite, a text editor, and a variety of other productivity apps from the get-go.
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 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:
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 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 }
}
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, How to install Kafka 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 offers a faster way to move data from 150+ 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 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!