Search Our Database

How to Set Up MongoDB and Configure Scaling in Linux

Last updated on |
by

Introduction

MongoDB is a widely used NoSQL database that offers high performance, scalability, and flexibility. Unlike traditional relational databases, MongoDB stores data in JSON-like documents, making it ideal for handling large volumes of unstructured or semi-structured data. Due to its schema-less design and horizontal scaling capabilities, MongoDB is a popular choice for modern applications, especially in microservices architectures, IoT platforms, content management systems, and real-time analytics engines.

Setting up MongoDB involves installing the server software, configuring a database instance, and securing access. Scaling MongoDB can be achieved either vertically (by upgrading server resources) or horizontally (by adding more nodes). MongoDB supports built-in mechanisms for horizontal scaling through sharding and high availability via replica sets. This allows developers and administrators to handle large workloads and high-throughput environments efficiently.

This guide is intended for system administrators, DevOps engineers, and backend developers who want to deploy MongoDB on a self-managed server or cloud instance. The focus will be on setting up MongoDB using the official package repositories on Linux-based systems and introducing the core concepts of vertical and horizontal scaling, including replica sets and sharding.

Common challenges with MongoDB setup and scaling include configuration errors, lack of resource limits, inefficient query design, and improper handling of distributed data. This guide addresses these concerns by providing a structured approach to both initial setup and scale-out design.

 

Prerequisites

  • A Linux-based server (Ubuntu 20.04 / CentOS 8 or later).
  • Root or sudo access to the server.
  • A stable network connection and open ports (default: 27017).
  • At least 2 GB of RAM and 10 GB of disk space (for small-scale deployments).
  • MongoDB v6.0 or later (latest stable release recommended).
  • For scaling: Multiple nodes or virtual machines with network access to each other.

 

Step-by-step Guide

Step 1: Install MongoDB on a Linux Server

To begin the MongoDB setup, install the official MongoDB packages on a supported Linux system. The following steps outline the installation process for Ubuntu 20.04 and above.

  • Import the MongoDB public GPG key:
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
  • Add the MongoDB repository to APT sources:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
  • Update APT and install MongoDB:
sudo apt update
sudo apt install -y mongodb-org
  • Enable and start the MongoDB service:
sudo systemctl start mongod
sudo systemctl enable mongod
🖊️ Tip: Use mongo to enter the MongoDB shell and verify installation.

 

Step 2: Secure MongoDB

By default, MongoDB does not enable access control. To secure your deployment, enable authentication and create an administrative user.

  • Open the MongoDB configuration file:
sudo nano /etc/mongod.conf
  • Add the following lines under the security section:
security:
  authorization: "enabled"
  • Restart MongoDB for the changes to take effect:
sudo systemctl restart mongod
  • Connect to MongoDB and create an admin user:
mongo
use admin
db.createUser({
  user: "admin",
  pwd: "securepassword",
  roles: [{ role: "userAdminAnyDatabase", db: "admin" }]
})

 

Step 3: Scale Vertically (Upgrade Resources)

Vertical scaling involves upgrading server hardware such as CPU, RAM, and disk capacity to support higher performance workloads.

  • Monitor resource usage with tools such as:
htop, iotop, mongostat
  • Based on the monitoring output, resize your server via your hosting or cloud provider dashboard.
🖊️ Tip: Vertical scaling is a good starting point but is limited by hardware constraints. For larger workloads, horizontal scaling is recommended.

 

Step 4: Scale Horizontally with Replica Sets

Replica sets offer high availability and redundancy by allowing multiple MongoDB nodes to replicate data between each other.

  • Deploy MongoDB on three servers with network access between them.
  • Edit /etc/mongod.conf on each server and add:
replication:
  replSetName: "rs0"
  • Restart MongoDB on each server, then connect to one node and run:
mongo
rs.initiate({
  _id: "rs0",
  members: [
    { _id: 0, host: "mongo1:27017" },
    { _id: 1, host: "mongo2:27017" },
    { _id: 2, host: "mongo3:27017" }
  ]
})
  • Verify replication status:
rs.status()

 

Step 5: Scale Horizontally with Sharding (for Large Datasets)

Sharding distributes data across multiple servers to support huge datasets and high throughput.

  • Deploy the following components:
    • 3 Config Servers
    • 2 or more Shards (each a replica set)
    • 1 or more Mongos Routers
  • Start a shard server:
mongod --shardsvr --replSet shard1 --port 27018
  • Start the config servers:
mongod --configsvr --replSet configReplSet --port 27019
  • Start mongos and connect:
mongos --configdb configReplSet/config1:27019,config2:27019,config3:27019
  • Add shards via the mongos shell:
sh.addShard("shard1/mongo1:27018")
sh.enableSharding("mydatabase")
sh.shardCollection("mydatabase.mycollection", { "_id": 1 })
⚠️ Important Note: Sharding adds complexity and should only be used for large-scale applications with high data volumes or throughput requirements.

 

Conclusion

Setting up MongoDB provides a powerful, schema-less database environment optimized for performance and scalability. This guide has covered the initial installation on Linux, securing the database, and scaling vertically and horizontally through replica sets and sharding. Proper configuration ensures that MongoDB can handle increasing loads while maintaining availability and performance.

For production deployments, it is recommended to monitor your cluster using MongoDB Atlas or external monitoring tools and ensure regular backups and security audits are in place.

Should you have any inquiries about the guidelines, please feel free to open a ticket through your portal account or contact us at support@ipserverone.com. We’ll be happy to assist you further.