Database Manual / Security / Encryption / TLS/SSL

TLS/SSL Configuration for Clients客户端的TLS/SSL配置

Clients must have support for TLS/SSL to connect to a mongod or a mongos instance that require TLS/SSL connections.客户端必须支持TLS/SSL才能连接到需要TLS/SSL连接mongodmongos实例。

Note

  • The Linux 64-bit legacy x64 binaries of MongoDB do not include support for TLS/SSL.MongoDB的Linux 64位旧式x64二进制文件不包括对TLS/SSL的支持。
  • MongoDB disables support for TLS 1.0 encryption on systems where TLS 1.1+ is available.MongoDB在TLS 1.1+可用的系统上禁用对TLS 1.0加密的支持。

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的先验知识以及有效证书的访问权限。

MongoDB Shell

mongosh provides various TLS/SSL settings, including:提供各种TLS/SSL设置,包括:

TLS OptionNotes备注
--tlsEnables TLS/SSL connection.启用TLS/SSL连接。
--tlsCertificateKeyFile

Specifies the .pem file that contains mongosh's certificate and key to present to the mongod or mongos instance. 指定包含mongosh证书和键的.pem文件,以呈现给mongodmongos实例。This option is mutually exclusive with --tlsCertificateSelector此选项与--tlsCertificateSelector互斥

mongod / mongos logs a warning on connection if the presented X.509 certificate expires within 30 days of the mongod/mongos host system time.如果所提供的X.509证书在mongod/mongos主机系统时间后30天内过期,则记录连接警告。

--tlsCertificateKeyFilePasswordIf mongosh's certificate key file is encrypted.如果mongosh的证书键文件是加密的。
--tlsCAFileSpecifies the Certificate Authority (CA) .pem file for verification of the certificate presented by the mongod or the mongos instance.指定证书颁发机构(CA).pem文件,用于验证mongodmongos实例提供的证书。
--tlsCertificateSelector

If running on Windows or macOS, use a certificate from the system certificate store.如果在Windows或macOS上运行,请使用系统证书存储中的证书。

This option is mutually exclusive with --tlsCertificateKeyFile.此选项与--tlsCertificateKeyFile互斥。

mongod / mongos logs a warning on connection if the presented X.509 certificate expires within 30 days of the mongod/mongos host system time.如果所提供的X.509证书在mongod/mongos主机系统时间后30天内过期,则记录连接警告。

For a complete list of mongosh's tls options, see TLS options.有关mongosh tls选项的完整列表,请参阅tls选项

