mongod and mongos for TLS/SSLmongod和mongosOn this page本页内容
This document helps you to configure a new MongoDB instance to support TLS/SSL. 本文档帮助您配置新的MongoDB实例以支持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.有关将当前不使用TLS/SSL的群集升级为使用TLS/SSL的说明,请参阅将群集升级为改用TLS/SSL。
MongoDB uses the native TLS/SSL OS libraries:MongoDB使用本机TLS/SSL OS库:
| Platform | TLS/SSL Library |
|---|---|
| Windows | Secure Channel (Schannel) |
| Linux/BSD | OpenSSL |
| macOS | Secure Transport |
A full description of TLS/SSL, PKI (Public Key Infrastructure) certificates, and Certificate Authority is beyond the scope of this document. TLS/SSL、PKI(公钥基础设施)证书和证书颁发机构的完整描述超出了本文档的范围。This page assumes prior knowledge of TLS/SSL as well as access to valid certificates.本页假定您事先了解TLS/SSL以及访问有效证书。
For production use, your MongoDB deployment should use valid certificates generated and signed by a certificate authority. 对于生产使用,MongoDB部署应使用由证书颁发机构生成和签名的有效证书。You or your organization can generate and maintain an independent certificate authority, or use certificates generated by third-party TLS vendors. 您或您的组织可以生成和维护独立的证书颁发机构,或者使用第三方TLS供应商生成的证书。Obtaining and managing certificates is beyond the scope of this documentation.获取和管理证书超出了本文档的范围。
mongod and mongos Certificate Key FileWhen establishing a TLS/SSL connection, the mongod/mongos presents a certificate key file to its clients to establish its identity. [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.这最大限度地减少了私钥的暴露,并允许主机名验证。
| [1] | openssl pkcs8 and others.openssl pkcs8等。 |
net.tls Settings)net.tls设置)Starting in version 4.2, MongoDB provides net.tls settings (and corresponding command-line options) that corresponds to the net.ssl settings (and their corresponding command-line options). The net.tls settings provide identical functionality as the net.ssl options since MongoDB has always supported TLS 1.0 and later.
The procedures in this section use the 本节中的过程使用net.tls settings. net.tls设置。For procedures using the net.ssl alias, see Procedures (Using net.ssl Settings).
mongod and mongos with TLS/SSL Certificate and KeyThe following section configures mongod/mongos to use TLS/SSL connections. With these TLS/SSL settings, mongod/mongos presents its certificate key file to the client. However, the mongod/mongos does not require a certificate key file from the client to verify the client's identity. To require client's certificate key file, see Set Up mongod and mongos with Client Certificate Validation instead.
The procedure uses the net.tls settings (available starting in MongoDB 4.2). For procedures using the net.ssl settings, see Procedures (Using net.ssl Settings).
To use TLS/SSL connections, include the following TLS/SSL settings in your mongod/mongos instance's configuration file:
| Setting | Notes |
|---|---|
net.tls.mode | Set to This setting restricts each server to use only TLS/SSL encrypted connections. You can also specify either the value |
net.tls.certificateKeyFile | Set to the path of the file that contains the TLS/SSL certificate and key.
|
For example, consider the following configuration file for a mongod instance:
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 Starting in MongoDB 4.0, you can use system SSL certificate stores for Windows and macOS. To use the system SSL certificate store, specify
net.tls.certificateSelectorinstead of specifying the certificate key file.
Setting Notes net.tls.modeSet to
requireTLS.This setting restricts each server to use only TLS/SSL encrypted connections. You can also specify either the value
allowTLSorpreferTLSto set up the use of mixed TLS/SSL modes on a port. Seenet.tls.modefor details.net.tls.certificateSelectorSet to the property (either
subjectorthumbprint) and value.
This setting is used to select the certificate.此设置用于选择证书。Seenet.tls.certificateSelectorfor details.For example, consider the following configuration file for a
mongodinstance: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
mongodinstance that uses the above configuration can only use TLS/SSL connections:mongod --config <path/to/configuration/file>That is, clients must specify TLS/SSL connections. See Connect to MongoDB Instance Using Encryption (
tlsOptions) for more information on connecting with TLS/SSL.Tip提示See also:参阅:You can also configure
mongodandmongosusing command-line options instead of the configuration file:
- For
mongod, see:--tlsMode;--tlsCertificateKeyFile; and--tlsCertificateSelector.- For
mongos, see:--tlsMode;--tlsCertificateKeyFile; and--tlsCertificateSelector.Set Up
mongodandmongoswith Client Certificate ValidationThe following section configures
mongod/mongosto use TLS/SSL connections and perform client certificate validation. With these TLS/SSL settings:
mongod/mongospresents its certificate key file to the client for verification.mongod/mongosrequires a certificate key file from the client to verify the client's identity.Note注意The procedure uses the
net.tlssettings (available starting in MongoDB 4.2). For procedures using thenet.sslsettings, see Procedures (Usingnet.sslSettings).To use TLS/SSL connections and perform client certificate validation, include the following TLS/SSL settings in your
mongod/mongosinstance's configuration file:Note注意Starting in MongoDB 4.0, you can use system SSL certificate stores for Windows and macOS. To use the system SSL certificate store, specify
net.ssl.certificateSelectorinstead of specifying the certificate key file.
Setting Notes net.tls.modeSet to
requireTLS.This setting restricts each server to use only TLS/SSL encrypted connections. You can also specify either the value
allowTLSorpreferTLSto set up the use of mixed TLS/SSL modes on a port. Seenet.tls.modefor details.net.tls.certificateKeyFileSet to the path of the file that contains the TLS/SSL certificate and key.
The
mongod/mongosinstance presents this file to its clients to establish the instance's identity.net.tls.CAFileSet to the path of the file that contains the certificate chain for verifying client certificates.
The
mongod/mongosinstance use this file to verify certificates presented by its clients. The certificate chain includes the certificate of the root Certificate Authority.For example, consider the following configuration file for a
mongodinstance: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: 27017A
mongodinstance that uses the above configuration can only use TLS/SSL connections and requires valid certificate from its clients:mongod --config <path/to/configuration/file>That is, clients must specify TLS/SSL connections and presents its certificate key file to the instance. See Connect to MongoDB Instance that Requires Client Certificates (
tlsOptions) for more information on connecting with TLS/SSL.Tip提示See also:参阅:You can also configure
mongodandmongosusing command-line options instead of the configuration file:
- For
mongod, see--tlsMode,--tlsCertificateKeyFile, and--tlsCAFile.- For
mongos, see--tlsMode,--tlsCertificateKeyFile,--tlsCAFile.Block Revoked Certificates for Clients
Note注意The procedure uses the
net.tlssettings (available starting in MongoDB 4.2). For procedures using thenet.sslsettings, see Procedures (Usingnet.sslSettings).To prevent clients with revoked certificates from connecting to the
mongodormongosinstance, you can use:
- Online Certificate Status Protocol (OCSP)
Starting in version 4.4, to check for certificate revocation, MongoDB
enablesthe use of OCSP (Online Certificate Status Protocol) by default as an alternative to specifying a CRL file or using thesystem SSL certificate store.In versions 4.0 and 4.2, the use of OCSP is available only through the use of
system certificate storeon Windows or macOS.
- Certificate Revocation List (CRL)
To specify a CRL file, include
net.tls.CRLFileset to a file that contains revoked certificates.
For Example:例如:net: tls: mode: requireTLS certificateKeyFile: /etc/ssl/mongodb.pem CAFile: /etc/ssl/caToValidateClientCertificates.pem CRLFile: /etc/ssl/revokedCertificates.pemClients who presents certificates that are listed in the
/etc/ssl/revokedCertificates.pemwill not be able to connect.Tip提示See also:参阅:You can also configure the revoked certificate list using the command-line option.
- For
mongod, see--tlsCRLFile.- For
mongos, see--tlsCRLFile.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.tlssettings (available starting in MongoDB 4.2). For procedures using thenet.sslsettings, see Procedures (Usingnet.sslSettings).To bypass client certificate validation for clients that do not present a certificate, include
net.tls.allowConnectionsWithoutCertificatesset totrue.
For Example:例如:net: tls: mode: requireTLS certificateKeyFile: /etc/ssl/mongodb.pem CAFile: /etc/ssl/caToValidateClientCertificates.pem allowConnectionsWithoutCertificates: trueA
mongod/mongosrunning with these settings allows connection from:
- 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.
See TLS/SSL Configuration for Clients for more information on TLS/SSL connections for clients.
Tip提示See also:参阅:You can also configure using the command-line options:
- For
mongod, see--tlsAllowConnectionsWithoutCertificates.- For
mongos, see--tlsAllowConnectionsWithoutCertificates.Disallow Protocols
Note注意The procedure uses the
net.tlssettings (available starting in MongoDB 4.2). For procedures using thenet.sslsettings, see Procedures (Usingnet.sslSettings).To prevent MongoDB servers from accepting incoming connections that use specific protocols, include
net.tls.disabledProtocolsset to the disallowed protocols.For example, the following configuration prevents
mongod/mongosfrom accepting incoming connections that use eitherTLS1_0orTLS1_1net: tls: mode: requireTLS certificateKeyFile: /etc/ssl/mongodb.pem CAFile: /etc/ssl/caToValidateClientCertificates.pem disabledProtocols: TLS1_0,TLS1_1Tip提示See also:参阅:You can also configure using the command-line options:
- For
mongod, see--tlsDisabledProtocols.- For
mongos, see--tlsDisabledProtocols.TLS/SSL Certificate Passphrase
If the certificate key files for
mongod/mongosare encrypted, includenet.tls.certificateKeyFilePasswordset to the passphrase.Tip提示Starting in MongoDB 4.2, to avoid specifying the passphrase in cleartext, you can use an expansion value in the configuration file.
Tip提示See also:参阅:You can also configure using the command-line options:
- For
mongod, see--tlsCertificateKeyFilePassword.- For
mongos, see--tlsCertificateKeyFilePassword.Online Certificate Rotation
Starting in MongoDB 5.0, you can rotate the following certificate key files on-demand:
TLS CertificatesCRL (Certificate Revocation List) files(on Linux and Windows platforms)CA (Certificate Authority) filesTo rotate one or more of these certificates:
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 thecertificateKeyFilePasswordconfiguration file setting). Certificate rotation does not support the interactive password prompt.- Connect
mongoshto themongodormongosinstance that you wish to perform certificate rotation on.- Run the
rotateCertificatescommand or thedb.rotateCertificates()shell method to rotate the certificates used by the themongodormongosinstance.When certificate rotation takes place:
- Existing connections to the
mongodormongosinstance are not terminated, and will continue to use the old certificates.- 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
mongodormongosprocess.Previous to MongoDB 5.0, certificate rotation required downtime, and was typically performed during maintenance windows.
See
rotateCertificatesordb.rotateCertificates()for additional considerations and full usage instructions.Run in FIPS Mode
Note注意FIPS-compatible TLS/SSL is available only in MongoDB Enterprise. See Configure MongoDB for FIPS for more information.
See Configure MongoDB for FIPS for more details.
Next Steps
To configure TLS/SSL support for clients, see TLS/SSL Configuration for Clients.
Tip提示See also:参阅:Procedures (Using
net.sslSettings)Note注意Starting in version 4.2, MongoDB provides
net.tlssettings (and corresponding command-line options) that corresponds to thenet.sslsettings (and their corresponding command-line options). Thenet.tlssettings provide identical functionality as thenet.ssloptions since MongoDB has always supported TLS 1.0 and later.The procedures in this section use the
net.sslsettings. For procedures using thenet.tlsaliases, see Procedures (Usingnet.tlsSettings).Set Up
mongodandmongoswith TLS/SSL Certificate and KeyThe following section configures
mongod/mongosto use TLS/SSL connections. With these TLS/SSL settings,mongod/mongospresents its certificate key file to the client. However, themongod/mongosdoes not require a certificate key file from the client to verify the client's identity. To require client's certificate key file, see Set Upmongodandmongoswith Client Certificate Validation instead.To use TLS/SSL connections, include the following TLS/SSL settings in your
mongod/mongosinstance's configuration file:
Setting Notes net.ssl.modeSet to
requireSSL.This setting restricts each server to use only TLS/SSL encrypted connections. You can also specify
allowSSLorpreferSSLto use mixed TLS/SSL modes. Seenet.ssl.modefor details.net.ssl.PEMKeyFileSet to the
.pemfile that contains the TLS/SSL certificate and key.The
mongod/mongosinstance presents this file to its clients to establish the instance's identity.If the key is encrypted, specify the passphrase (
net.ssl.PEMKeyPassword).For example, consider the following configuration file for a
mongodinstance: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 Starting in MongoDB 4.0, you can use system SSL certificate stores for Windows and macOS. To use the system SSL certificate store, specify
net.ssl.certificateSelectorinstead of specifying the certificate key file.
Setting Notes net.ssl.modeSet to
requireSSL.This setting restricts each server to use only TLS/SSL encrypted connections. You can also specify
allowSSLorpreferSSLto use mixed TLS/SSL modes. Seenet.ssl.modefor details.net.ssl.certificateSelectorSet to the property (either
subjectorthumbprint) and value.This setting is used to select the certificate. See
net.ssl.certificateSelectorfor details.For example, consider the following configuration file for a
mongodinstance: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
mongodinstance that uses the above configuration can only use TLS/SSL connections:mongod --config <path/to/configuration/file>That is, clients must specify TLS/SSL connections. See Connect to MongoDB Instance Using Encryption (
--sslOptions) for more information on connecting with TLS/SSL.Tip提示See also:参阅:You can also configure
mongodandmongosusing command-line options instead of the configuration file:
- For
mongod, see:--sslMode;--sslPEMKeyFile; and--sslCertificateSelector.- For
mongos, see:--sslMode;--sslPEMKeyFile; and--sslCertificateSelector.Set Up
mongodandmongoswith Client Certificate ValidationThe following section configures
mongod/mongosto use TLS/SSL connections and perform client certificate validation. With these TLS/SSL settings:
mongod/mongospresents its certificate key file to the client for verification.mongod/mongosrequires 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/mongosinstance's configuration file:Note注意Starting in MongoDB 4.0, you can use system SSL certificate stores for Windows and macOS. To use the system SSL certificate store, specify
net.ssl.certificateSelectorinstead of specifying the certificate key file.
Setting Notes net.ssl.modeSet to
requireSSL.This setting restricts each server to use only TLS/SSL encrypted connections. You can also specify
allowSSLorpreferSSLto use mixed TLS/SSL modes. Seenet.ssl.modefor details.net.ssl.PEMKeyFileSet to the
.pemfile that contains the TLS/SSL certificate and key.The
mongod/mongosinstance presents this file to its clients to establish the instance's identity.If the key is encrypted, specify the passphrase (
net.ssl.PEMKeyPassword).net.ssl.CAFileSet to the path of the file that contains the certificate chain for verifying client certificates.
The
mongod/mongosinstance use this file to verify certificates presented by its clients. The certificate chain includes the certificate of the root Certificate Authority.For example, consider the following configuration file for a
mongodinstance: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: 27017A
mongodinstance that uses the above configuration can only use TLS/SSL connections and requires valid certificate from its clients:mongod --config <path/to/configuration/file>That is, clients must specify TLS/SSL connections and presents its certificate key file to the instance. See Connect to MongoDB Instance that Requires Client Certificates (
sslOptions) for more information on connecting with TLS/SSL.Tip提示See also:参阅:You can also configure
mongodandmongosusing command-line options instead of the configuration file:
- For
mongod, see--sslMode,--sslPEMKeyFile, and--sslCAFile.- For
mongos, see--sslMode,--sslPEMKeyFile, and--sslCAFile.Block Revoked Certificates for Clients
To prevent clients with revoked certificates from connecting to the
mongodormongosinstance, you can use:
- Online Certificate Status Protocol (OCSP)
Starting in version 4.4, to check for certificate revocation, MongoDB
enablesthe use of OCSP (Online Certificate Status Protocol) by default as an alternative to specifying a CRL file or using thesystem SSL certificate store.In versions 4.0 and 4.2, the use of OCSP is available only through the use of
system certificate storeon Windows or macOS.
- Certificate Revocation List (CRL)
To specify a CRL file, include
net.ssl.CRLFileset to a file that contains revoked certificates.
For Example:例如:net: ssl: mode: requireSSL PEMKeyFile: /etc/ssl/mongodb.pem CAFile: /etc/ssl/caToValidateClientCertificates.pem CRLFile: /etc/ssl/revokedCertificates.pemClients who presents certificates that are listed in the
/etc/ssl/revokedCertificates.pemwill not be able to connect.Tip提示See also:参阅:You can also configure the revoked certificate list using the command-line option.
- For
mongod, see--sslCRLFile.- For
mongos, see--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.
To bypass client certificate validation for clients that do not present a certificate, include
net.ssl.allowConnectionsWithoutCertificatesset totrue.
For Example:例如:net: ssl: mode: requireSSL PEMKeyFile: /etc/ssl/mongodb.pem CAFile: /etc/ssl/caToValidateClientCertificates.pem allowConnectionsWithoutCertificates: trueA
mongod/mongosrunning with these settings allows connection from:
- 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.
See TLS/SSL Configuration for Clients for more information on TLS/SSL connections for clients.
Tip提示See also:参阅:You can also configure using the command-line options:
- For
mongod, see--sslAllowConnectionsWithoutCertificates.- For
mongos, see--sslAllowConnectionsWithoutCertificates.Disallow Protocols
To prevent MongoDB servers from accepting incoming connections that use specific protocols, include
net.ssl.disabledProtocolsset to the disallowed protocols.For example, the following configuration prevents
mongod/mongosfrom accepting incoming connections that use eitherTLS1_0orTLS1_1net: ssl: mode: requireSSL PEMKeyFile: /etc/ssl/mongodb.pem CAFile: /etc/ssl/caToValidateClientCertificates.pem disabledProtocols: TLS1_0,TLS1_1Tip提示See also:参阅:You can also configure using the command-line options:
- For
mongod, see--sslDisabledProtocols.- For
mongos, see--sslDisabledProtocols.TLS/SSL Certificate Passphrase
If the certificate key files for
mongod/mongosare encrypted, includenet.ssl.PEMKeyPasswordset to the passphrase.Tip提示See also:参阅:You can also configure using the command-line options:
- For
mongod, see--sslPEMKeyPassword.- For
mongos, see--sslPEMKeyPassword.Run in FIPS Mode
Note注意FIPS-compatible TLS/SSL is available only in MongoDB Enterprise. See Configure MongoDB for FIPS for more information.
See Configure MongoDB for FIPS for more details.
Next Steps
To configure TLS/SSL support for clients, see TLS/SSL Configuration for Clients.
Tip提示See also:参阅: