Are you here because you want to Install MongoDB in ubuntu? And are you struggling to find a detailed guide that will help you set up your MongoDB database on your Ubuntu installation? If your answer is yes then my friend you are at the right place. Follow the easy steps given below and easily install and set your MongoDB database on any database on any Ubuntu and Linux-powered system.

Install MongoDB in Ubuntu

Once you go through this blog you will be able to successfully Install MongoDB in Ubuntu without any problem. Also, in this blog, you will get an understanding of the tools and techniques. And it will also help you polish your skills much further.

Also Read: $ in MongoDB: Information on the topic

Introduction to MongoDB

Install MongoDB in Ubuntu

MongoDB is a high-performance document-oriented database that is powered by a NoSQL structure. In MongoDB, you can use collections (tables) each having multiple documents (records). And it also allows storing data in a very non-relational format.

The data are stored as objects in MongoDB which are generally identified as documents. The documents are stored in collections, similar to how tables work in relational databases. MongoDB is also known for its ease of use, reliability, scalability, and no compulsion for using a fixed schema. Among all stored documents it gives them the ability to have varying fields.

If you want more information on MongoDB you can check the official site here.

Introduction to Ubuntu

Install MongoDB in Ubuntu

Ubuntu is a Linux-based operating system that is available open-source. Open source means it is available for free of cost for any user that wishes to use it. Ubuntu is highly rated by the best-of-class community developers who ensure that the operating system always remains up to date and provides a smooth user experience. Ubuntu is available for all platforms. And it is also compatible with different platforms like smartphones, computers, and even servers.

Basic Requirements:

  • You need a working knowledge of MongoDB
  • Also, you need to install MongoDB on the host workstation.
  • Little idea about working with command line/shell commands.
  • Also, Ubuntu installed as the Operating system on the host workstation.

Steps to Install MongoDB on Ubuntu

Here is how you can install MongoDB on Ubuntu or your Linux Installation using the following steps.

  • Firstly, importing MongoDB repositries
  • Then Installing the MongoDB packages.
  • After that launching as a service on Ubuntu
  • Then confiugring and connecting MongoDB
  • Lastly, uninstall MongoDB on Ubuntu

Importing MongoDB Repositiries

The first thing you need to do for the process of installation is to import the public key leveraged by the package management system associated with your Ubuntu installation.

Ubuntu package management tools help you ensure your package consistency and authenticity by cross verifying that these are signed using the GPG keys. Use the following keys to 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>

After you have imported the GPG key then you need to create the source list for your MongoDB installation. You can do that by adding the following line of code and create “/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

Now your list is created you can easily install the Local Package repository. Use the code given below to do that.

> sudo apt-get update

Here your first step of the installation is complete let’s jump onto the second part.

Installing MongoDB Packages.

Now you need to install the latest stable version of MongoDB on your system. Use the code given below.

 sudo apt-get install -y mongodb-org

If you want to install a specific version of MongoDB on your local. Then you will need to specify the version for each component package while installing them. Follow the command given below to do that.

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

Launching MongoDB as a Service on Ubuntu

Now your MongoDB is ready to go it’s time for you to create a Unit file. Creating a unit file will help your system to understand the process of managing resources. Let us provide you with an example of the most commonly leveraged Unit file that helps determine how to start, stop or auto-manage a service.

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

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

Then copy the configuration file given below.

#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

After you have created your configuration file then you need to update your system service using the following command.

> systemctl daemon-reload

All you need to do now is activate the updated system service for your MongoDB instance.

> sudo systemctl start mongodb

Now your MongoDB instance is up and all you need to do now is verify if MongoDB started on port 27017. For this, you need to use the “netstat” command as follows:

> netstat -plntu

Then to verify your MongoDB instance started correctly, you can use the status command as follows.

> sudo systemctl status mongodb

You can activate the auto-start functionality for your system as follows.

> sudo systemctl enable mongodb

Now if you want to stop or restart your MongoDB instance running on your Ubuntu installation follow the code given below.

> sudo systemctl stop mongodb

> sudo systemctl restart mongodb

Configuring and Connecting MongoDB

Now to do this open your MongoDB shell and then switch to the database admin mode using the following command.

> mongo
> use admin

You can also create a root user for your MongoDB installation and then exit the Mongo shell as follows.

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

Now you will be able to see your MongoDB set up a connection. Also, you can use the “show DBS” command as follows open and see the list of all available databases.

> show dbs

This is how you can easily successfully install MongoDB on Ubuntu and can also successfully launch it.

Conclusion

In this blog, we talked about Install MongoDB in ubuntu swiftly and fast. We also provide in-depth knowledge about the concepts after every step to help you understand why it is happening. Hope you find this information useful. Thank you for the read.