
This article demonstrates how to deploy MongoDB on Ubuntu VPS. This guide walks you through the step-by-step process of deploying MongoDB on an Ubuntu VPS (Ubuntu 20.04 or 22.04).
What is MongoDB?
MongoDB is a NoSQL, open-source, document-oriented database designed for high performance, scalability, and flexibility. Unlike traditional relational databases (like MySQL or PostgreSQL), MongoDB stores data in JSON-like documents (called BSON โ Binary JSON), which allows for more dynamic and hierarchical data structures.
๐ Key Features:
- Schema-less: Documents in the same collection can have different structures.
- Scalable: Supports horizontal scaling via sharding.
- Flexible data model: Great for unstructured or semi-structured data.
- Rich query language: Supports ad-hoc queries, indexing, and aggregation.
- High performance: Efficient storage and fast read/write operations.
๐ฆ Common Use Cases:
- Real-time analytics
- Content management systems (CMS)
- IoT applications
- Mobile and web apps requiring rapid iteration
๐ Example Document (BSON):
{ "name": "Jane Doe", "email": "jane@example.com", "orders": [ { "product": "Book", "price": 9.99 }, { "product": "Pen", "price": 1.49 } ] }
๐ Editions:
- MongoDB Community Edition: Free and open-source.
- MongoDB Enterprise: Includes advanced security, monitoring, and support.
- MongoDB Atlas: Fully managed cloud version.
โ Prerequisites
- A fresh Ubuntu VPS (Ubuntu 20.04 LTS or Ubuntu 22.04 LTS)
- Root or sudo user access
- Basic terminal/SSH knowledge

๐ ๏ธ How to Deploy MongoDB on Ubuntu VPS
To deploy MongoDB on Ubuntu VPS, follow the steps provided below:
-
Update Your Server
Login as root user via SSH, and run the following command:
sudo apt update && sudo apt upgrade -y
-
Import MongoDB GPG Key
sudo apt-get install gnupg curl curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \ sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \ --dearmor
-
Add MongoDB Repository
-
For Ubuntu 22.04 (Jammy):
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
-
For Ubuntu 20.04 (Focal):
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
Then update the package index:
sudo apt update
-
-
Install MongoDB
sudo apt install -y mongodb-org
This installs:
mongod
: the MongoDB daemonmongo
: the MongoDB shellmongos
,mongodb-org-server
, etc.
-
Start and Enable MongoDB
sudo systemctl start mongod sudo systemctl enable mongod
Check status:
sudo systemctl status mongod
-
Secure MongoDB
By default, MongoDB listens on
127.0.0.1
. For external access:-
Allow access from remote IPs (if needed):
Edit the config file:
sudo nano /etc/mongod.conf
Find the
bindIp
line and change:bindIp: 127.0.0.1,0.0.0.0
Then restart MongoDB:
sudo systemctl restart mongod
โ ๏ธ Use a firewall to restrict access to trusted IPs only.
-
Create admin user and enable authentication:
Start Mongo shell:
mongosh
Switch to
admin
and create user:use admin db.createUser({ user: "admin", pwd: "strong_password_here", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] })
Exit shell and enable authentication in config file:
sudo nano /etc/mongod.conf
Add or uncomment:
security: authorization: enabled
Restart:
sudo systemctl restart mongod
Test login:
mongosh -u admin -p --authenticationDatabase admin
-
-
Configure UFW Firewall (Optional but Recommended)
sudo ufw enable sudo ufw allow OpenSSH sudo ufw allow 27017/tcp sudo ufw status
Replace
27017
with the specific port if you change the default. -
Monitor and Manage MongoDB
Basic commands:
# Check MongoDB status sudo systemctl status mongod # Stop MongoDB sudo systemctl stop mongod # Start MongoDB sudo systemctl start mongod # Restart MongoDB sudo systemctl restart mongod

Conclusion
You now know how to deploy MongoDB on Ubuntu VPS. Having following the steps outlined above, MongoDB is now installed, secured, and running on your Ubuntu VPS. You can now begin developing applications or integrating MongoDB with other services on your stack.