Database Manual / Security / Encryption / TLS/SSL

Configure mongod and mongos for TLS/SSL on Self-Managed Deployments在自我管理部署上配置mongodmongos的TLS/SSL

Overview概述

This document helps you to configure a new MongoDB instance to support TLS/SSL. For instructions on upgrading a cluster currently not using TLS/SSL to using TLS/SSL, see Upgrade a Cluster to Use TLS/SSL instead.本文档帮助您配置新的MongoDB实例以支持TLS/SSL。有关将当前未使用TLS/SSL的群集升级为使用TLS/SSL的说明,请参阅将群集升级为改用TLS/SSL

To set up a local development environment with TLS/SSL, see Developing MongoDB Locally with TLS.要使用TLS/SSL设置本地开发环境,请参阅使用TLS在本地开发MongoDB

MongoDB uses the native TLS/SSL OS libraries:MongoDB使用本机TLS/SSL操作系统库:

Platform平台TLS/SSL Library
WindowsSecure Channel (Schannel)安全通道(Schannel)
Linux/BSDOpenSSL
macOSSecure Transport安全传输

Note

  • MongoDB disables support for TLS 1.0 encryption on systems where TLS 1.1+ is available.MongoDB在TLS 1.1+可用的系统上禁用对TLS 1.0加密的支持。
  • MongoDB's TLS/SSL encryption only allows the use of strong TLS/SSL ciphers with a minimum of 128-bit key length for all connections.MongoDB的TLS/SSL加密只允许对所有连接使用最小128位键长度的强TLS/SSL密码。
  • The Linux 64-bit legacy x64 builds of MongoDB do not include support for TLS/SSL.MongoDB的Linux 64位旧x64版本不包括对TLS/SSL的支持。

Prerequisites先决条件

Important

A full description of TLS/SSL, PKI (Public Key Infrastructure) certificates, and Certificate Authority is beyond the scope of this document. This page assumes prior knowledge of TLS/SSL as well as access to valid certificates.TLS/SSL、PKI(公钥基础设施)证书和证书颁发机构的完整描述超出了本文档的范围。本页假设您已具备TLS/SSL的先验知识以及有效证书的访问权限。

Certificate Authorities证书颁发机构

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.对于生产使用,MongoDB部署应该使用由证书颁发机构生成和签名的有效证书。您或组织可以生成和维护独立的证书颁发机构,或使用第三方TLS供应商生成的证书。获取和管理证书超出了本文档的范围。

Member Certificate Requirements会员证书要求

When TLS is enabled, use member certificates to verify membership to internal connections in a sharded cluster or a replica set. 启用TLS后,使用成员证书验证分片集群或副本集中内部连接的成员资格。You can configure member certificate file paths with the net.tls.clusterFile and net.tls.certificateKeyFile options. 您可以使用net.tls.clusterFilenet.tls.certificateKeyFile选项配置成员证书文件路径。Members have the following configuration requirements:成员具有以下配置要求:

  • Cluster member configuration must specify a non-empty value for at least one of the attributes used for authentication. By default, MongoDB accepts:群集成员配置必须为用于身份验证的至少一个属性指定非null值。默认情况下,MongoDB接受:

    • the Organization组织 (O)
    • the Organizational Unit组织单位 (OU)
    • the Domain Component域组件 (DC)

    MongoDB verifies that entries match exactly across all member certificates. If you list multiple OU values, all certificates must use an identical list.MongoDB验证所有成员证书中的条目是否完全匹配。如果列出多个OU值,则所有证书必须使用相同的列表。

    You can specify alternative attributes to use for authentication by setting net.tls.clusterAuthX509.extensionValue.您可以通过设置net.tls.clusterAuthX509.extensionValue来指定用于身份验证的替代属性。

  • Cluster member configuration must include the same net.tls.clusterAuthX509.attributes and use matching values. Attribute order doesn't matter. The following example sets O and OU, but not DC:群集成员配置必须包含相同的net.tls.clusterAuthX509.attributes,并使用匹配的值。属性顺序并不重要。以下示例设置了OOU,但没有设置DC

    net:
    tls:
    clusterAuthX509:
    attributes: O=MongoDB, OU=MongoDB Server

Note

If you set the enforceUserClusterSeparation parameter to false, the following behaviors apply:如果将enforceUserClusterSeparation参数设置为false,则将应用以下行为:

  • You cannot set clusterAuthMode to an option that allows X.509 or the server will not start. The server will only start if clusterAuthMode is keyFile.您不能将clusterAuthMode设置为允许X.509的选项,否则服务器将无法启动。只有当clusterAuthModekeyFile时,服务器才会启动。
  • A client can create a user in the $external database whose O/OU/DC attributes match the server's configured attributes for cluster membership.客户端可以在$external数据库中创建一个用户,该用户的O/OU/DC属性与服务器为集群成员资格配置的属性相匹配。
  • A client presenting a member certificate can now attempt MONGODB-X509 authentication as a user in the $external database.出示成员证书的客户端现在可以作为$external数据库中的用户尝试MONGODB-X509身份验证。

To set the enforceUserClusterSeparation parameter to false, run the following command during startup:要将enforceUserClusterSeparation参数设置为false,请在启动过程中运行以下命令:

mongod --setParameter enforceUserClusterSeparation=false

