Portabase Logo

MongoDB

Configuration for MongoDB.

The agent will use mongodump to perform backups and mongorestore to restore backups.

Configuration

When running portabase db add, select mongodb as the database type.

In your databases.json (or .toml) file, configure the following block.

With Authentication

databases.json
{
  "name": "Database - MongoDB Auth",
  "database": "testdbauth",
  "type": "mongodb",
  "username": "username",
  "password": "password",
  "port": 27017,
  "host": "db-mongodb-auth",
  "generated_id": "..."
}

Without Authentication

databases.json
{
  "name": "my-mongo",
  "type": "mongodb",
  "host": "db-mongodb",
  "port": 27017,
  "database": "testdb",
  "generated_id": "..."
}
Generate UUID for your configuration
Generating...

MongoDB Atlas / Cloud (SRV)

Managed MongoDB clusters (MongoDB Atlas and equivalents) are reached through a DNS SRV record instead of a fixed host and port. The connection string uses the mongodb+srv:// scheme.

To use an SRV connection, omit the port field (or set it to 0). The agent detects this and automatically switches to mongodb+srv://. Use the cluster hostname (ending in .mongodb.net) as the host.

Via CLI: run portabase db add, choose mongodb, then leave the port empty to enable the SRV connection.

databases.json
{
  "name": "MongoDB Cluster Cloud",
  "database": "mydb",
  "type": "mongodb",
  "username": "username",
  "password": "password",
  "host": "cluster0.abcde.mongodb.net",
  "generated_id": "..."
}
Generate UUID for your configuration
Generating...

No port is set for SRV connections. When port is absent (or 0), the agent builds a mongodb+srv://user:password@host/database?authSource=admin URI. With authentication, the credentials are URL-encoded automatically and authSource=admin is appended. Without a username and password, the URI is built without credentials or query string.

Docker Compose Example

Example with a MongoDB image.

docker-compose.yml
services:
    db-mongodb-auth:
        container_name: db-mongodb-auth
        image: mongo:latest
        ports:
          - "27082:27017"
        environment:
          MONGO_INITDB_ROOT_USERNAME: root
          MONGO_INITDB_ROOT_PASSWORD: rootpassword
          MONGO_INITDB_DATABASE: testdbauth
        command: mongod --auth
        networks:
          - portabase
        volumes:
          - mongodb-data-auth:/data/db
        healthcheck:
          test: [ "CMD", "mongo", "--eval", "db.adminCommand('ping')" ]
          interval: 5s
          timeout: 5s
          retries: 10

    db-mongodb:
        container_name: db-mongodb
        image: mongo:latest
        ports:
          - "27083:27017"
        volumes:
          - mongodb-data:/data/db
        healthcheck:
          test: [ "CMD", "mongosh", "--eval", "db.adminCommand('ping')" ]
          interval: 5s
          timeout: 5s
          retries: 10
        environment:
          MONGO_INITDB_DATABASE: testdb
        networks:
          - portabase

    agent:
        image: portabase/agent:latest
        # ... agent configuration ...
        depends_on:
          - db-mongodb
          - db-mongodb-auth
        networks:
          - portabase

networks:
  portabase:
    external: true

volumes:
  mongodb-data:
  mongodb-data-auth:

If you use localhost as the host (because the agent is on the host machine and not in Docker, or via host-gateway), ensure your database is listening on all interfaces (0.0.0.0) or is accessible from the agent.

Last updated on

On this page