Back to all templates
MongoDB Replica Set logo

MongoDB Replica Set

Database2048MB+ RAM

Production-ready 3-node replica set with transactions and failover

databasenosqlmongodbhigh-availabilityreplication

Deploy MongoDB Replica Set in 3 Steps

1

Connect Your VPS

Add your server credentials to Server Compass

2

Select MongoDB Replica Set

Choose from our template library

3

Deploy & Configure

Fill in settings and click Deploy

No Docker knowledge required
Step-by-step deployment guide

Deploy MongoDB Replica Set on a VPS with Server Compass

Use the MongoDB Replica Set template in Server Compass to deploy a three-node MongoDB replica set on your VPS, then verify the primary node with mongosh.

About 10 minutesBrowser verified
1
Step 1

Open the server Apps tab

Select the tutorial-vps VPS, open the Apps tab, and start a new app deployment. Keep sensitive server details hidden before capturing or sharing screenshots.

Server Compass Apps tab before creating a MongoDB Replica Set app
2
Step 2

Choose an app template

Click New App and choose the template deployment path so Server Compass can load the built-in catalog.

Choosing to deploy an app from a Server Compass template
3
Step 3

Search for MongoDB Replica Set

Use the template picker search to find MongoDB Replica Set in the Server Compass template catalog.

Searching for MongoDB Replica Set in the Server Compass template picker
4
Step 4

Select the MongoDB Replica Set template

Choose the MongoDB Replica Set template. Server Compass fills the primary node, two secondary nodes, shared keyfile volume, host port, root username, and generated password.

MongoDB Replica Set template selected in Server Compass
5
Step 5

Review the MongoDB Replica Set settings

Confirm the app name and compose service. In this run, the app was named mongodb-replicaset-demo and used host port 27017.

Reviewing MongoDB Replica Set project settings and compose services
6
Step 6

Deploy MongoDB Replica Set

Review the generated environment values, keep the MongoDB root password masked, confirm the primary node port is available, and click Deploy Now.

Reviewing MongoDB Replica Set environment variables and port before deployment
7
Step 7

Watch the deployment progress

Keep the deployment modal open while Server Compass uploads the compose file, pulls the MongoDB Replica Set image, starts the container, and verifies the stack.

Server Compass deploying the MongoDB Replica Set template on the VPS
8
Step 8

Confirm MongoDB Replica Set is running

After deployment finishes, return to the Apps tab and confirm the MongoDB Replica Set app is marked Running with the database port available.

MongoDB Replica Set template running in the Server Compass Apps tab
9
Step 9

Verify MongoDB Replica Set is reachable

Open the app detail view and verify the running MongoDB Replica Set containers. In this tutorial run, mongosh confirmed the replica set name and primary status without exposing the generated password.

MongoDB Replica Set app detail screen after verifying the deployed database container

After MongoDB Replica Set Opens

  • Store the generated MongoDB root password in a secure password manager.
  • Create application-specific MongoDB users and databases before production use.
  • Restrict external access to port 27017 unless remote clients explicitly need it.
  • Configure regular backups for all MongoDB replica set data volumes before storing production data.
  • Create application-specific users and databases before production use.

Verified Result

The MongoDB primary container returned rs0:PRIMARY from an internal mongosh verification command.

MongoDB Replica Set deployment questions

What does the MongoDB Replica Set template deploy?

It deploys one MongoDB primary, two MongoDB secondaries, and an init container that creates the rs0 replica set with shared keyfile authentication.

Which port did the tutorial use?

The tutorial used host port 27017, which maps to the MongoDB primary container port 27017.

Why is there no browser verification screenshot?

MongoDB Replica Set is a database service, not a web application. The tutorial verifies it with mongosh inside the running primary container instead of opening a browser page.

Should this become a blog post?

No. The deployment guide should live on the MongoDB Replica Set template detail page and be linked from the reusable template deployment docs page.

Command Line Setup

Install MongoDB Replica Set Manually

Set up MongoDB Replica Set yourself using Docker Compose and the command line.

1

Log into Your Server