The certificates have the following requirements:证书有以下要求:

  • A single Certificate Authority (CA) must issue all X.509 certificates for the members of a sharded cluster or a replica set.单个证书颁发机构(CA)必须为分片集群或副本集的成员颁发所有X.509证书。
  • At least one of the Subject Alternative Name (SAN) entries must match the server hostname used by other cluster members. When comparing SANs, MongoDB can compare either DNS names or IP addresses.至少有一个主题备选名称(SAN)条目必须与其他群集成员使用的服务器主机名匹配。在比较SAN时,MongoDB可以比较DNS名称或IP地址。

    If you don't specify subjectAltName, MongoDB compares the Common Name (CN) instead. However, this usage of CN is deprecated per RFC2818如果不指定subjectAltName,MongoDB会比较通用名(CN)。然而,根据RFC2818,CN的这种用法已被弃用

  • If the certificate used as the certificateKeyFile includes extendedKeyUsage, the value must include both clientAuth ("TLS Web Client Authentication") and serverAuth ("TLS Web Server Authentication").如果用作certificateKeyFile的证书包括extendedKeyUsage,则该值必须同时包括clientAuth(“TLS Web客户端身份验证”)和serverAuth(”TLS Web服务器身份验证“)。

    extendedKeyUsage = clientAuth, serverAuth
  • If the certificate used as the clusterFile includes extendedKeyUsage, the value must include clientAuth.如果用作clusterFile的证书包括extendedKeyUsage,则该值必须包括clientAuth

    extendedKeyUsage = clientAuth

mongod and mongos Certificate Key Filemongodmongos证书键文件

When establishing a TLS/SSL connection, the mongod / mongos presents a certificate key file to its clients to establish its identity. 在建立TLS/SSL连接时,mongod/mongos向其客户端提供证书键文件以建立其身份。[1] The certificate key file contains a public key certificate and its associated private key, but only the public component is revealed to the client.证书键文件包含公钥证书及其关联的私钥,但只向客户端显示公共组件。

MongoDB can use any valid TLS/SSL certificate issued by a certificate authority, or a self-signed certificate. MongoDB可以使用证书颁发机构颁发的任何有效TLS/SSL证书或自签名证书。If you use a self-signed certificate, although the communications channel will be encrypted to prevent eavesdropping on the connection, there will be no validation of server identity. 如果您使用自签名证书,尽管通信通道将被加密以防止连接被窃听,但不会对服务器身份进行验证。This leaves you vulnerable to a man-in-the-middle attack. Using a certificate signed by a trusted certificate authority will permit MongoDB drivers to verify the server's identity.这会使您容易受到中间人攻击。使用由受信任的证书颁发机构签名的证书将允许MongoDB驱动程序验证服务器的身份。

In general, avoid using self-signed certificates unless the network is trusted.一般来说,除非网络是可信的,否则避免使用自签名证书。

With regards to certificates for replica set and sharded cluster members, it is advisable to use different certificates on different servers. This minimizes exposure of the private key and allows for hostname validation.关于副本集和分片集群成员的证书,建议在不同的服务器上使用不同的证书。这最大限度地减少了私钥的暴露,并允许主机名验证。

Note

If a MongoDB deployment is not configured to use a CA file, it bypasses client certificate validation.如果MongoDB部署未配置为使用CA文件,则会绕过客户端证书验证。

[1] For FIPS mode, ensure that the certificate is FIPS-compliant (i.e uses a FIPS-compliant algorithm) and the private key meets the PKCS#8 standard. 对于FIPS模式,请确保证书符合FIPS标准(即使用符合FIPS标准的算法),私钥符合PKCS#8标准。If you need to convert a private key to PKCS#8 format, various conversion tools exist, such as openssl pkcs8 and others.如果您需要将私钥转换为PKCS#8格式,可以使用各种转换工具,如openssl pkcs8等。

Procedures (Using net.tls Settings)程序(使用net.tls设置)

Note

MongoDB provides net.tls settings (and --tls command-line options) that correspond to the net.ssl settings (and --ssl command-line options). MongoDB提供了与net.tls设置(和--ssl命令行选项)相对应的net.ssl设置(和--tls命令行参数)。The new tls settings provide identical functionality as the ssl settings since MongoDB has always supported TLS 1.0 and later.新的tls设置提供了与ssl设置相同的功能,因为MongoDB一直支持tls 1.0及更高版本。

The procedures in this section use the net.tls settings. For procedures using the net.ssl alias, see Procedures (Using net.ssl Settings).本节中的程序使用net.tls设置。有关使用net.ssl别名的过程,请参阅过程(使用net.ssl设置)

Set Up mongod and mongos with TLS/SSL Certificate and Key使用TLS/SSL证书和键设置mongodmongos

The following section configures mongod / mongos to use TLS/SSL connections. 以下部分将配置mongod/mongos以使用TLS/SSL连接。With these TLS/SSL settings, mongod / mongos presents its certificate key file to the client. 通过这些TLS/SSL设置,mongod/mongos向客户端提供其证书键文件。However, the mongod / mongos does not require a certificate key file from the client to verify the client's identity. 但是,mongod/mongos不需要客户端的证书键文件来验证客户端的身份。To require client's certificate key file, see Set Up mongod and mongos with Client Certificate Validation instead.要要求客户端的证书键文件,请参阅使用客户端证书验证设置mongodmongos

Note

The procedure uses the net.tls settings. For procedures that use the net.ssl settings, see Procedures (Using net.ssl Settings).该程序使用net.tls设置。有关使用net.ssl设置的过程,请参阅过程(使用net.ssl设定)

