How to Install MongoDB in Ubuntu?

A step-by-step guide to installing MongoDB in a Ubuntu server and accessing it in MongoDB Compass from your local host.

What is MongoDB?

MongoDB is an open-source NoSQL Document Oriented Database. It's a modern database with High Performance, JSON-like data structure and featured reached Query Language. We can install and use it on a Cloud Server and localhost.

Here we will see the step-by-step guild to installing MongoDB in the Ubuntu server and accessing the database using a GUI tool called Compass

How to install MongoDB in Ubuntu?

Follow these step-by-step guides to install MongoDB on a cloud server. Then will use the MongoDB Compass GUI tool to manage the database with a Graphical interface.

How to download MongoDB

Follow these steps to download MongoDB for Ubuntu.

Install CURL

CURL is a tool that helps us to download a resource from the internet. Use the following command to fetch the MongoDB latest package and install it, if you don't have CURL installed, install it.

sudo apt-get install gnupg curl

Fetch MongoDB

curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
--dearmor
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

Install MongoDB

Now update the system and install MongoDB

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

This will install the MongoDB NoSQL database in your Ubuntu Serve.

What are mongod, mongosh, and MongoDB Compass?

mongod : mongod is the menon or process that runs the MongoDB database in the server. When it's up and running, the DB is available to us.

mongosh : mongosh is a shell-like command line util that helps us to run the Query Language in the shell. We can log in to the server using SSH and we can use Mongosh to connect to the DB.

MongoDB Compass: It's a GUI tool that helps us to connect to any Mongo database interactively.

Working with MongoDB?

We have installed the MongoDB successfully. But we should know the basic commands to manage and run it as a service.

Basic mongod Commands

These commands are used to manage the Mongod service.

Enable Mongod

sudo systemctl enable mongod

Start Mongod

sudo systemctl start mongod

Check Mongod Status

sudo systemctl status mongod

Stop Mongod

sudo systemctl stop mongod

Restart Mongod

sudo systemctl restart mongod

Reload the Mongod Demon

sudo systemctl daemon-reload

Print the Mongod logs

sudo cat /var/log/mongodb/mongod.log

Creating a User in MongoDB

Now we need to create a user so that we can access the DB in the Compass GUI tool.

Login as root

mongosh
You will land in the Mongosh console, now use the admin database

use admin

Create a New User

User with access as a root user.
db.createUser({
user: "username",
pwd: "password",
roles: [ { role: "root", db: "admin" } ]
})
This will create a user with root access to the whole database.
User with access to a specific database
db.createUser({
user: "username",
pwd: "password",
roles: [ { role: "readWrite", db: "your_database_name" } ]
})
This will create a user with read-write access to the specific database. * Replace username, password, and your_database_name with real creds and then run.

Connect to MongoDB Using the MongoDB Compass

Download the MongoDB Compass and install it on your PC. Follow these steps.

What is a Connection String in MongoDB?

A connection string is a string/command used to connect to the database, which authenticates that you have the right access to the database

Connecting to a local MongoDB database in Compass

mongodb://localhost:27017
Connecting to a MongoDB install in a VPS of Cloud Server
mongodb://user:password@ip:27017/admin?authSource=admin&authMechanism=SCRAM-SHA-256

This is the basic command replace the user, password, and ip with the real creds and paste it into the compass. The UI will be like this. Press connect.


Note: Now it will give you a connection time-out error, to fix this we need to make some network adjustments.

Firewall configuration for remote connection in MongoDB?

Remember we just installed Mongo on our server, but we have not configured the firewall, so it is only available to the SSH used to log in to the server.

Allow Port in ufw

sudo ufw allow 27017
MongoDB runs on 27017 by default, so it needs to be allowed in the ufw firewall.

Edit the mongod.conf

sudo nano /etc/mongod.conf

It will open the nano editor, now find the net > bindIp and change its value to 0.0.0.0

# network interfaces
net:
port: 27017
bindIp: 0.0.0.0

now press ctrl+x and y and then enter to exit the nano editor

Restart Mongod

sudo systemctl restart mongod

Now try to reconnect, it will successfully connect you to the MongoDB Database:)