A MongoDB connection can succeed against its seed address and still fail moments later. Replica-set discovery gives the driver a list of members, and the driver then connects to those names. A certificate that covers only a convenient database alias leaves the discovered members unverifiable. This guide builds the certificate plan around that behavior instead of treating TLS as a switch on one listening port.
The example assumes a self-managed MongoDB 8.x replica set, Linux, SCRAM application users, existing keyfile membership authentication, and three DNS names: mongo1.example.com, mongo2.example.com, and mongo3.example.com. It is not an Atlas configuration or an X.509 membership migration. Use documentation matching your precise server and driver releases. Commands illustrate a deployment plan; no database, certificate issuance, or failover test was performed for this article.
Step 1: Inventory the names that MongoDB actually advertises
Connect through your current administrative access and inspect the replica-set configuration. Record each member's host field, the set name, the ports, the client networks, and any split-horizon configuration. Work from those values rather than a diagram someone last updated before a migration. A client must resolve the advertised names to reachable addresses and verify them against certificate subject alternative names.
rs.conf().members.map(m => ({id: m._id, host: m.host}))
rs.status().members.map(m => ({name: m.name, state: m.stateStr}))For this example the set is rs0 and every member advertises its own fully qualified name on port 27017. Internal DNS may map those names to private addresses. Public reachability is unnecessary for DNS-01 certificate validation, but the names must be under a domain you control. Public certificate authorities will not issue certificates for an unqualified name such as mongo1 or a private address such as 10.20.0.11.
Decide whether to use individual certificates or a wildcard. Individual keys keep one member's compromise from automatically exposing another member's key. A wildcard can simplify name coverage but increases the consequence of distributing the same key. A wildcard for *.example.com does not cover mongo1.db.example.com. Include a connection alias only when clients genuinely use it, and do not change replica-set hostnames merely to hide a certificate mistake.
Before changing anything, take an appropriate database backup and copy the effective mongod configuration and existing TLS files into a protected rollback directory. Record current primary and secondary health. A copy of the data directory while mongod is writing is not a consistent backup. The TLS maintenance plan should not become an accidental experiment in database recovery.
Step 2: Prepare issuance separately from database membership
Create the required domain registration in SimpleACME before configuring your renewal client. Copy the assigned challenge CNAME exactly into the authoritative DNS zone and check that it resolves. Active paid access enables the delegated TXT updates used for validation. The free preparation account does not supply a working update endpoint for issuance.
Use the public Certbot integration guide and the subscriber Certbot instructions for the supported challenge setup. Your ACME client requests the certificates, keeps the private keys, and later deploys the results. SimpleACME handles the delegated DNS update; it does not become MongoDB's certificate authority, issue the certificate itself, or configure database authentication.
Keep membership trust separate. Modern publicly issued certificates should not be assumed to support client authentication. In particular, do not reuse an ACME server certificate as an X.509 cluster identity without checking its extended key usage and MongoDB's membership requirements. This example retains the existing keyfile and follows MongoDB's documented server-TLS approach for that authentication model. A cluster already using X.509 membership needs its own migration design and private membership CA.
The distinction matters during incident response. Losing a challenge credential permits updates for its delegated validation target, whereas losing a database membership secret affects a different trust boundary. Store both carefully and record which automation owns each. Do not put MongoDB passwords, private keys, or challenge credentials into a public repository or an AI prompt while preparing this configuration.
Step 3: Assemble a MongoDB-readable PEM and trust bundle
For each member, place its own private key and certificate chain in a single PEM file readable by the mongod service account. Use a staging directory with restrictive permissions and validate the materials before switching the live path. Substitute your actual account name; some packages use mongodb rather than mongod. The public certificate chain is not secret, but its neighboring private key is.
umask 077
cat /secure/issued/mongo1/privkey.pem \
/secure/issued/mongo1/fullchain.pem > /secure/stage/mongo1.pem
openssl x509 -in /secure/issued/mongo1/cert.pem \
-noout -subject -issuer -dates -ext subjectAltName
openssl pkey -in /secure/issued/mongo1/privkey.pem -pubout |
openssl sha256
openssl x509 -in /secure/issued/mongo1/cert.pem -pubkey -noout |
openssl sha256The two public-key digests must match. Inspect the actual chain as well as the leaf name. Configure CAFile with an appropriate trusted CA bundle for the connection model, not the leaf certificate renamed ca.pem. The server's chain file helps peers build a chain; the trust bundle expresses which roots are accepted. They are related but serve different purposes.
Test readability as the real service identity. Directory traversal permissions and mandatory access-control labels can block a file even when its mode looks correct. Do not solve a permission error by making every private key world-readable. Copying only a Certbot live symlink into a container or restricted directory also fails if the corresponding archive target is unavailable. A deliberately deployed regular file avoids that ambiguity.
Step 4: Roll through TLS modes without partitioning the set
For a new TLS rollout, use MongoDB's staged sequence rather than switching every member straight to requireTLS. The intermediate configuration below is a merge example, not a replacement for your full mongod.conf. Preserve storage, replication, bind addresses, authorization, and existing keyfile settings. The documented keyfile approach withholds member client certificates so a public server certificate is not accidentally treated as a cluster client identity.
net:
tls:
mode: allowTLS
certificateKeyFile: /etc/mongodb/tls/mongo1.pem
CAFile: /etc/mongodb/tls/trusted-roots.pem
allowConnectionsWithoutCertificates: true
security:
clusterAuthMode: keyFile
keyFile: /etc/mongodb/keyfile
setParameter:
tlsWithholdClientCertificate: trueAllowing connections without client certificates does not grant unauthenticated database access. Existing SCRAM authorization remains necessary. It simply avoids requiring every application to present a client TLS certificate. Conversely, encrypted transport alone does not repair an installation with authorization disabled or a firewall that exposes the database to the world.
Move one secondary at a time, confirm it returns to SECONDARY, and keep a majority available. After the set accepts TLS everywhere, move through preferTLS so outgoing member connections use TLS, then requireTLS after all applications have migrated. Follow the official rolling procedure for your version and primary handover policy. Do not restart several voting members together just because the configuration file is identical.
Step 5: Validate discovery, failover, and renewal deployment
Use a real driver connection that names the replica set and keeps hostname verification enabled. Prompt for secrets or obtain them from your approved secret store; avoid credentials in shell history. A successful direct connection to one node is a useful diagnostic but does not prove discovery works across the set.
mongosh 'mongodb://mongo1.example.com:27017,mongo2.example.com:27017,mongo3.example.com:27017/?replicaSet=rs0&tls=true' \
--username appcheck --authenticationDatabase admin --password \
--tlsCAFile /etc/ssl/certs/ca-certificates.crt
openssl s_client -connect mongo2.example.com:27017 \
-servername mongo2.example.com -verify_hostname mongo2.example.com \
-verify_return_error </dev/nullCheck each member from the application network, then run a controlled read and write with a test account limited to a disposable test database. Observe connection pools after a planned primary transition. If an address mismatch appears only after discovery, inspect the advertised hostname and its SAN instead of adding tlsAllowInvalidHostnames or tlsInsecure. Those options can conceal the exact identity failure you are trying to prevent.
Renewal needs a deployment transaction: validate the new files, preserve the prior generation, replace the member's PEM with correct ownership, and use the certificate rotation mechanism supported by your exact release or a controlled rolling restart. Do not assume editing a file changes an established TLS session. Verify a fresh connection's serial number and expiry. Alert separately on renewal-client failure, deployment failure, and the certificate actually served by each member.
If a member cannot rejoin, restore its previous certificate generation and configuration before proceeding elsewhere. Keep the remaining members untouched while diagnosing. Avoid automatically rolling the entire cluster back to plaintext; recovery should preserve the strongest known working state. Record whether the fault was name coverage, trust, file access, or authentication so the next renewal does not reproduce it.
A concrete acceptance record for the replica set
Keep a member-by-member acceptance record rather than one global green check. For mongo1, record that its DNS name resolves from the application subnet, the expected process answers on 27017, the certificate verifies for that name, and the node remains in the intended replica-set state. Repeat for the other members. Include the driver's discovered addresses so the record reflects what applications will use after a topology refresh.
Connection pools deserve a second observation after the maintenance window. A process may retain healthy existing sockets while new workers cannot establish verified sessions. Restart a controlled test application worker or deliberately establish a new client connection, then compare behavior with an already-running worker. This separates certificate activation from the misleading reassurance of uninterrupted old connections.
Also consider diagnostic consumers such as backup agents and monitoring exporters. They may connect with different hostnames or maintain their own CA files. Give each consumer owner the canonical names and intended trust policy. An exporter that cannot reconnect should generate a precise alert rather than drive a broad relaxation of MongoDB TLS verification. Completing this inventory makes both routine renewal and later member replacement more predictable.
Frequently asked questions
Why does the seed connection work while the application says no primary is available?
The driver may have learned member names that differ from the seed alias. Resolve and connect to every advertised member from the application's network, then compare each name with its certificate SAN. A healthy replica set can still be unusable to one client network because discovery points at unreachable or unverifiable addresses.
Can an ACME certificate replace my X.509 membership certificate?
Not automatically. Server authentication and membership authentication have different requirements. Keep private cluster identities and their CA unless you have explicitly designed an X.509 transition. An ACME certificate's public trust does not prove it is suitable for client authentication or MongoDB member recognition.
Does SimpleACME need access to my database?
No. The challenge update uses the domain's precreated delegated registration. The ACME client and your deployment automation handle certificate files on the database hosts. A TXT activity event in the portal shows challenge activity, not that a member loaded a certificate or that replication succeeded.
Should I issue a certificate for the private IP instead?
Use a controlled DNS hostname and private DNS resolution for private deployments. Public ACME issuance is not a route to trusted certificates for private IP addresses. Keep the hostname stable across routing changes, and test the same name your driver will actually verify.
Official references
MongoDB server TLS rollout explains the keyfile and membership alternatives. MongoDB client TLS connections documents verified replica-set connections. MongoDB certificate preparation covers the required certificate materials. Check these pages against your installed release before applying changes.