To use TLS/SSL connections, include the following TLS/SSL settings in your mongod / mongos instance's configuration file:要使用TLS/SSL连接,请在mongod/mongos实例的配置文件中包含以下TLS/SSL设置

PEMKeyFile(Linux/Windows/macOS)
Setting设置Notes备注
net.tls.mode

Set to requireTLS.设置为requireTLS

This setting restricts each server to use only TLS/SSL encrypted connections. 此设置限制每个服务器仅使用TLS/SSL加密连接。You can also specify either the value allowTLS or preferTLS to set up the use of mixed TLS/SSL modes on a port. 您还可以指定值allowTLSpreferTLS,以在端口上设置混合TLS/SSL模式的使用。See net.tls.mode for details.详见net.tls.mode

If you set --tlsMode to any value other than disabled, MongoDB uses the certificate specified in net.tls.certificateKeyFile for both server and client authentication in internal replica set connections. 如果将--tlsMode设置为disabled以外的任何值,MongoDB将在内部副本集连接中使用net.tls.certificateKeyFile中指定的证书进行服务器和客户端身份验证。This certificate setting applies regardless of whether you set security.clusterAuthMode to X.509.无论您是否将security.clusterAuthMode设置为X.509,此证书设置都适用。

net.tls.certificateKeyFile

Set to the path of the file that contains the TLS/SSL certificate and key.设置为包含TLS/SSL证书和键的文件的路径。

The mongod / mongos instance presents this file to its clients to establish the instance's identity.mongod/mongos实例将此文件呈现给其客户端,以建立实例的身份。

For example, consider the following configuration file for a mongod instance:例如,考虑以下mongod实例的配置文件

net:
tls:
mode: requireTLS
certificateKeyFile: /etc/ssl/mongodb.pem
systemLog:
destination: file
path: "/var/log/mongodb/mongod.log"
logAppend: true
storage:
dbPath: "/var/lib/mongodb"
processManagement:
fork: true
net:
bindIp: localhost,mongodb0.example.net
port: 27017
System SSL Certificate Store(Windows/macOS)

You can use system SSL certificate stores for Windows and macOS. To use the system SSL certificate store, specify net.tls.certificateSelector instead of specifying the certificate key file.您可以使用Windows和macOS的系统SSL证书存储。要使用系统SSL证书存储,请指定net.tls.certificateSelector,而不是指定证书键文件。

Setting设置Notes备注
net.tls.mode

Set to requireTLS.设置为requireTLS

This setting restricts each server to use only TLS/SSL encrypted connections. 此设置限制每个服务器仅使用TLS/SSL加密连接。You can also specify either the value allowTLS or preferTLS to set up the use of mixed TLS/SSL modes on a port. See net.tls.mode for details.您还可以指定值allowTLSpreferTLS,以在端口上设置混合TLS/SSL模式的使用。有关详细信息,请参阅net.tls.mode

If you set --tlsMode to any value other than disabled, MongoDB uses the certificate specified in net.tls.certificateKeyFile for both server and client authentication in internal replica set connections. 如果将--tlsMode设置为disabled以外的任何值,MongoDB将在内部副本集连接中使用net.tls.certificateKeyFile中指定的证书进行服务器和客户端身份验证。This certificate setting applies regardless of whether you set security.clusterAuthMode to X.509.无论您是否将security.clusterAuthMode设置为X.509,此证书设置都适用。

net.tls.certificateSelector

Set to the property (either subject or thumbprint) and value.设置为属性(subjectthumbprint)和值。

This setting is used to select the certificate. 此设置用于选择证书。See net.tls.certificateSelector for details.有关详细信息,请参阅net.tls.certificateSelector

For example, consider the following configuration file for a mongod instance:例如,考虑以下mongod实例的配置文件

net:
tls:
mode: requireTLS
certificateSelector: subject="<CertificateCommonName>"
systemLog:
destination: file
path: "/var/log/mongodb/mongod.log"
logAppend: true
storage:
dbPath: "/var/lib/mongodb"
processManagement:
fork: true
net:
bindIp: localhost,mongodb0.example.net
port: 27017

A mongod instance that uses the above configuration can only accept TLS/SSL connections:使用上述配置的mongod实例只能接受TLS/SSL连接:

mongod --config <path/to/configuration/file>

See Connect to MongoDB Instances Using Encryption for more information on connecting with TLS/SSL.有关使用TLS/SSL连接的更多信息,请参阅使用加密连接到MongoDB实例

Tip

You can also configure mongod and mongos using command-line options instead of the configuration file:您还可以使用命令行选项而不是配置文件来配置mongodmongos

Set Up mongod and mongos with Client Certificate Validation使用客户端证书验证设置mongodmongos

The following section configures mongod / mongos to use TLS/SSL connections and perform client certificate validation. With these TLS/SSL settings:以下部分将配置mongod/mongos以使用TLS/SSL连接并执行客户端证书验证。使用这些TLS/SSL设置:

  • mongod / mongos presents its certificate key file to the client for verification.将其证书键文件呈现给客户端进行验证。
  • mongod / mongos requires a certificate key file from the client to verify the client's identity.需要来自客户端的证书键文件来验证客户端的身份。

Note

The procedure uses the net.tls settings For procedures that use the net.ssl settings, see Procedures (Using net.ssl Settings).该过程使用net.tls设置。有关使用net.ssl设置的过程,请参阅过程(使用net.ssl设定)

