> This page location: /docs.md > /docs/management.md > /docs/v8.0/core/self-managed-security.md > /docs/v8.0/core/security-users.md > /docs/v8.0/core/authentication.md > /docs/v8.0/core/security-x.509.md

> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

<!--
Tab options on this page. Append to the .md URL to filter:
  ?tabs=<id,...>   select specific tabs (e.g. ?tabs=nodejs,shell)
  ?allTabs=true    include every tab
  (no param)       default: one tab per tabset

Each tab below is delimited by  comments.

Available tabs:
  other tabs: cmdline, cfg, connect, uri-auth, authafter
-->

# Use X.509 to Authenticate Clients on Self-Managed MongoDB

The following procedure sets up X.509 certificate authentication for client authentication on a standalone [`mongod`](https://www.mongodb.com/docs/v8.0/reference/program/mongod.md#mongodb-binary-bin.mongod) instance. This is also known as Mutual TLS or mTLS.

To use X.509 authentication for replica sets or sharded clusters, see [Verify Cluster Membership with X.509 on Self-Managed MongoDB.](https://www.mongodb.com/docs/v8.0/tutorial/configure-x509-member-authentication.md)

## Prerequisites

A full description of TLS/SSL, PKI (Public Key Infrastructure) certificates, in particular X.509 certificates, and Certificate Authority is beyond the scope of this document. This tutorial assumes prior knowledge of TLS/SSL as well as access to valid X.509 certificates.

### Certificate Authority

For production use, your MongoDB deployment should use valid certificates generated and signed by a certificate authority. You or your organization can generate and maintain an independent certificate authority, or use certificates generated by third-party TLS vendors. Obtaining and managing certificates is beyond the scope of this documentation.

To use X.509 authentication, `--tlsCAFile` or `net.tls.CAFile` must be specified unless you are using `--tlsCertificateSelector` or `--net.tls.certificateSelector`.

### Client X.509 Certificate

You must have valid X.509 certificates. The client X.509 certificates must meet the [client certificate requirements.](https://www.mongodb.com/docs/v8.0/core/security-x.509.md#std-label-client-x509-certificates-requirements)

If you specify `--tlsAllowInvalidCertificates` or `net.tls.allowInvalidCertificates: true`, an invalid certificate is sufficient only to establish a TLS connection but it is *insufficient* for authentication.

## Procedure

1. Deploy with X.509 Authentication

         To set up X.509 authentication for replica sets or sharded clusters, see [Verify Cluster Membership with X.509 on Self-Managed MongoDB.](https://www.mongodb.com/docs/v8.0/tutorial/configure-x509-member-authentication.md#std-label-x509-internal-authentication)

2. Add X.509 Certificate `subject` as a User

   To authenticate with a client certificate, you must first add the value of the `subject` from the client certificate as a MongoDB user to the `$external` database. Each unique X.509 client certificate corresponds to a single MongoDB user. You cannot use a single client certificate to authenticate more than one MongoDB user.

   **Note: Username Requirements**

   - To use [Client Sessions and Causal Consistency Guarantees](https://www.mongodb.com/docs/v8.0/core/read-isolation-consistency-recency.md#std-label-sessions) with `$external` authentication users (Kerberos, LDAP, or X.509 users), usernames cannot be greater than 10k bytes.

   - The RDNs in the `subject` string must be compatible with the [RFC2253](https://www.ietf.org/rfc/rfc2253.txt) standard.

   1. You can retrieve the `RFC2253` formatted `subject` from the client certificate with the following command:

      ```bash
      openssl x509 -in <pathToClientPEM> -inform PEM -subject -nameopt RFC2253
      ```

      The command returns the `subject` string and the certificate:

      ```bash
      subject= CN=myName,OU=myOrgUnit,O=myOrg,L=myLocality,ST=myState,C=myCountry
      -----BEGIN CERTIFICATE-----
      # ...
      -----END CERTIFICATE-----
      ```

   2. Add the `RFC2253` compliant value of the `subject` as a user. Omit spaces as needed.

      The following example adds a user and grants the user [`readWrite`](https://www.mongodb.com/docs/v8.0/reference/built-in-roles.md#mongodb-authrole-readWrite) role in the `test` database and the [`userAdminAnyDatabase`](https://www.mongodb.com/docs/v8.0/reference/built-in-roles.md#mongodb-authrole-userAdminAnyDatabase) role:

      ```javascript
      db.getSiblingDB("$external").runCommand(
        {
          createUser: "CN=myName,OU=myOrgUnit,O=myOrg,L=myLocality,ST=myState,C=myCountry",
          roles: [
               { role: "readWrite", db: "test" },
               { role: "userAdminAnyDatabase", db: "admin" }
          ],
          writeConcern: { w: "majority" , wtimeout: 5000 }
        }
      )
      ```

      See [Manage Users and Roles on Self-Managed Deployments](https://www.mongodb.com/docs/v8.0/tutorial/manage-users-and-roles.md) for details on adding a user with roles.

3. Authenticate with a X.509 Certificate

   After you have [added the X.509 client certificate subject as a corresponding MongoDB user](https://www.mongodb.com/docs/v8.0/tutorial/configure-x509-client-authentication.md#std-label-addX509SubjectUser), you can authenticate with the client certificate:

      <!--tab tabid="uri-auth"-->

   ### Connection String

   To authenticate during connection with authentication options specified in the connection string, run following command:

   ```bash
   mongosh 'mongodb://<host>:<port>/?tls=true&tlsCertificateKeyFile=</path/to/client>.pem&tlsCAFile=</path/to/rootCA>.pem&authSource=$external&authMechanism=MONGODB-X509'
   ```

   | Option | Description |
   | --- | --- |
   | `tls` | Set to `true`. |
   | `tlsCertificateKeyFile` | Path to a PEM file that contains a private key and an X.509 certificate. |
   | `tlsCAFile` | Path to the Certificate Authority CA) file to verify the certificate presented by the [`mongod`](https://www.mongodb.com/docs/v8.0/reference/program/mongod.md#mongodb-binary-bin.mongod) instance. |
   | `authSource` | Specify `'$external'`. |
   | `authMechanism` | Specify `MONGODB-X509`. |

   <!--/tab-->

   ## Next Steps

To use X.509 authentication for replica sets or sharded clusters, see [Verify Cluster Membership with X.509 on Self-Managed MongoDB.](https://www.mongodb.com/docs/v8.0/tutorial/configure-x509-member-authentication.md)
