How to Install MongoDB on Ubuntu
Tags: mongodb, ubuntu, howto, database
Published: 2020-04-18 11:11:13


*This post was [published](https://aligoren.com/how-to-install-mongodb-on-ubuntu/) on my blog before*

---

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

Before this post, I [published](https://dev.to/aligoren/project-idea-01-simple-url-shortener-4pkk) 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](https://www.mongodb.com/) 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;

```bash
sudo apt update
```

### Step - 2 Install MongoDB

```bash
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 <kbd>Y</kbd> and enter. If your OS is up to date, MongoDB will be installed successfully.

### Step - 3 Check MongoDB Service Status

```bash
sudo systemctl status mongodb
```

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

```bash
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;

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

Output will be like that;

```bash
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**

```bash
sudo systemctl status mongodb
```

**Stop Service**

```bash
sudo systemctl stop mongodb
```

**Start Service**

```bash
sudo systemctl start mongodb
```

**Restart Service**

```bash
sudo systemctl restart mongodb
```

**Disable MongoDB Service**

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

```bash
sudo systemctl disable mongodb
```

**Enable MongoDB Service**

To enable again, use this command;

```bash
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](https://aligoren.com/project-idea-01-simple-url-shortener/)

The second thing is NestJS fully [supported](https://docs.nestjs.com/techniques/mongodb) 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 

- DigitalOcean [documentation](https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-18-04).
- DZone [When to Use (and Not to Use) MongoDB](https://dzone.com/articles/why-mongodb)
- WikiPedia [Document-oriented Database](https://en.wikipedia.org/wiki/Document-oriented_database)