To use TLS/SSL connections and perform client certificate validation, include the following TLS/SSL settings in your mongod / mongos instance's configuration file:要使用TLS/SSL连接并执行客户端证书验证,请在mongod/mongos实例的配置文件中包含以下TLS/SSL设置

Note

You can use system SSL certificate stores for Windows and macOS. To use the system SSL certificate store, specify net.ssl.certificateSelector instead of specifying the certificate key file.您可以使用Windows和macOS的系统SSL证书存储。要使用系统SSL证书存储,请指定net.ssl.certificateSelector,而不是指定证书键文件。

Setting设置Notes备注
net.tls.mode

Set to requireTLS.设置为requireTLS

This setting restricts each server to use only TLS/SSL encrypted connections. 此设置限制每个服务器仅使用TLS/SSL加密连接。You can also specify either the value allowTLS or preferTLS to set up the use of mixed TLS/SSL modes on a port. 您还可以指定值allowTLSpreferTLS,以在端口上设置混合TLS/SSL模式的使用。See net.tls.mode for details.详见net.tls.mode

If you set --tlsMode to any value other than disabled, MongoDB uses the certificate specified in net.tls.certificateKeyFile for both server and client authentication in internal replica set connections. 如果将--tlsMode设置为disabled以外的任何值,MongoDB将在内部副本集连接中使用net.tls.certificateKeyFile中指定的证书进行服务器和客户端身份验证。This certificate setting applies regardless of whether you set security.clusterAuthMode to X.509.无论您是否将security.clusterAuthMode设置为X.509,此证书设置都适用。

net.tls.certificateKeyFile

Set to the path of the file that contains the TLS/SSL certificate and key.设置为包含TLS/SSL证书和键的文件的路径。

The mongod / mongos instance presents this file to its clients to establish the instance's identity.mongod/mongos实例将此文件呈现给其客户端,以建立实例的身份。

net.tls.CAFile

Set to the path of the file that contains the certificate chain for verifying client certificates.设置为包含用于验证客户端证书的证书链的文件的路径。

The mongod / mongos instance use this file to verify certificates presented by its clients. mongod/mongos实例使用此文件来验证其客户端提供的证书。The certificate chain includes the certificate of the root Certificate Authority.证书链包括根证书颁发机构的证书。

Important

When starting a mongod instance with TLS/SSL enabled, you must specify a value for the --tlsCAFile flag, the net.tls.CAFile configuration option, or the tlsUseSystemCA parameter.启动启用了TLS/SSLmongod实例时,必须为--tlsCAFile标志、net.tls.CAFile配置选项或tlsUseSystemCA参数指定一个值。

--tlsCAFile, tls.CAFile, and tlsUseSystemCA are all mutually exclusive.--tlsCAFiletls.CAFiletlsUseSystemCA都是互斥的。

For example, consider the following configuration file for a mongod instance:例如,考虑以下mongod实例的配置文件

net:
tls:
mode: requireTLS
certificateKeyFile: /etc/ssl/mongodb.pem
CAFile: /etc/ssl/caToValidateClientCertificates.pem

systemLog:
destination: file
path: "/var/log/mongodb/mongod.log"
logAppend: true
storage:
dbPath: "/var/lib/mongodb"
processManagement:
fork: true
net:
bindIp: localhost,mongodb0.example.net
port: 27017

A mongod instance that uses the above configuration can only accept TLS/SSL connections and requires a valid certificate from its clients:使用上述配置的mongod实例只能接受TLS/SSL连接,并要求其客户端提供有效证书:

mongod --config <path/to/configuration/file>

Clients must specify TLS/SSL connections and present their certificate key file to the instance. 客户端必须指定TLS/SSL连接,并向实例提供其证书键文件。See Connect to MongoDB Instances that Require Client Certificates for more information on connecting with TLS/SSL.有关使用TLS/SSL连接的更多信息,请参阅连接到需要客户端证书的MongoDB实例

Tip

You can also configure mongod and mongos using command-line options instead of the configuration file:您还可以使用命令行选项而不是配置文件来配置mongodmongos

Block Revoked Certificates for Clients阻止客户端的吊销证书

Note

The procedure uses the net.tls settings. For procedures that use the net.ssl settings, see Procedures (Using net.ssl Settings).该程序使用net.tls设置。有关使用net.ssl设置的过程,请参阅过程(使用net.ssl设定)

To prevent clients with revoked certificates from connecting to the mongod or mongos instance, you can use a Certificate Revocation List (CRL).为了防止证书被吊销的客户端连接到mongodmongos实例,您可以使用证书吊销列表(CRL)。

To specify a CRL file, include net.tls.CRLFile set to a file that contains revoked certificates.要指定CRL文件,请将net.tls.CRLFile设置为包含已吊销证书的文件。

For example:例如:

net:
tls:
mode: requireTLS
certificateKeyFile: /etc/ssl/mongodb.pem
CAFile: /etc/ssl/caToValidateClientCertificates.pem
CRLFile: /etc/ssl/revokedCertificates.pem

Clients that present certificates that are listed in the /etc/ssl/revokedCertificates.pem file are not able to connect.提供/etc/ssl/revokedCertificates.pem文件中列出的证书的客户端无法连接。

Tip

You can also configure the revoked certificate list using the command-line option.您还可以使用命令行选项配置吊销的证书列表。

Validate Only if a Client Presents a Certificate仅在客户端出示证书时进行验证

