Obtain server certificates to enable TLS encryption for your self-managed MongoDB replica set deployments.
Important
These steps apply to self-managed MongoDB deployments. MongoDB Atlas clusters use TLS by default. If you use Cloud Manager or Ops Manager, configure TLS through your deployment management tool.
Before you Begin
Before you start, ensure you have the following information and resources:
You have a self-managed MongoDB replica set deployment that you want to secure with TLS.
You have at least one admin user enabled on your deployment to verify TLS connections in later tutorials. If you want to enable X.509 client authentication, the admin user must have at least the
userAdminrole to create and modify users in the$externaldatabase.You have a hostname for each node in your deployment, such as
mongo0.example.com,mongo1.example.com, andmongo2.example.com. If you are using a public CA, you must have a registered domain name that corresponds to these hostnames.You have OpenSSL installed on your machine.
If you are planning on using a public CA, such as Let's Encrypt or DigiCert, you know which public CA you are using. If you are planning on using a private CA, you have access to your organization's PKI information. The process for obtaining certificates might be different based on the CA you use. However, you must end with the same
.pemfiles described in the final result section of this tutorial.You have your preferred command line interface open.
You know your deployment TLS configuration requirements and whether your certificates need
clientAuthEKU based on the TLS Planning page.
Steps
This tutorial creates one certificate called mongo0.pem for the first node
in your deployment. When generating certificates for additional nodes, be specific
in your file names. For example, name the certificate for your first secondary node
mongo1.pem.
Follow this tutorial to obtain a server certificate from a public CA. You must obtain a certificate for each node in your deployment.
Generate a private key
Note
If you are using a CA that uses the ACME protocol like Let's Encrypt,
the CA generates the certificate for you. In this case,
follow your CA's instructions instead and resume this tutorial at
the step where you create .pem files from your certificate
and private key.
Generate a private key with OpenSSL:
openssl genrsa -out mongo0.key 4096
This generates a 4096-bit RSA private key. If you need to use a different key size or algorithm, see the OpenSSL documentation.
Restrict permissions on the generated mongo0.key file:
chmod 600 mongo0.key
Keep this file secret. The server uses this key to prove it owns the certificate.
Create a certificate signing request
Important
Starting in April 2026, major public CAs stopped issuing TLS
certificates that support client authentication. If you are using
a public CA, do not include extendedKeyUsage = clientAuth
in your CSR.
If you need server certificates for client authentication, use a private CA.
See the Public Certificate Authority Policy Changes Affecting mTLS
Technical Advisory for more information.
First, create a minimal OpenSSL configuration file called csr.conf that uses the
following format:
[ req ] distinguished_name = dn prompt = no req_extensions = req_ext # Replace values in this section with your own information. [ dn ] C = US # 2-letter country code ST = New-York # State or province L = New York City # City or locality O = Example Corp # Organization name CN = mongo0.example.com # Hostname of your MongoDB node [ req_ext ] subjectAltName = @alt_names keyUsage = digitalSignature extendedKeyUsage = serverAuth # Replace values in this section with any alternative # hostnames or IP addresses for your MongoDB node. [ alt_names ] DNS.1 = mongo0.example.com DNS.2 = localhost IP.1 = 127.0.0.1
For more information about the fields in this file, see the OpenSSL
config file documentation.
Ensure that you fill out both the Common Name (CN) and Subject Alternative
Name (alt_names) fields in the configuration file.
Then, generate a CSR:
openssl req -new -key mongo0.key -out mongo0.csr -config csr.conf
This creates mongo0.csr. You will submit this file to
the CA.
Submit the CSR to a public CA
Sign in to your chosen public CA's portal and follow their instructions
to submit your CSR. When prompted, upload or paste the contents of your
mongo0.csr file. Choose the appropriate validation level:
Domain Validation (DV): The CA verifies that you control the domain for which you're requesting a certificate. This is typically the quickest and least expensive option.
Organization/Extended Validation (OV/EV): Choose this if your organization requires a higher level of trust.
Follow the CA's instructions to complete the validation process. After successful completion, the CA issues your certificate.
Download and verify the certificate and chain
After validation, download:
Your server certificate, stored as
mongo0.crtThe
.pemCA intermediate bundle, stored asca.pem
After you receive or create your certificate, verify that it matches the private key:
# Compare the modulus of the key and certificate openssl rsa -noout -modulus -in mongo0.key | openssl sha256 openssl x509 -noout -modulus -in mongo0.crt | openssl sha256
The output digests must match. If they do not, the certificate and key do not belong together, and MongoDB will not be able to use them.
If your CA provided a separate chain file, you can also inspect it:
openssl x509 -in mongo0.crt -text -noout openssl x509 -in ca.pem -text -noout
Confirm that the certificate's Issuer and Subject fields, as well as
validity dates, look correct.
Organize the certificate and chain files
Copy your .pem files to the directory where you plan to store TLS
assets for MongoDB. You will reference these paths in your MongoDB configuration
file in the next tutorial.
You might need to copy the .pem files onto
the remote server on which you host your node. Additionally, ensure you
secure your files with appropriate permissions, such as read-only access for the owner.
In Linux/MacOS:
sudo mkdir -p /etc/ssl/mongodb sudo cp mongo0.pem ca.pem /etc/ssl/mongodb
In Windows PowerShell:
New-Item -ItemType Directory -Path C:\tls\mongodb -Force Copy-Item mongo0.pem,ca.pem ` -Destination C:\tls\mongodb
Repeat for other nodes in deployment
If you have multiple nodes or need multiple certificates for each node, obtain a certificate for each node following the same process.
Note
In your MongoDB deployment, all certificates must be signed by the same CA.
Therefore, you only need one CA certificate (ca.pem) and can skip downloading
it again when obtaining your additional certificates.
If your organization operates its own internal PKI or private CA, obtain certificates from that CA. You must obtain a certificate for each node in your deployment.
Generate a private key
Note
Your CA might use a different process for certificate generation,
such as a web interface. In this case,
follow your CA's instructions instead and resume this tutorial at
the step where you create .pem files from your certificate
and private key.
Generate a private key with OpenSSL:
openssl genrsa -out mongo0.key 4096
This generates a 4096-bit RSA private key. If you need to use a different key size or algorithm, see the OpenSSL documentation.
Restrict permissions on the generated mongo0.key file:
chmod 600 mongo0.key
Keep this file secret. The server uses this key to prove it owns the certificate.
Create a certificate signing request
Ensure that your CSR meets your organization's PKI policies.
Note
If you want intra-cluster mTLS between
nodes in your deployment, set extendedKeyUsage = serverAuth, clientAuth
in your CSR configuration file so that your nodes can authenticate
as both TLS servers and clients.
First, create a minimal OpenSSL configuration file called csr.conf that uses the
following format:
[ req ] distinguished_name = dn prompt = no req_extensions = req_ext # Replace values in this section with your own information. [ dn ] C = US # 2-letter country code ST = New-York # State or province L = New York City # City or locality O = Example Corp # Organization name CN = mongo0.example.com # Hostname of your MongoDB node [ req_ext ] subjectAltName = @alt_names keyUsage = digitalSignature extendedKeyUsage = serverAuth # Replace values in this section with any alternative # hostnames or IP addresses for your MongoDB node. [ alt_names ] DNS.1 = mongo0.example.com DNS.2 = localhost IP.1 = 127.0.0.1
For more information about the fields in this file, see the OpenSSL
config file documentation.
Ensure that you fill out both the Common Name (CN) and Subject Alternative
Name (alt_names) fields in the configuration file.
Then, generate a CSR:
openssl req -new -key mongo0.key -out mongo0.csr -config csr.conf
This creates mongo0.csr. You will submit this file to
the CA.
Download and verify the certificate and chain
After validation, download:
Your server certificate, stored as
mongo0.crtThe
.pemCA intermediate bundle, stored asca.pem
After you receive or create your certificate, verify that it matches the private key:
# Compare the modulus of the key and certificate openssl rsa -noout -modulus -in mongo0.key | openssl sha256 openssl x509 -noout -modulus -in mongo0.crt | openssl sha256
The output digests must match. If they do not, the certificate and key do not belong together, and MongoDB will not be able to use them.
If your CA provided a separate chain file, you can also inspect it:
openssl x509 -in mongo0.crt -text -noout openssl x509 -in ca.pem -text -noout
Confirm that the certificate's Issuer and Subject fields, as well as
validity dates, look correct.
Organize the certificate and chain files
Copy your .pem files to the directory where you plan to store TLS
assets for MongoDB. You will reference these paths in your MongoDB configuration
file in the next tutorial.
You might need to copy the .pem files onto
the remote server on which you host your node. Additionally, ensure you
secure your files with appropriate permissions, such as read-only access for the owner.
In Linux/MacOS:
sudo mkdir -p /etc/ssl/mongodb sudo cp mongo0.pem ca.pem /etc/ssl/mongodb
In Windows PowerShell:
New-Item -ItemType Directory -Path C:\tls\mongodb -Force Copy-Item mongo0.pem,ca.pem ` -Destination C:\tls\mongodb
Repeat for other nodes in deployment
If you have multiple nodes or need multiple certificates for each node, obtain a certificate for each node following the same process.
Note
In your MongoDB deployment, all certificates must be signed by the same CA.
Therefore, you only need one CA certificate (ca.pem) and can skip downloading
it again when obtaining your additional certificates.
Use self-signed certificates for local development and testing. Do not use self-signed certificates in production.
Generate a private key
Generate a private key with OpenSSL:
openssl genrsa -out mongo0.key 4096
This generates a 4096-bit RSA private key. If you need to use a different key size or algorithm, see the OpenSSL documentation.
Restrict permissions on the generated mongo0.key file:
chmod 600 mongo0.key
Keep this file secret. The server uses this key to prove it owns the certificate.
Create a configuration file
Note
If you want intra-cluster mTLS between
nodes in your deployment, set extendedKeyUsage = serverAuth, clientAuth
in your CSR configuration file so that your nodes can authenticate
as both TLS servers and clients.
Create a minimal OpenSSL configuration file called csr.conf that uses the
following format:
[ req ] distinguished_name = dn prompt = no req_extensions = req_ext # Replace values in this section with your own information. [ dn ] C = US # 2-letter country code ST = New-York # State or province L = New York City # City or locality O = Example Corp # Organization name CN = mongo0.example.com # Hostname of your MongoDB node [ req_ext ] subjectAltName = @alt_names keyUsage = digitalSignature extendedKeyUsage = serverAuth # Replace values in this section with any alternative # hostnames or IP addresses for your MongoDB node. [ alt_names ] DNS.1 = mongo0.example.com DNS.2 = localhost IP.1 = 127.0.0.1
For more information about the fields in this file, see the OpenSSL
config file documentation.
Ensure that you fill out both the Common Name (CN) and Subject Alternative
Name (alt_names) fields in the configuration file.
Verify the certificate
After you receive or create your certificate, verify that it matches the private key:
# Compare the modulus of the key and certificate openssl rsa -noout -modulus -in mongo0.key | openssl sha256 openssl x509 -noout -modulus -in mongo0.crt | openssl sha256
The output digests must match. If they do not, the certificate and key do not belong together, and MongoDB will not be able to use them.
Create .pem files
You must combine your certificate and its private key to create a .pem
file. For example, on Linux or MacOS:
cat mongo0.crt mongo0.key > mongo0.pem
In Windows PowerShell:
type mongo0.crt mongo0.key > mongo0.pem
You can use your self-signed certificate as the CA file. For consistency with this tutorial, rename the certificate:
cp mongo0.crt ca.pem
Organize the certificate and chain files
Copy your .pem files to the directory where you plan to store TLS
assets for MongoDB. You will reference these paths in your MongoDB configuration
file in the next tutorial.
You might need to copy the .pem files onto
the remote server on which you host your node. Additionally, ensure you
secure your files with appropriate permissions, such as read-only access for the owner.
In Linux/MacOS:
sudo mkdir -p /etc/ssl/mongodb sudo cp mongo0.pem ca.pem /etc/ssl/mongodb
In Windows PowerShell:
New-Item -ItemType Directory -Path C:\tls\mongodb -Force Copy-Item mongo0.pem,ca.pem ` -Destination C:\tls\mongodb
Final Result
At the end of this tutorial, you have the following .pem files
in /etc/ssl/mongodb:
For each node, you have a
.pemfile that contains the certificate and private key for that node, such asmongo0.pem,mongo1.pem, andmongo2.pem.For the deployment overall, you have the intermediate CA certificate that issued the certificates for each node, such as
ca.pem.
Next Steps
To learn how to configure TLS for your self-managed MongoDB deployment, continue to the next tutorial, Configure TLS for a Self-Managed Deployment.