How to install MongoDB on Ubuntu 18.04 – Beginner’s Guide

If we talk about data management, surely you immediately think of . However, it is not the only data management system out there. There are many powerful Linux-based data management systems such as PostgreSQL, SQLite, Oracle, MongoDB or MariaDB. In this article we will explain step by step how to install MongoDB on Ubuntu 18.04.

PostgreSQL, SQLite, Oracle and MariaDB, in addition to being relational databases, use the SQL language. While MongoDB, one of the most popular database managers, is NoSQL, highly functional and with great performance.

What is MongoDB?

As we said before, MongoDB is one of the most popular and well-known NoSQL database managers. It is primarily used for applications that can save data in formatted documents, such as BSON.

This means that instead of saving the data in SQL-like records, it saves it in documents.

In NoSQL type, the documents do not have a defined schema. You might be worried that this will cause a messy database, but in reality, it’s quite the opposite. In a system like this, “fields” and data are simplified, therefore easier to manage and faster to store.

In addition to this, MongoDB is popular in environments where massive scalability is involved. With MongoDB, you can quickly perform replication techniques that enable data scalability. Therefore, for any application that requires storing semi-structured data, you can use MongoDB.

Install MongoDB on Ubuntu 18.04

Although MongoDB is a well-known application, it is not in the official Ubuntu repositories, so you will need to add it manually. However, this is a great advantage because it makes it easier to install and update the application. Now, to install MongoDB on Ubuntu, you will first need to connect to your server:

See also  Linux cat command – With usage examples

ssh your-user@your-server

If you are using Ubuntu 18.04, open the terminal and add the MongoDB repository’s PGP key to avoid compromising downloaded packages:

sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 9DA31620334BD75D9DCB49F368818C72E52529D4

After this you will be able to add the MongoDB repository without any problem. To do so, run this command:

echo “deb https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

Then update the command APT to sync all repositories.

sudo apt update

Next, install MongoDB using APT:

sudo apt-get install -y mongodb sudo apt install mongodb-org

Upon completion of the installation, enable and start the MongoDB service. At this point, you can start using it.

sudo systemctl enable mongodb sudo systemctl start mongodb

Finally, check the status of the service.

sudo systemctl status mongodb

Now you know how to install MongoDB on Ubuntu, and it is ready to be used.

Getting started with MongoDB

Now that we know how to install MongoDB on Ubuntu, let’s learn some MongoDB basics.

Note that the MongoDB configuration file is /etc/mongod.conf. Any changes you make to that file require a restart of the app to work.

the directory /var/log/mongodb it was created during installation and is where the application logs will be located.

Finally, MongoDB’s default port is 27017.

Create a new database

MongoDB comes with a single database called admin. To ensure a smooth workflow, you’ll need to create more for your project. To do so, first go to the MongoDB console:

mongo

Once inside, you can create a database with the use of the following command. Note that, unlike the SQL language, there is no “create database” command, only a use command. If the database exists, it can be used; otherwise, the command will create it.

See also  How to install Git on Ubuntu

use

As simple as that.

Create a new user

When you finish installing MongoDB on Ubuntu you will notice that it does not include a default administrator account. Instead, you’ll need to start creating different users for each database. And you will have to create users with specific permissions in each database.

Once inside the MongoDB console, you can access the help offered by its interface.

help

In this section, you can read about the function db.createUser(). With this function you can specify the name, the password, the database and the roles that the user will have.

The function db.createUser, like everything else in MongoDB, receives parameters in JSON. So, to make room for a new user for the newly created database, run this command:

db.createUser( { user: “deyi”, pwd: “deyi123”, roles: } )

There are several types of roles, such as dbAdmin, dbUser, read and others. Therefore, it is best to visit the to determine what is most convenient for your needs.

Now, with the following command, you can see all the users created so far with the command:

show users

To test that everything is ok with the created user, exit the MongoDB console with exit and run the following command:

exit mongo -u -p /

Enable remote authentication on MongoDB

By default, MongoDB authorizes all logs on the local machine. You will not have problems while you are running the application.

However, because you need to enable authentication, you may run into issues when your app is ready and you need to deploy it.

See also  How to install PostgreSQL on Ubuntu 18.04

To avoid inconvenience, open the file /etc/mongodb.conf and comment out the line that says bindIP: 127.0.0.1.

sudo nano /etc/mongodb.conf

Then restart the service. You can modify MongoDB’s default port in the same file.

sudo systemctl restart mongodb

Now only local users will be able to login without authentication to MongoDB. If your server is compromised, or if you want to increase security even further, you can always undo the change.

conclusion

There are many applications with different data needs. That is why NoSQL alternatives like MongoDB arise.

MongoDB is one of the most important database managers out there, due to its robustness, speed, and scalability.

In this article, we explain how to install MongoDB on Ubuntu 18.04 and get started with the database manager.

Deyi is a digital marketing enthusiast, with a background in web design, content creation, copywriting, and SEO. She is part of ‘s SEO & Localization team. In her free time, she likes to develop projects, read a book or watch a good movie.

Loading Facebook Comments ...
Loading Disqus Comments ...