In most cases, it is important to ensure that clients present valid certificates. However, if you have clients that cannot present a client certificate or are transitioning to using a certificate, you may only want to validate certificates from clients that present a certificate.在大多数情况下,确保客户端出示有效证书非常重要。但是,如果客户端无法提供客户端证书或正在转换为使用证书,您可能只想验证提供证书的客户端的证书。

Note

The procedure uses the net.tls settings. For procedures using the net.ssl settings, see Procedures (Using net.ssl Settings).该程序使用net.tls设置。有关使用net.ssl设置的过程,请参阅过程(使用net.ssl设定)

To bypass client certificate validation for clients that do not present a certificate, include net.tls.allowConnectionsWithoutCertificates set to true.要绕过未提供证书的客户端的客户端证书验证,请将net.tls.allowConnectionsWithoutCertificates证书设置为true

For example:例如:

net:
tls:
mode: requireTLS
certificateKeyFile: /etc/ssl/mongodb.pem
CAFile: /etc/ssl/caToValidateClientCertificates.pem
allowConnectionsWithoutCertificates: true

A mongod / mongos running with these settings allows connection from:使用这些设置运行的mongod/mongos允许从以下位置连接:

  • Clients that do not present a certificate.不提供证书的客户端。
  • Clients that present a valid certificate.提供有效证书的客户端。

Note

If the client presents a certificate, the certificate must be a valid certificate.如果客户端提供证书,则证书必须是有效的证书。

All connections, including those that have not presented certificates, are encrypted using TLS/SSL.所有连接,包括那些没有提供证书的连接,都使用TLS/SSL进行加密。

See TLS/SSL Configuration for Clients for more information on TLS/SSL connections for clients.有关客户端TLS/SSL连接的更多信息,请参阅客户端的TLS/SSL配置

Tip

You can also configure using the command-line options:您还可以使用命令行选项进行配置:

Disallow Protocols不允许协议

Note

The procedure uses the net.tls settings. For procedures using the net.ssl settings, see Procedures (Using net.ssl Settings).该程序使用net.tls设置。有关使用net.ssl设置的过程,请参阅过程(使用net.ssl设定)

To prevent MongoDB servers from accepting incoming connections that use specific protocols, include net.tls.disabledProtocols set to the disallowed protocols.为了防止MongoDB服务器接受使用特定协议的传入连接,请将net.tls.disabledProtocols设置为不允许的协议。

For example, the following configuration prevents mongod / mongos from accepting incoming connections that use either TLS1_0 or TLS1_1例如,以下配置阻止mongod/mongos接受使用TLS1_0TLS1_1的传入连接

net:
tls:
mode: requireTLS
certificateKeyFile: /etc/ssl/mongodb.pem
CAFile: /etc/ssl/caToValidateClientCertificates.pem
disabledProtocols: TLS1_0,TLS1_1

Tip

You can also configure using the command-line options:您还可以使用命令行选项进行配置:

TLS/SSL Certificate Passphrase证书密码

If the certificate key files for mongod / mongos are encrypted, include net.tls.certificateKeyFilePassword set to the passphrase.如果mongod/mongos的证书键文件已加密,请将net.tls.certificateKeyFilePassword设置为密码。

Tip

To avoid specifying the passphrase in cleartext, you can use an expansion value in the configuration file.为了避免以明文形式指定密码,您可以在配置文件中使用扩展值

Tip

You can also configure using the command-line options:您还可以使用命令行选项进行配置:

Online Certificate Rotation在线证书轮换

Starting in MongoDB 5.0, you can rotate the following certificate key files on-demand:从MongoDB 5.0开始,您可以按需轮换以下证书键文件:

To rotate one or more of these certificates:要轮换其中一个或多个证书,请执行以下操作:

  1. Replace the certificate or certificates you wish to rotate on the filesystem, noting the following constraints:替换您希望在文件系统上轮换的一个或多个证书,注意以下限制:

    • Each new certificate must have the same filename and same filepath as the certificate it is replacing.每个新证书必须具有与其替换的证书相同的文件名和文件路径。
    • If rotating an encrypted TLS Certificate, its password must be the same as the password for the old certificate (as specified to the certificateKeyFilePassword configuration file setting). 如果轮换加密的TLS证书,其密码必须与旧证书的密码相同(如certificateKeyFilePassword配置文件设置中指定的)。Certificate rotation does not support the interactive password prompt.证书轮换不支持交互式密码提示。
  2. Connect mongosh to the mongod or mongos instance that you wish to perform certificate rotation on.mongosh连接到要对其执行证书轮换的mongodmongos实例。
  3. Run the rotateCertificates command or the db.rotateCertificates() shell method to rotate the certificates used by the mongod or mongos instance.运行rotateCertificates命令或db.rotateCertificates()shell方法来轮换mongodmongos实例使用的证书。

When certificate rotation takes place:证书轮换时:

  • Existing connections to the mongod or mongos instance are not terminated, and will continue to use the old certificates.mongodmongos实例的现有连接不会终止,并将继续使用旧证书。
  • Any new connections will use the new certificates.任何新连接都将使用新证书。

Incorrect, expired, revoked, or missing certificate files will cause the certificate rotation to fail, but will not invalidate the existing TLS configuration or terminate the running mongod or mongos process.不正确、过期、吊销或丢失的证书文件将导致证书轮换失败,但不会使现有的TLS配置无效或终止正在运行的mongodmongos进程。

