Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Install MongoDB on Ubuntu

Add the MongoDB repository, install mongodb-org and start the service.

Databases2 min read8 steps3 screenshots

What MongoDB is

MongoDB is a popular NoSQL document database. Why people use it:

  1. Flexible. Its schema-less design stores documents of different shapes.
  2. Scalable. It grows by adding more servers (horizontal scaling).
  3. Fast, especially for reading and writing large amounts of data.
  4. Easy to learn.
  5. Rich in features: indexes, aggregation, replication and more.

Requirements

  1. A recent Ubuntu system.
  2. A user with sudo rights.
  3. Basic use of the terminal.

Step 1: Add the MongoDB repository

The package mongodb-org is not in the Ubuntu repositories, so add the official MongoDB repository first.

Import the key (the example uses MongoDB 7.0; check the MongoDB documentation for the current series):

curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor

Create the source list for your Ubuntu release (this one is for Ubuntu 22.04, "jammy"; use focal for 20.04):

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

Step 2: Update the package list

sudo apt-get update
Updating the package list

Step 3: Install MongoDB

sudo apt install mongodb-org

Step 4: Start MongoDB and check it

sudo systemctl start mongod
MongoDB is active
sudo systemctl status mongod
The MongoDB status

Start it at boot:

sudo systemctl enable mongod

Use it

Open the MongoDB shell (mongosh):

mongosh

Try:

use test
db.items.insertOne({ name: "first" })
db.items.find()

Secure it

  • By default MongoDB listens on localhost only. Leave it so unless you need remote access.
  • If you open it to the network, turn on authentication (security.authorization: enabled in /etc/mongod.conf) and use a firewall.

How MongoDB is used

  • E-commerce. Product catalogs and customers.
  • Content management. Website content.
  • Mobile apps. User data and settings.
  • Real-time analytics.

Quick recap

  • Add the MongoDB repository and key.
  • sudo apt install mongodb-org, then start mongod.
  • Use mongosh and keep access limited.