How to Forward Logs from a MongoDB Cluster

MongoDB is a source-available cross-platform document-oriented database program for high-volume storage. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas.


Log forwarding allows you to transmit log data from any number of sources to a centralized database cluster. You can do so by creating and managing log sinks for your database clusters via the control panel or the API. MongoDB supports forwarding to OpenSearch, Elasticsearch, and Rsyslog. You can 1-Click deploy these tools to a Droplet from the Databases section of the DigitalOcean Marketplace.

You can only forward logs from Managed MongoDB to Managed OpenSearch if you add the MongoDB cluster’s node IPs to the OpenSearch cluster’s trusted sources.

Create a Log Sink Using the API

To create a database using the API, you need to provide values for the sink_name, sink_type, and config fields, which specify the name of the log sink, its type (elasticsearch or opensearch), and your desired URL or server/port configuration, respectively.

How to create a log sink using the DigitalOcean API

To set up log forwarding:

  1. Create a personal access token with the appropriate scope, and save it for use with the API.

  2. Send a POST request to /v2/databases/$DATABASE_ID/logsink.

Payload

    
        
            
{
  "config": {
    "server": "192.168.0.1",
    "port": 514,
    "tls": false,
    "format": "rfc5424"
  }
}

        
    

cURL

    
        
            
curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
  -d '{"sink_name": "logsink", "sink_type": "elasticsearch", "config": {"server": "192.168.10.1", "port": 514, "tls": false, "format": "rfc5424"}}' \
  "https://api.digitalocean.com/v2/databases/9cc10173-e9ea-4176-9dbc-a4cee4c4ff30/logsink"

        
    

Forward Logs Using the Control Panel

To forward logs from a database cluster in the control panel, go to the Databases page, then select your database cluster. Click the name of the cluster to go to its Overview page, then click the Settings tab.

Cluster settings page.

On the Settings page, in the Log forwarding section, click Edit. Select the service you want to forward logs to.

Log forwarding service selection screen.

Enter your destination name, endpoint URL, and index prefix. You can also click Show advanced configuration options to edit the log sink’s maximum days to store logs, request timeout (in seconds), and CA certificate for authentication. To confirm your changes, click Add destination.

Forward logs to OpenSearch popup.