Previous to MongoDB 5.0, certificate rotation required downtime, and was typically performed during maintenance windows.在MongoDB 5.0之前,证书轮换需要停机,通常在维护窗口期间执行。

See rotateCertificates or db.rotateCertificates() for additional considerations and full usage instructions.有关其他注意事项和完整使用说明,请参阅rotateCertificatesdb.rotateCertificates()

Run in FIPS Mode在FIPS模式下运行

Note

FIPS-compatible TLS/SSL is available only in MongoDB Enterprise. See Configure MongoDB for FIPS for more information.FIPS兼容的TLS/SSL仅在MongoDB Enterprise中可用。有关更多信息,请参阅配置MongoDB for FIPS

See Configure MongoDB for FIPS for more details.有关更多详细信息,请参阅配置MongoDB for FIPS

Next Steps后续步骤

To configure TLS/SSL support for clients, see TLS/SSL Configuration for Clients.要为客户端配置TLS/SSL支持,请参阅客户端的TLS/SSL配置

Procedures (Using net.ssl Settings)程序(使用net.ssl设置)

Note

MongoDB provides net.tls settings (and --tls command-line options) that correspond to the net.ssl settings (and --ssl command-line options). MongoDB提供了与net.tls设置(和--tls命令行选项)相对应的net.ssl设置(和--ssl命令行参数)。The new tls settings provide identical functionality as the ssl settings since MongoDB has always supported TLS 1.0 and later.新的tls设置提供了与ssl设置相同的功能,因为MongoDB一直支持tls 1.0及更高版本。

The procedures in this section use the net.ssl settings. For procedures using the net.tls aliases, see Procedures (Using net.tls Settings).本节中的过程使用net.ssl设置。有关使用net.tls别名的过程,请参阅过程(使用net.tls设置)

Set Up mongod and mongos with TLS/SSL Certificate and Key使用TLS/SSL证书和键设置mongodmongos

The following section configures mongod / mongos to use TLS/SSL connections. 以下部分将配置mongod/mongos以使用TLS/SSL连接。With these TLS/SSL settings, mongod / mongos presents its certificate key file to the client. 通过这些TLS/SSL设置,mongod/mongos向客户端提供其证书键文件。However, the mongod / mongos does not require a certificate key file from the client to verify the client's identity. 但是,mongod/mongos不需要客户端的证书键文件来验证客户端的身份。To require client's certificate key file, see Set Up mongod and mongos with Client Certificate Validation instead.要要求客户端的证书键文件,请参阅使用客户端证书验证设置mongodmongos

To use TLS/SSL connections, include the following TLS/SSL settings in your mongod / mongos instance's configuration file:要使用TLS/SSL连接,请在mongod/mongos实例的配置文件中包含以下TLS/SSL设置:

System SSL Certificate Store(Windows/macOS)
Setting设置Notes备注
net.ssl.mode

Set to requireSSL.设置为requireSSL

This setting restricts each server to use only TLS/SSL encrypted connections. 此设置限制每个服务器仅使用TLS/SSL加密连接。You can also specify allowSSL or preferSSL to use mixed TLS/SSL modes. See net.ssl.mode for details.您还可以指定allowSSLpreferSSL使用混合TLS/SSL模式。有关详细信息,请参阅net.ssl.mode

net.ssl.PEMKeyFile

Set to the .pem file that contains the TLS/SSL certificate and key.设置为包含TLS/SSL证书和键的.pem文件。

The mongod / mongos instance presents this file to its clients to establish the instance's identity.mongod/mongos实例将此文件呈现给其客户端,以建立实例的身份。

If the key is encrypted, specify the passphrase (net.ssl.PEMKeyPassword).如果键已加密,请指定密码短语(net.ssl.PEMKeyPassword)。

For example, consider the following configuration file for a mongod instance:例如,考虑以下mongod实例的配置文件


net:
ssl:
mode: requireSSL
PEMKeyFile: /etc/ssl/mongodb.pem
systemLog:
destination: file
path: "/var/log/mongodb/mongod.log"
logAppend: true
storage:
dbPath: "/var/lib/mongodb"
processManagement:
fork: true
net:
bindIp: localhost,mongodb0.example.net
port: 27017
System SSL Certificate Store(Windows/macOS)

You can use system SSL certificate stores for Windows and macOS. To use the system SSL certificate store, specify net.ssl.certificateSelector instead of specifying the certificate key file.您可以使用Windows和macOS的系统SSL证书存储。要使用系统SSL证书存储,请指定net.ssl.certificateSelector,而不是指定证书键文件。

Setting设置Notes备注
net.ssl.mode

Set to requireSSL.设置为requireSSL

This setting restricts each server to use only TLS/SSL encrypted connections. 此设置限制每个服务器仅使用TLS/SSL加密连接。You can also specify allowSSL or preferSSL to use mixed TLS/SSL modes. See net.ssl.mode for details.您还可以指定allowSSLpreferSSL使用混合TLS/SSL模式。有关详细信息,请参阅net.ssl.mode

net.ssl.certificateSelector

Set to the property (either subject or thumbprint) and value.

This setting is used to select the certificate. See net.ssl.certificateSelector for details.此设置用于选择证书。有关详细信息,请参阅net.ssl.certificateSelector

For example, consider the following configuration file for a mongod instance:例如,考虑以下mongod实例的配置文件


