TLS Certificates and Secure Communication

How to generate TLS certificates for MongoDB server and client communication.

This page explains how to generate TLS certificates for secure communication in MongoDB deployments. It covers both server and client certificates, for use in TLS encryption only — not for mutual (X.509) authentication.


Prerequisites

  • OpenSSL installed

  • A working directory such as /tmp/mongo_tls

  • A custom Certificate Authority (CA) to sign certificates


1. Create a Certificate Authority (CA)

This CA will sign all server and client certificates.

mkdir -p /tmp/mongo_tls
cd /tmp/mongo_tls

# Generate CA key
openssl genrsa -out ca.key.pem 4096

# Generate CA certificate
openssl req -x509 -new -nodes -key ca.key.pem -sha256 -days 3650 -out ca.cert.pem -subj "/C=CH/L=Zurich/O=MongoCA/CN=mongodb.local.ca"

2. Generate a Server Certificate (for TLS communication)

This certificate is used by the MongoDB server to secure incoming connections.


3. Generate a Client Certificate (for TLS communication)

This certificate allows clients (e.g. mongosh) to use encrypted communication.


4. Combine PEM Files

MongoDB expects the certificate and private key in the same file:


5. Change MongoDB configuration

Move the three files to the $MONGO_BASE/admin/$MONGO_INSTANCE/secret directory.

Add the net.tls section to your configuration file.

mongosh connection:


Automated Script Example

Last updated