How to Install MongoDB on Ubuntu

MongoDB Poster

Hi. In this post, I’ll explain how to install MongoDB on Ubuntu 18.04.

Before this post, I published a post about a simple project idea.

My operating system is Ubuntu for now. I usually change my operating system every 30 days.

What is MongoDB?

MongoDB is a popular NoSQL database solution using by most famous companies.

That doesn’t mean only big companies can use it. In this post, we will install MongoDB and we’ll see how to enable it.

Prerequisites

  • Basic Linux Commands
  • Ubuntu 18.04
  • non-root user

These are the prerequisites. Actually, you don’t have to use Ubuntu. You can choose your own operating system. There are many documents on Google about your own OS.

Installing MongoDB

As you know, most GNU/Linux distributions have their package repositories with a package manager. Ubuntu is one of them. Ubuntu has official MongoDB packages in its package repositories.

Step – 1 Update Package List

We’ll update the packages list;

sudo apt update

Step – 2 Install MongoDB

sudo apt install mongodb

This command will install the MongoDB server along with some management tools. After this command, you will see a prompt. Type Y and enter. If your OS is up to date, MongoDB will be installed successfully.

Step – 3 Check MongoDB Service Status

sudo systemctl status mongodb

Normally, MongoDB starts automatically after installation but it would be good If we check it. Output will be like that;

mongodb.service - An object/document-oriented database
   Loaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-05-26 07:48:04 UTC; 2min 17s ago
     Docs: man:mongod(1)
 Main PID: 2312 (mongod)
    Tasks: 23 (limit: 1153)
   CGroup: /system.slice/mongodb.service
           └─2312 /usr/bin/mongod --unixSocketPrefix=/run/mongodb --config /etc/mongodb.conf

The second way to check MongoDB status use this command;

mongo --eval 'db.runCommand({ connectionStatus: 1 })'

Output will be like that;

MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
{
    "authInfo" : {
        "authenticatedUsers" : [ ],
        "authenticatedUserRoles" : [ ]
    },
    "ok" : 1
}

Helpful Systemd Commands for MongoDB Service

These are helpful systemd commands for MongoDB service.

Verify Service Status

sudo systemctl status mongodb

Stop Service

sudo systemctl stop mongodb

Start Service

sudo systemctl start mongodb

Restart Service

sudo systemctl restart mongodb

Disable MongoDB Service

By default, MongoDB starts on server startup. You can disable it to prevent an automatic start.

sudo systemctl disable mongodb

Enable MongoDB Service

To enable again, use this command;

sudo systemctl enable mongodb

Why I Choose MongoDB

To learn MongoDB is easy. If you’re familiar with JavaScript or similar languages, you will learn easy to manage it as a programmer. I don’t say you will learn in 24 hours. But you can find many resources about MongoDB from zero to hero.

  • Document Oriented
  • High performance
  • No JOINS (There are no SQL joins directly)
  • Flexible
  • Document-Based Query Language

In fact, choosing a database completely depends on what do you do. For instance, using MongoDB for a part of an e-commerce project would be good. Let’s say you want to build a blog script. You can use MongoDB.

We use MongoDB as the database solution, as we will create a URL shortener

The second thing is NestJS fully supported MongoDB. This really good for my atomic project 😛

EOL

That’s all. Thanks for reading. If there is anything wrong, let me know, please.

Resources