DEV Community

Cover image for How to Install MongoDB on Ubuntu
Itachi Uchiha
Itachi Uchiha

Posted on

How to Install MongoDB on Ubuntu

This post was published on my blog before


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
Enter fullscreen mode Exit fullscreen mode

Step - 2 Install MongoDB

sudo apt install mongodb
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

The second way to check MongoDB status use this command;

mongo --eval 'db.runCommand({ connectionStatus: 1 })'
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

Helpful Systemd Commands for MongoDB Service

These are helpful systemd commands for MongoDB service.

Verify Service Status

sudo systemctl status mongodb
Enter fullscreen mode Exit fullscreen mode

Stop Service

sudo systemctl stop mongodb
Enter fullscreen mode Exit fullscreen mode

Start Service

sudo systemctl start mongodb
Enter fullscreen mode Exit fullscreen mode

Restart Service

sudo systemctl restart mongodb
Enter fullscreen mode Exit fullscreen mode

Disable MongoDB Service

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

sudo systemctl disable mongodb
Enter fullscreen mode Exit fullscreen mode

Enable MongoDB Service

To enable again, use this command;

sudo systemctl enable mongodb
Enter fullscreen mode Exit fullscreen mode

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 :P

EOL

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

Resources

Top comments (3)

Collapse
 
padakipavan profile image
padaki-pavan

Or to make it even easier use docker on Ubuntu and install mongodb on it.

Collapse
 
itachiuchiha profile image
Itachi Uchiha

Thank you. Actually, I thought this. But I think Docker would be good If I use an isolated development environment. Just for me (:

Collapse
 
padakipavan profile image
padaki-pavan

It makes sense when you are working on multiple projects using mongoDB, makes things much easier then.