net:
ssl:
mode: requireSSL
certificateSelector: subject="<CertificateCommonName>"
systemLog:
destination: file
path: "/var/log/mongodb/mongod.log"
logAppend: true
storage:
dbPath: "/var/lib/mongodb"
processManagement:
fork: true
net:
bindIp: localhost,mongodb0.example.net
port: 27017

A mongod instance that uses the above configuration can only accept TLS/SSL connections:使用上述配置的mongod实例只能接受TLS/SSL连接:

mongod --config <path/to/configuration/file>

See Connect to MongoDB Instances Using Encryption for more information on connecting with TLS/SSL.有关使用TLS/SSL连接的更多信息,请参阅使用加密连接到MongoDB实例

Tip

You can also configure mongod and mongos using command-line options instead of the configuration file:您还可以使用命令行选项而不是配置文件来配置mongodmongos

  • For mongod, see --sslMode, --sslPEMKeyFile, and --sslCertificateSelector.对于mongod,请参阅--sslMode--sslPEMKeyFile--sslCertificateSelector
  • For mongos, see: --sslMode, --sslPEMKeyFile and --sslCertificateSelector.对于mongos,请参阅:--sslMode--sslPEMKeyFile--sslCertificateSelector

Set Up mongod and mongos with Client Certificate Validation使用客户端证书验证设置mongodmongos

The following section configures mongod / mongos to use TLS/SSL connections and perform client certificate validation. With these TLS/SSL settings:以下部分将配置mongod/mongos以使用TLS/SSL连接并执行客户端证书验证。使用这些TLS/SSL设置:

  • mongod / mongos presents its certificate key file to the client for verification.将其证书键文件呈现给客户端进行验证。
  • mongod / mongos requires a certificate key file from the client to verify the client's identity.需要来自客户端的证书键文件来验证客户端的身份。

To use TLS/SSL connections, include the following TLS/SSL settings in your mongod / mongos instance's configuration file:要使用TLS/SSL连接,请在mongod/mongos实例的配置文件中包含以下TLS/SSL设置:

Note

You can use system SSL certificate stores for Windows and macOS. To use the system SSL certificate store, specify net.ssl.certificateSelector instead of specifying the certificate key file.您可以使用Windows和macOS的系统SSL证书存储。要使用系统SSL证书存储,请指定net.ssl.certificateSelector,而不是指定证书键文件。

Setting设置Notes备注
net.ssl.mode

Set to requireSSL.

This setting restricts each server to use only TLS/SSL encrypted connections. You can also specify allowSSL or preferSSL to use mixed TLS/SSL modes. See net.ssl.mode for details.此设置限制每个服务器仅使用TLS/SSL加密连接。您还可以指定allowSSLpreferSSL使用混合TLS/SSL模式。有关详细信息,请参阅net.ssl.mode

net.ssl.PEMKeyFile

Set to the .pem file that contains the TLS/SSL certificate and key.设置为包含TLS/SSL证书和键的.pem文件。

The mongod / mongos instance presents this file to its clients to establish the instance's identity.mongod/mongos实例将此文件呈现给其客户端,以建立实例的身份。

If the key is encrypted, specify the passphrase (net.ssl.PEMKeyPassword).如果键已加密,请指定密码短语(net.ssl.PEMKeyPassword)。

net.ssl.CAFile

Set to the path of the file that contains the certificate chain for verifying client certificates.设置为包含用于验证客户端证书的证书链的文件的路径。

The mongod / mongos instance use this file to verify certificates presented by its clients. The certificate chain includes the certificate of the root Certificate Authority.mongod/mongos实例使用此文件来验证其客户端提供的证书。证书链包括根证书颁发机构的证书。

For example, consider the following configuration file for a mongod instance:例如,考虑以下mongod实例的配置文件

net:
ssl:
mode: requireSSL
PEMKeyFile: /etc/ssl/mongodb.pem
CAFile: /etc/ssl/caToValidateClientCertificates.pem

systemLog:
destination: file
path: "/var/log/mongodb/mongod.log"
logAppend: true
storage:
dbPath: "/var/lib/mongodb"
processManagement:
fork: true
net:
bindIp: localhost,mongodb0.example.net
port: 27017

A mongod instance that uses the above configuration can only accept TLS/SSL connections and requires a valid certificate from its clients:使用上述配置的mongod实例只能接受TLS/SSL连接,并要求其客户端提供有效证书:

mongod --config <path/to/configuration/file>

Clients must specify TLS/SSL connections and present their certificate key file to the instance. 客户端必须指定TLS/SSL连接,并向实例提供其证书键文件。See Connect to MongoDB Instances that Require Client Certificates for more information on connecting with TLS/SSL.有关使用TLS/SSL连接的更多信息,请参阅连接到需要客户端证书的MongoDB实例

Tip

You can also configure mongod and mongos using command-line options instead of the configuration file:您还可以使用命令行选项而不是配置文件来配置mongodmongos

  • For mongod, see --sslMode, --sslPEMKeyFile, and --sslCAFile.对于mongod,请参阅--sslMode--sslPEMKeyFile--sslCAFile
  • For mongos, see --sslMode, --sslPEMKeyFile, and --sslCAFile.

Block Revoked Certificates for Clients阻止客户端的吊销证书

To prevent clients with revoked certificates from connecting to the mongod or mongos instance, you can use a Certificate Revocation List (CRL).为了防止证书被吊销的客户端连接到mongodmongos实例,您可以使用证书吊销列表(CRL)。

