For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Connect to a TLS-Enabled Replica Set

This tutorial shows you how to connect mongosh to a TLS-enabled self-managed replica set.

Before you start, verify that you have the following:

If you do not want to use mutual TLS or X.509 client authentication, follow this procedure. If you set allowConnectionsWithoutCertificates to true in your server configuration, clients can connect without presenting a certificate and authenticate with SCRAM instead.

1

Connect mongosh to your replica set using the following options:

mongosh "mongodb://mongo0.example.com:27017,mongo1.example.com:27017,mongo2.example.com:27017" \
--tls --tlsCAFile /etc/ssl/mongodb/ca.pem
  • --tls enables TLS encryption for the connection.

  • --tlsCAFile is set to the CA certificate that signed the server certificates so that mongosh can verify the server's certificates.

Connecting without a client certificate establishes a TLS-encrypted session but does not authenticate you. To run most commands, you must authenticate with a mechanism such as SCRAM.

2

Use the db.auth() method to authenticate with your admin username and password.

3

Run the following to confirm your authentication state:

db.adminCommand({ connectionStatus: 1 })

Confirm that authInfo.authenticatedUsers includes the expected user.

After completing this tutorial, mongosh is connected to your replica set over an encrypted TLS connection, authenticated with either SCRAM or X.509.