Access your server's command line by opening a terminal and running the SSH command below.

terminal
# SSH into your server
ssh root@your-server-ip

# Using a custom SSH key
ssh -i ~/.ssh/id_rsa root@your-server-ip

First time? Need Docker? Install it: curl -fsSL https://get.docker.com | sh

2

Prepare the Install Location

Set up the folder structure for your Docker deployment.

terminal
# Create and navigate to project directory
mkdir -p ~/apps/mongodb-replicaset
cd ~/apps/mongodb-replicaset
3

Create docker-compose.yml

Set up your Docker Compose file with this configuration:

docker-compose.yml
services:
  mongodb-primary:
    image: mongo:7
    ports:
      - "27017:27017"
    entrypoint:
      - bash
      - -c
      - |
          set -e
          KEYFILE_PATH=/etc/mongo-keyfile/keyfile
          if [ ! -s $$KEYFILE_PATH ]; then
            umask 077
            openssl rand -base64 756 > $$KEYFILE_PATH
            chown mongodb:mongodb $$KEYFILE_PATH
            chmod 0400 $$KEYFILE_PATH
          fi
          exec docker-entrypoint.sh "$$@"
      - --
    command: ["--replSet", "rs0", "--bind_ip_all", "--keyFile", "/etc/mongo-keyfile/keyfile"]
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=<your-mongo-password>
    volumes:
      - mongodb_primary_data:/data/db
      - mongodb_keyfile:/etc/mongo-keyfile
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "mongosh", "--quiet", "--username", "admin", "--password", "<your-mongo-password>", "--authenticationDatabase", "admin", "--eval", "db.adminCommand('ping').ok"]
      interval: 30s
      timeout: 10s
      retries: 5

  mongodb-secondary1:
    image: mongo:7
    entrypoint:
      - bash
      - -c
      - |
          set -e
          KEYFILE_PATH=/etc/mongo-keyfile/keyfile
          until [ -s $$KEYFILE_PATH ]; do
            echo "Waiting for MongoDB keyfile..."
            sleep 1
          done
          exec docker-entrypoint.sh "$$@"
      - --
    command: ["--replSet", "rs0", "--bind_ip_all", "--keyFile", "/etc/mongo-keyfile/keyfile", "--auth"]
    volumes:
      - mongodb_secondary1_data:/data/db
      - mongodb_keyfile:/etc/mongo-keyfile:ro
    depends_on:
      - mongodb-primary
    restart: unless-stopped

  mongodb-secondary2:
    image: mongo:7
    entrypoint:
      - bash
      - -c
      - |
          set -e
          KEYFILE_PATH=/etc/mongo-keyfile/keyfile
          until [ -s $$KEYFILE_PATH ]; do
            echo "Waiting for MongoDB keyfile..."
            sleep 1
          done
          exec docker-entrypoint.sh "$$@"
      - --
    command: ["--replSet", "rs0", "--bind_ip_all", "--keyFile", "/etc/mongo-keyfile/keyfile", "--auth"]
    volumes:
      - mongodb_secondary2_data:/data/db
      - mongodb_keyfile:/etc/mongo-keyfile:ro
    depends_on:
      - mongodb-primary
    restart: unless-stopped

  mongodb-init:
    image: mongo:7
    depends_on:
      - mongodb-primary
      - mongodb-secondary1
      - mongodb-secondary2
    restart: "no"
    environment:
      - MONGO_USER=admin
      - MONGO_PASSWORD=<your-mongo-password>
    entrypoint:
      - bash
      - -lc
      - |
          set -e

          echo "Waiting for MongoDB primary (auth)..."
          until mongosh --quiet mongodb-primary:27017/admin --authenticationDatabase admin --username $$MONGO_USER --password $$MONGO_PASSWORD --eval "db.adminCommand({ ping: 1 }).ok" 2>/dev/null | grep -q 1; do
            sleep 1
          done

          echo "Waiting for MongoDB secondaries..."
          until mongosh --quiet mongodb-secondary1:27017/admin --eval "db.hello().ok" 2>/dev/null | grep -q 1; do
            sleep 1
          done
          until mongosh --quiet mongodb-secondary2:27017/admin --eval "db.hello().ok" 2>/dev/null | grep -q 1; do
            sleep 1
          done

          # Idempotency: if already configured, do nothing.
          if mongosh --quiet mongodb-primary:27017/admin --authenticationDatabase admin --username $$MONGO_USER --password $$MONGO_PASSWORD --eval "db.hello().setName" 2>/dev/null | grep -q rs0; then
            echo "Replica set already initialized."
            exit 0
          fi

          echo "Initializing replica set..."
          mongosh --quiet mongodb-primary:27017/admin --authenticationDatabase admin --username $$MONGO_USER --password $$MONGO_PASSWORD --eval "rs.initiate({_id: 'rs0', members: [{_id: 0, host: 'mongodb-primary:27017'}, {_id: 1, host: 'mongodb-secondary1:27017'}, {_id: 2, host: 'mongodb-secondary2:27017'}]})"

          echo "Waiting for PRIMARY..."
          until mongosh --quiet mongodb-primary:27017/admin --authenticationDatabase admin --username $$MONGO_USER --password $$MONGO_PASSWORD --eval "db.hello().isWritablePrimary ? 1 : 0" 2>/dev/null | grep -q 1; do
            sleep 1
          done

          echo "Replica set initialized."