To specify a CRL file, include net.ssl.CRLFile set to a file that contains revoked certificates.要指定CRL文件,请将net.ssl.CRLFile设置为包含已吊销证书的文件。

For example:例如:

net:
ssl:
mode: requireSSL
PEMKeyFile: /etc/ssl/mongodb.pem
CAFile: /etc/ssl/caToValidateClientCertificates.pem
CRLFile: /etc/ssl/revokedCertificates.pem

Clients that present certificates that are listed in the /etc/ssl/revokedCertificates.pem file are not able to connect.提供/etc/ssl/revokedCertificates.pem文件中列出的证书的客户端无法连接。

Tip

You can also configure the revoked certificate list using the command-line option.您还可以使用命令行选项配置吊销的证书列表。

  • For mongod, see --sslCRLFile.关于mongod,请参阅--sslCRLFile
  • For mongos, see --sslCRLFile.关于mongos,请参阅--sslCRLFile

Validate Only if a Client Presents a Certificate仅在客户端出示证书时进行验证

In most cases, it is important to ensure that clients present valid certificates. However, if you have clients that cannot present a client certificate or are transitioning to using a certificate, you may only want to validate certificates from clients that present a certificate. 在大多数情况下,确保客户端出示有效证书非常重要。但是,如果客户端无法提供客户端证书或正在转换为使用证书,您可能只想验证提供证书的客户端的证书。If the client presents a certificate, the certificate must be a valid certificate. All connections, including those that have not presented certificates, are encrypted using TLS/SSL.如果客户端提供证书,则证书必须是有效的证书。所有连接,包括那些没有提供证书的连接,都使用TLS/SSL进行加密。

To bypass client certificate validation for clients that do not present a certificate, include net.ssl.allowConnectionsWithoutCertificates set to true.若要绕过未提供证书的客户端的客户端证书验证,请将net.ssl.allowConnectionsWithoutCertificates设置为true

For example:例如:

net:
ssl:
mode: requireSSL
PEMKeyFile: /etc/ssl/mongodb.pem
CAFile: /etc/ssl/caToValidateClientCertificates.pem
allowConnectionsWithoutCertificates: true

A mongod / mongos running with these settings allows connection from:使用这些设置运行的mongod/mongos允许从以下位置连接:

  • Clients that do not present a certificate.不提供证书的客户端。
  • Clients that present a valid certificate.提供有效证书的客户端。

Note

To use mongot with TLS, net.tls.allowConnectionsWithoutCertificates must be set to true. 要将mongot与TLS一起使用,必须将net.tls.allowConnectionsWithoutCertificates证书设置为trueFor details, see syncSource.replicaSet.tls.有关详细信息,请参阅syncSource.replicaSet.tls

See TLS/SSL Configuration for Clients for more information on TLS/SSL connections for clients.有关客户端TLS/SSL连接的更多信息,请参阅客户端的TLS/SSL配置

Tip

You can also configure using the command-line options:您还可以使用命令行选项进行配置:

  • For mongod, see --sslAllowConnectionsWithoutCertificates.对于mongod,请参阅--sslAllowConnectionsWithoutCertificates
  • For mongos, see --sslAllowConnectionsWithoutCertificates.对于mongos,请参阅--sslAllowConnectionsWithoutCertificates

Disallow Protocols不允许协议

To prevent MongoDB servers from accepting incoming connections that use specific protocols, include net.ssl.disabledProtocols set to the disallowed protocols.为了防止MongoDB服务器接受使用特定协议的传入连接,请将net.ssl.disabledProtocols设置为不允许的协议。

For example, the following configuration prevents mongod / mongos from accepting incoming connections that use either TLS1_0 or TLS1_1例如,以下配置阻止mongod/mongos接受使用TLS1_0TLS1_1的传入连接

net:
ssl:
mode: requireSSL
PEMKeyFile: /etc/ssl/mongodb.pem
CAFile: /etc/ssl/caToValidateClientCertificates.pem
disabledProtocols: TLS1_0,TLS1_1

Tip

You can also configure using the command-line options:您还可以使用命令行选项进行配置:

  • For mongod, see --sslDisabledProtocols.关于mongod,请参阅--sslDisabledPotocols
  • For mongos, see --sslDisabledProtocols.对于mongos,请参阅--sslDisabledPotocols

TLS/SSL Certificate PassphraseTLS/SSL证书密码

If the certificate key files for mongod / mongos are encrypted, include net.ssl.PEMKeyPassword set to the passphrase.如果mongod/mongos的证书键文件已加密,请将net.ssl.PEMKeyPassword设置为密码。

Tip

You can also configure using the command-line options:您还可以使用命令行选项进行配置:

  • For mongod, see sslPEMKeyPassword.对于mongod,请参阅sslPEMKeyPassword
  • For mongos, see --sslPEMKeyPassword.关于mongos,请参阅--sslPEMKeyPassword

Run in FIPS Mode在FIPS模式下运行

Note

FIPS-compatible TLS/SSL is available only in MongoDB Enterprise. See Configure MongoDB for FIPS for more information.FIPS兼容的TLS/SSL仅在MongoDB Enterprise中可用。有关更多信息,请参阅配置MongoDB for FIPS

See Configure MongoDB for FIPS for more details.有关更多详细信息,请参阅配置MongoDB for FIPS

Next Steps后续步骤

To configure TLS/SSL support for clients, see TLS/SSL Configuration for Clients.要为客户端配置TLS/SSL支持,请参阅客户端的TLS/SSL配置