VPS

How to Install MongoDB on Ubuntu 18.04 – Guide for Beginners

When talking about data management, most people immediately think about MySQL. However, it is not the only data management system that exists. There are plenty of powerful data management systems for Linux based VPS, such as PostgreSQL, SQLite, Oracle or MariaDB.

In addition to being relational databases, these managers use the SQL language.  But, there are other NoSQL database managers that are highly functional and have great yields.

One of the most popular is MongoDB. In this article, we will provide a  step by step tutorial on how to install MongoDB in Ubuntu 18.04.

What is MongoDB

MongoDB is one of the most popular and best know NoSQL database managers. Mainly, it is used for applications where you can save data in formatted documents such as BSON.

This means that instead of saving data in records such as SQL types, it saves it in documents.

In NoSQL type databases documents don’t have a defined schema. You might worry, that this will cause a messy database, but actually, it’s quite the opposite. In a system like this, “fields” and data are simplified, thus easier to manage and stored faster.

In addition to this, MongoDB is popular in environments where massive scalability. With MongoDB, you can quickly perform replication techniques that allow the scalability of the data. So any application that requires storing semi-structured data 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 have to add it manually. However, this is a huge advantage because it makes the installation and application updates easier. First, we have to connect to our server using SSH:

ssh your-user@your-server

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

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

After that, you can add the MongoDB repository without any problem. To do it, run this command:

echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

Next, refresh the APT command  to synchronize all repositories.

sudo apt update

Next, install MongoDB using APT:

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

At the end of the installation, enable and start the MongoDB service. With this, you will be able to start using it.

sudo systemctl enable mongodb
sudo systemctl start mongodb

Finally, check the service status.

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 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 application to work.

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

Finally, the default port of MongoDB is 27017.

Create a New Database

MongoDB comes with a single database called admin. To ensure a good workflow, you will 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 command use. Note that unlike the SQL language there is no command or “create databases” clause, only use. If the database exists then it can be used, otherwise, the command will create it.

use [database_name]

Simple as that.

Create a New User

By default, MongoDB does not include a default administrator account. Instead, start creating different users for each database. However, it is necessary 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 see the function db.createUser(). In it, specify the name, password, database, and roles it will have.

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

db.createUser(
{
user: "edward",
pwd: "edward123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)

There are several types of roles such as dbAdmin, dbUser, read and others. So it is best to visit the official MongoDB documentation to determine what is most convenient in each case.

Now you can show all the users created so far with the command:

show users

To test the operation, exit the MongoDB console with exit and run the following command:

exit
mongo -u [user] -p [password] [host:port]/[database]

The MongoDB connection to host and port remotely will be covered in the next section.

Enable Remote Authentication on MongoDB

By default, MongoDB authorizes all logs from the local machine. There are no problems while the application is developing.

However, because you have to enable authentication, you might run into issues when the application is ready and you have to deploy it.

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

sudo nano /etc/mongodb.conf

Next, restart the service. You can modify the default port of MongoDB in the same file.

sudo systemctl restart mongodb

Now only local users can log in without authentication at MongoDB. If your server gets compromised, or you want to increase security, even more, you can always eradicate that.

Conclusion

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

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

In this article, you learned how to install MongoDB in Ubuntu 18.04 and take the first steps with the database manager.

About the author

Edward S.

Edward is Hostinger's copywriter. He's an expert communicator with years of experience in IT as a writer, marketer, and Linux enthusiast. IT is a core pillar of his life, personal and professional. Edward's goal is to encourage millions to achieve an impactful online presence. He also really loves dogs, guitars, and everything related to space.

Add Comment

Click here to post a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Get Exclusive Content

Join thousands of webmasters who get our free newsletter with tips on how to drive more traffic and revenue to their websites!

Please wait...

Thank you for sign up!

More in VPS
How to Remove All Images in Docker
Close