volumes:
  mongodb_primary_data:
  mongodb_secondary1_data:
  mongodb_secondary2_data:
  mongodb_keyfile:
Configuration Options
PORTPrimary node port(default: 27017)
MONGO_USERRoot username(default: admin)
MONGO_PASSWORDRoot password
4

Run the Deployment

Start all services defined in your compose file.

terminal
# Start the containers in detached mode
docker compose up -d

# Check if containers are running
docker compose ps

# View logs
docker compose logs -f
5

Configure Network Access

Open the port so you can access the application externally.

terminal
# Allow the application port through firewall
sudo ufw allow 27017/tcp
sudo ufw reload

# Access your app at:
# http://your-server-ip:27017
Skip the Terminal

Want one-click deploys? Try Server Compass.

No terminal needed. Deploy MongoDB Replica Set through a visual dashboard with automatic configuration.

  • Visual configuration UI
  • One-click deployment
  • Automatic SSL setup
  • Zero-downtime updates
  • Built-in monitoring
  • One-click rollbacks
Download Server Compass$29 one-time • Lifetime license

After Deployment

After deploying MongoDB Replica Set with Server Compass, complete these steps to finish setup

1

Verify replica set status with rs.status() (auto-initialized)

2

Create application users and databases

3

Configure backup schedules

4

Set up monitoring for all nodes

Need help? Check out our documentation for detailed guides.

MongoDB Replica Set FAQ

Common questions about self-hosting MongoDB Replica Set

How do I deploy MongoDB Replica Set with Server Compass?

Simply download Server Compass, connect to your VPS, and select MongoDB Replica Set from the templates list. Fill in the required configuration and click Deploy. The entire process takes under 3 minutes.

What are the system requirements for MongoDB Replica Set?

MongoDB Replica Set requires a minimum of 2048MB RAM. We recommend a VPS with at least 4096MB RAM for optimal performance. Any modern Linux server with Docker support will work.

Can I migrate my existing MongoDB Replica Set data?

Yes! Server Compass provides volume mapping that allows you to import existing data. You can also use standard MongoDB Replica Set backup and restore procedures.

How do I update MongoDB Replica Set to the latest version?

Server Compass makes updates easy. Simply click the Update button in your deployment dashboard, and the latest MongoDB Replica Set image will be pulled and deployed with zero downtime.

Is MongoDB Replica Set free to self-host?

MongoDB Replica Set is open-source software. You only pay for your VPS hosting (typically $5-20/month) and optionally Server Compass ($29 one-time). No subscription fees or per-seat pricing.

Ready to Self-Host MongoDB Replica Set?

Download Server Compass and deploy MongoDB Replica Set to your VPS in under 3 minutes. No Docker expertise required.

Download Server Compass