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.
See how MongoDB 9.0 delivers up to 2x higher throughput.
MongoDB Branding Shape
Register now >
Docs Menu

Configure X.509 Certificate Authentication

In the X.509 authentication mechanism, the server and client use the TLS protocol to exchange X.509 public-key certificates. You can use this mechanism to authenticate to MongoDB Atlas, MongoDB Enterprise Advanced, and MongoDB Community Edition.

Tip

X.509 Mechanism

To learn how to use TLS/SSL with the Scala driver, see the Configure Transport Layer Security (TLS) guide.

For more information about X.509 certificates, see Use x.509 Certificates to Authenticate Clients on Self-Managed Deployments in the MongoDB Server manual.

The code examples on this page use the following placeholders:

  • <hostname>: The network address of your MongoDB deployment.

  • <port>: The port number of the MongoDB deployment. If you omit this parameter, the driver uses the default port number (27017). You don't need a port number to connect to a MongoDB Atlas cluster.

To use the code examples, replace these placeholders with your own values.

Select the Connection String or the MongoCredential tab for instructions and a code example:

To specify X.509 authentication with a connection string, set the authMechanism parameter to MONGODB-X509 and the tls parameter to true:

val mongoClient = MongoClient("mongodb://<hostname>:<port>/?authMechanism=MONGODB-X509&tls=true")

The MongoCredential class represents an authentication credential and includes static factory methods for each authentication mechanism. To specify X.509 authentication with a MongoCredential object, call the createMongoX509Credential() factory method. To enable TLS, also call applyToSslSettings() and set the enabled property to true:

val credential = MongoCredential.createMongoX509Credential()
val mongoClient = MongoClient(MongoClientSettings
.builder()
.applyToClusterSettings(builder =>
builder.hosts(Collections.singletonList(ServerAddress("<hostname>", <port>))))
.applyToSslSettings(builder =>
builder.enabled(true))
.credential(credential)
.build())

To learn more about any of the MongoDB methods and types used on this page, see the following API documentation: