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
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