For TLS/SSL connections, mongosh validates the certificate presented by the mongod or mongos instance:对于TLS/SSL连接,mongosh会验证mongodmongos实例提供的证书:

  • mongosh verifies that the certificate is from the specified Certificate Authority (--tlsCAFile. 验证证书是否来自指定的证书颁发机构(--tlsCAFile)。If the certificate is not from the specified CA, mongosh will fail to connect.如果证书不是来自指定的CA,mongosh将无法连接。
  • mongosh verifies that the hostname (specified in --host option or the connection string) matches the SAN (or, if SAN is not present, the CN) in the certificate presented by the mongod or mongos. mongosh验证主机名(在--host选项或连接字符串中指定)是否与mongodmongos提供的证书中的SAN(或者,如果SAN不存在,则为CN)匹配。If SAN is present, mongosh does not match against the CN. 如果存在SAN,则mongosh与CN不匹配。If the hostname does not match the SAN (or CN), mongosh will fail to connect.如果主机名与SAN(或CN)不匹配,mongosh将无法连接。

    Starting in MongoDB 4.2, when performing comparison of SAN, MongoDB supports comparison of DNS names or IP addresses. In previous versions, MongoDB only supports comparisons of DNS names.从MongoDB 4.2开始,在执行SAN比较时,MongoDB支持DNS名称或IP地址的比较。在以前的版本中,MongoDB只支持DNS名称的比较。

    To connect mongosh to a mongod or mongos that requires TLS/SSL, specify the --host option or use a connection string to specify the hostname. 要将mongosh连接到需要TLS/SSL的mongodmongos,请指定--host选项或使用连接字符串指定主机名。All other TLS/SSL options must be specified using the command-line options.必须使用命令行选项指定所有其他TLS/SSL选项。

Connect to MongoDB Instances Using Encryption使用加密连接到MongoDB实例

To connect to a mongod or mongos instance that requires encrypted communication, start mongosh with:要连接到需要加密通信mongodmongos实例,请使用以下命令启动mongosh

For example, consider a mongod instance running on hostname.example.com with the following options:例如,考虑一个在hostname.example.com上运行的mongod实例,具有以下选项:

mongod --tlsMode requireTLS --tlsCertificateKeyFile <pem>

To connect to the instance, start mongosh with the following options:要连接到实例,请使用以下选项启动mongosh

mongosh --tls --host hostname.example.com --tlsCAFile /etc/ssl/caToValidateServerCertificates.pem

mongosh verifies the certificate presented by the mongod instance against the specified hostname and the CA file.根据指定的主机名和CA文件验证mongod实例提供的证书。

Connect to MongoDB Instances that Require Client Certificates连接到需要客户端证书的MongoDB实例

To connect to a mongod or mongos that requires CA-signed client certificates, start mongosh with:要连接到需要CA签名的客户端证书的mongodmongos,请使用以下命令启动mongosh

  • --tls
  • --host and the --tlsCAFile to validate the server certificate,用于验证服务器证书,
  • --tlsCertificateKeyFile option to specify the client certificate to present to the server.选项用于指定要呈现给服务器的客户端证书。

For example, consider a mongod instance running on hostname.example.com with the following options:例如,考虑一个在hostnameexample.com上运行的mongod实例,具有以下选项:

mongod --tlsMode requireTLS --tlsCertificateKeyFile /etc/ssl/mongodb.pem --tlsCAFile /etc/ssl/caToValidateClientCertificates.pem

To connect to the instance, start mongosh with the following options:要连接到实例,请使用以下选项启动mongosh

mongosh --tls --host hostname.example.com --tlsCertificateKeyFile /etc/ssl/client.pem --tlsCAFile /etc/ssl/caToValidateServerCertificates.pem

Windows and macOS

To specify a client certificate from the system certificate store, use the --tlsCertificateSelector option instead of --tlsCertificateKeyFile.要从系统证书存储中指定客户端证书,请使用--tlsCertificateSelector选项,而不是--tlsCertificateKeyFile

If the CA file is also in the system certificate store, you can omit the --tlsCAFile option.如果CA文件也在系统证书存储中,则可以省略--tlsCAFile选项。

For example, if a certificate with the CN (Common Name) of myclient.example.net and the accompanying CA file are both in the macOS system certificate store, you can connect like this:例如,如果带有myclient.example.net的CN(Common Name)的证书和附带的CA文件都在macOS系统证书存储中,则可以按如下方式连接:

mongosh --tls  --host hostname.example.com --tlsCertificateSelector subject="myclient.example.net"

There are available in mongosh, but you should use the tls alternatives instead.mongosh中有可用的,但您应该使用tls替代品。

Avoid Use of --tlsAllowInvalidCertificates Option避免使用--tlsAllowInvalidCertificates选项

Warning

Although available, avoid using the --tlsAllowInvalidCertificates option if possible. If the use of --tlsAllowInvalidCertificates is necessary, only use the option on systems where intrusion is not possible.虽然可用,但如果可能的话,请避免使用--tlsAllowInvalidCertificates选项。如果需要使用--tlsAllowInvalidCertificates,请仅在不可能发生入侵的系统上使用该选项。

If mongosh runs with the --tlsAllowInvalidCertificates option, mongosh will not attempt to validate the server certificates. 如果mongosh使用--tlsAllowInvalidCertificates选项运行,mongosh将不会尝试验证服务器证书。This creates a vulnerability to expired mongod and mongos certificates as well as to foreign processes posing as valid mongod or mongos instances. If you only need to disable the validation of the hostname in the TLS/SSL certificates, see --tlsAllowInvalidHostnames.这为过期的mongodmongos证书以及冒充有效mongodmongos实例的外部进程创建了一个漏洞。如果只需要禁用TLS/SSL证书中的主机名验证,请参阅--tlsAllowInvalidHostnames

MongoDB Atlas, MongoDB Cloud Manager and MongoDB Ops ManagerMongoDB Atlas、MongoDB云管理器和MongoDB运维管理器

MongoDB Atlas uses TLS/SSL to encrypt the connections to your databases.MongoDB Atlas使用TLS/SSL加密与数据库的连接。

The MongoDB Cloud Manager and Ops Manager Monitoring agents use encrypted communication to gather its statistics. Because the agents already encrypt communications to the MongoDB Cloud Manager/Ops Manager servers, this is just a matter of enabling TLS/SSL support in MongoDB Cloud Manager/Ops Manager on a per host basis.MongoDB Cloud Manager和Ops Manager监控代理使用加密通信来集合其统计数据。因为代理已经加密了与MongoDB Cloud Manager/Ops Manager服务器的通信,所以这只是在每个主机的基础上在MongoDB Cloud Manager/Eps Manager中启用TLS/SSL支持的问题。

For more information, see:有关更多信息,请参阅:

MongoDB Drivers驱动程序

The MongoDB Drivers support encrypted communication. For details, see:MongoDB驱动程序支持加密通信。有关详细信息,请参阅:

MongoDB Tools工具

Various MongoDB utility programs support encrypted communication. These tools include:各种MongoDB实用程序支持加密通信。这些工具包括:

To use encrypted communication with these tools, use the same tls options as mongosh. See MongoDB Shell.要使用这些工具进行加密通信,请使用与mongosh相同的tls选项。请参阅MongoDB Shell。