MongoDB is a free and open-source, cross-platform, document-oriented NoSQL database. Unlike traditional relational databases, MongoDB stores data in flexible JSON-like documents with dynamic schemas, making it easier to integrate and adapt as your application evolves.

This article covers installing MongoDB on both Ubuntu and AlmaLinux.

Step 1: Log in to your VPS via SSH

# ssh username@your_server_ip

Step 2: Add the MongoDB Repository

For Ubuntu (20.04 / 22.04)

Import the MongoDB public GPG key:

# curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -

Add the MongoDB repository:

# echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)
/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

Update packages:

# sudo apt update
For AlmaLinux (8 / 9)

Create the repo file:

# sudo nano /etc/yum.repos.d/mongodb-org.repo

Paste the lines below

# [mongodb-org-7.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/7.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-7.0.asc   

Save and exit (CTRL+X, then Y, then Enter).

Step 3: Install MongoDB

# sudo apt install -y mongodb-org             (On Ubuntu)
# sudo dnf install -y mongodb-org             (On AlmaLinux)

Step 4: Start and Enable MongoDB

# sudo systemctl start mongod
# sudo systemctl enable mongod

Check status:

# sudo systemctl status mongod

Step 5: Verify the Installation

Run the MongoDB shell:

# mongo

You should see something like:

MongoDB shell version v7.0.x

connecting to: mongodb://127.0.0.1:27017/ 

Type exit to leave.

Step 6: Changing the Default Port

By default, MongoDB listens on port 27017. To change it:

Edit the config file:

# sudo nano /etc/mongod.conf

Find port 27017 and change it to your desired port, e.g., 22222.

Restart MongoDB:

# sudo systemctl restart mongod

In this way, you’ve successfully installed MongoDB on your Ubuntu or AlmaLinux VPS.

Was this answer helpful? 0 Users Found This Useful (0 Votes)