对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

从密钥文件身份验证升级到 X.509 身份验证

要将当前使用密钥文件身份验证的自管理集群升级到 X.509身份验证,请使用以下滚动升级过程。

注意

MongoDB在支持 TLS 1.2+ 的系统上禁用对 TLS 1.0 和 TLS 1.1加密的支持。

注意

MongoDB 提供与 net.ssl 设置(和 --ssl 命令行选项)相对应的 net.tls 设置(和 --tls 命令行选项)。由于 MongoDB 始终支持 TLS 1.0 及更高版本,因此新的 tls 设置可提供与 ssl 设置相同的功能。

本部分中的过程使用tls设置/选项。 有关使用ssl设置/选项的过程,请参阅升级过程(使用ssl选项)。

注意

该过程使用tls设置/选项。 有关使用ssl设置/选项的过程,请参阅当前使用 TLS/SSL 的集群(使用ssl选项)。

对于使用 TLS/SSL 和密钥文件身份验证的集群,要升级到 X.509集群身份验证,请使用以下滚动升级进程:

  1. 对于集群的每个成员,将以下设置添加到配置文件中并重新启动:

    • security.clusterAuthMode
      设置为 sendKeyFile。使用此设置值,每个节点将继续发送其密钥文件,以验证自身的成员身份。但是,每个节点都可以从其他节点接收密钥文件或 X.509 证书,以对这些节点进行身份验证。
    • net.tls.clusterFile
      设置为用于成员身份验证的节点证书密钥文件的相应路径。 mongod / mongos会向集群的其他成员提供此文件,以表明自己的成员身份。

    包括其他TLS/SSL 选项以及适合您的特定配置的任何其他选项。

    例如:

    net: tls: mode: requireTLS certificateKeyFile: /etc/ssl/mongodb.pem CAFile: /etc/ssl/caToValidateReceivedCertificates.pem clusterFile: "/etc/ssl/myReplMembershipCertificateKeyFile.pem"security: clusterAuthMode: sendKeyFile keyFile: /my/securely/located/membershipkeyreplication: replSetName: myReplicaSetnet: bindIp: localhost,mongodb0.example.net port: 27017systemLog: destination: file path: "/var/log/mongodb/mongod.log" logAppend: truestorage: dbPath: "/var/lib/mongodb"processManagement: fork: true

    在继续之前,更新集群的所有节点以包含security.clusterAuthMode和net.tls.clusterFile设置。

  2. Connect to each node and use the setParameter command to update the clusterAuthMode to sendX509. [1]

    db.adminCommand( { setParameter: 1, clusterAuthMode: "sendX509" } )

    使用 sendX509 时,每个节点都会发送其 net.tls.clusterFile 来验证自己的成员身份。但是,每个节点都可以从其他节点接收密钥文件或 X.509 证书,以对这些节点进行身份验证。

    在继续之前,将集群的所有节点升级到此设置。

  3. Optional but recommended. Finally, for each node of the cluster, connect to the node and use the setParameter command to update the clusterAuthMode to x509 to only use the X.509 certificate for authentication. [1]

    db.adminCommand( { setParameter: 1, clusterAuthMode: "x509" } )
  4. 升级所有节点后,使用适当的 X.509 设置编辑配置文件,以确保在后续重新启动时,集群使用 X.509身份验证。示例:

    net: tls: mode: requireTLS certificateKeyFile: /etc/ssl/mongodb.pem CAFile: /etc/ssl/caToValidateReceivedCertificates.pem clusterFile: "/etc/ssl/myReplMembershipCertificateKeyFile.pem"security.clusterAuthMode: x509replication: replSetName: myReplicaSetnet: bindIp: localhost,mongodb0.example.net port: 27017systemLog: destination: file path: "/var/log/mongodb/mongod.log" logAppend: truestorage: dbPath: "/var/lib/mongodb"processManagement: fork: true

提示

您也可以使用命令行选项而不是配置文件来配置 mongod和mongos:

注意

该过程使用tls选项。 有关使用ssl设置/选项的过程,请参阅当前未使用 TLS/SSL 的集群(使用ssl选项)。

对于使用密钥文件身份验证但未使用 TLS/SSL 的集群,要升级到 X.509 成员身份验证和 TLS/SSL 连接:

  1. 对于集群的每个成员,将以下设置添加到配置文件中并重新启动:

    • net.tls.mode
      设置为allowTLS 。 此值允许节点接受 TLS/SSL 和非 TLS/非 SSL 传入连接。 其传出连接不使用 TLS/SSL。
    • net.tls.certificateKeyFile
      设置为包含 TLS/SSL 证书和密钥的文件的路径。 mongod / mongos将此文件提供给其客户端以确定实例的身份。 使用 TLS/SSL 时必需。
    • net.tls.clusterFile
      设置为用于成员身份验证的节点证书密钥文件的相应路径。 mongod / mongos会向集群的其他成员提供此文件,以表明自己的成员身份。
    • net.tls.CAFile
      设置为包含用于验证收到证书的证书链的文件的路径。
    • security.clusterAuthMode
      设置为 sendKeyFile。这允许每个节点继续发送其密钥文件,以验证自己的成员身份。但是,每个节点都可以从其他节点接收密钥文件或 X.509 证书,以对这些节点进行身份验证。

    包括其他TLS/SSL 选项以及适合您的特定配置的任何其他选项。

    例如:

    net: tls: mode: allowTLS certificateKeyFile: /etc/ssl/mongodb.pem clusterFile: "/etc/ssl/myReplMembershipCertificateKeyFile.pem" CAFile: /etc/ssl/caToValidateReceivedCertificates.pemsecurity: clusterAuthMode: sendKeyFile keyFile: /my/securely/located/membershipkeyreplication: replSetName: myReplicaSetnet: bindIp: localhost,mongodb0.example.net port: 27017systemLog: destination: file path: "/var/log/mongodb/mongod.log" logAppend: truestorage: dbPath: "/var/lib/mongodb"processManagement: fork: true

    升级集群的所有节点以包含net.tls.mode 、 net.tls.certificateKeyFile 、 net.tls.clusterFile和security.clusterAuthMode 。

  2. Connect to each node and use the setParameter command [1] to:

    1. 将tlsMode更新为preferSSL 。 将tlsMode设置为preferTLS时,该节点接受 TLS/SSL 和非 TLS/非 SSL 传入连接,并且其传出连接使用 TLS/SSL。

    2. 将 clusterAuthMode 更新为 sendX509。将 clusterAuthMode 设立为 sendX509 后,每个节点都会发送其 net.tls.clusterFile 以验证自身的成员身份。但是,每个节点会继续接受来自其他节点的密钥文件或 X.509 证书,以对这些节点进行身份验证。

    db.adminCommand( { setParameter: 1, tlsMode: "preferTLS" } );db.adminCommand( { setParameter: 1, clusterAuthMode: "sendX509" } );

    在继续之前,将集群的所有节点升级到这些设置。

  3. 升级所有节点后,使用适当的 TLS/SSL 和 X.509 设置编辑配置文件,以确保在后续重新启动时,集群使用 X.509 身份验证。

    net: tls: mode: preferTLS certificateKeyFile: /etc/ssl/mongodb.pem clusterFile: "/etc/ssl/myReplMembershipCertificateKeyFile.pem" CAFile: /etc/ssl/caToValidateReceivedCertificates.pemsecurity: clusterAuthMode: sendX509replication: replSetName: myReplicaSetnet: bindIp: localhost,mongodb0.example.net port: 27017systemLog: destination: file path: "/var/log/mongodb/mongod.log" logAppend: truestorage: dbPath: "/var/lib/mongodb"processManagement: fork: true

    注意

    此时,

    • 集群中的节点使用 TLS/SSL 连接。 但是,节点可以接受来自客户端应用程序的非 TLS/SSL 连接。

    • 节点发送其 X.509 证书进行成员身份验证,但也可以接受来自其他节点的 X.509 证书或密钥文件以验证其他节点的成员资格。

    要从客户端应用程序实施TLS/SSL 连接以及仅接受 X.509 进行成员身份验证,请参阅下一步。

  4. 可选但推荐使用。更新所有节点以仅使用 TLS/SSL 连接和 X.509 证书进行成员身份验证。

    重要

    此 TLS/SSL 连接要求应用于所有连接;即客户端与集群节点之间的连接。换言之,客户端在连接到集群时必须指定 TLS/SSL 连接并提供其证书密钥文件。有关使用 TLS/SSL 连接的更多信息,请参阅连接到启用 TLS 的副本集。另请参阅 仅在客户端出示证书时验证。

    将net.tls.mode更新为requireTLS ,并将security.clusterAuthMode更新为x509 。

    例如:

    net: tls: mode: requireTLS certificateKeyFile: /etc/ssl/mongodb.pem clusterFile: "/etc/ssl/myReplMembershipCertificateKeyFile.pem" CAFile: /etc/ssl/caToValidateReceivedCertificates.pemsecurity: clusterAuthMode: x509replication: replSetName: myReplicaSetnet: bindIp: localhost,mongodb0.example.net port: 27017systemLog: destination: file path: "/var/log/mongodb/mongod.log" logAppend: truestorage: dbPath: "/var/lib/mongodb"processManagement: fork: true

提示

您也可以使用命令行选项而不是配置文件来配置 mongod和mongos:

注意

MongoDB 提供与 net.ssl 设置(和 --ssl 命令行选项)相对应的 net.tls 设置(和 --tls 命令行选项)。由于 MongoDB 始终支持 TLS 1.0 及更高版本,因此新的 tls 设置可提供与 ssl 设置相同的功能。

本部分中的过程使用ssl选项。 有关使用tls选项的过程,请参阅升级过程(使用tls选项)。

注意

该过程使用ssl选项。 有关使用tls选项的过程,请参阅当前使用 TLS/SSL 的集群(使用ssl选项)。

对于使用 TLS/SSL 和密钥文件身份验证的集群,要升级到 X.509集群身份验证,请使用以下滚动升级进程:

  1. 对于集群的每个节点,启动节点时,将选项--clusterAuthMode设置为sendKeyFile ,并将选项--sslClusterFile设置为节点证书的相应路径。 包括其他TLS/SSL 选项以及特定配置所需的任何其他选项。 例如:

    mongod --replSet <name> --sslMode requireSSL --clusterAuthMode sendKeyFile --sslClusterFile <path to membership certificate and key PEM file> --sslPEMKeyFile <path to TLS/SSL Certificate and key PEM file> --sslCAFile <path to root CA PEM file> --bind_ip localhost,<hostname(s)|ip address(es)>

    通过此设置,每个节点将继续使用其密钥文件来验证自身的成员身份。不过,每个节点现在都可以接受来自其他成员的密钥文件或 X.509 证书,以对这些成员进行身份验证。将集群的所有节点升级到此设置。

  2. Then, for each node of a cluster, connect to the node and use the setParameter command to update the clusterAuthMode to sendX509. [1] For example,

    db.adminCommand( { setParameter: 1, clusterAuthMode: "sendX509" } )

    通过此设置,每个节点都会使用其 X.509 证书(在上一步中通过 --sslClusterFile 选项指定)来验证自身的成员身份。但是,每个节点会继续接受来自其他节点的密钥文件或 X.509 证书,以对这些节点进行身份验证。将集群的所有节点升级到此设置。

  3. Optional but recommended. Finally, for each node of the cluster, connect to the node and use the setParameter command to update the clusterAuthMode to x509 to only use the X.509 certificate for authentication. [1] For example:

    db.adminCommand( { setParameter: 1, clusterAuthMode: "x509" } )
  4. 升级所有节点后,使用适当的 X.509 设置编辑配置文件,以确保在后续重新启动时,集群使用 X.509身份验证。

有关各种模式及其说明,请参阅--clusterAuthMode 。

注意

该过程使用ssl选项。 有关使用tls选项的过程,请参阅使用密钥文件而不是 TLS/SSL 更新(使用tls选项)集群。

对于使用密钥文件身份验证但未使用 TLS/SSL 的集群,要升级到 X.509身份验证,请使用以下滚动升级进程:

  1. 对于集群的每个节点,启动节点时,将选项--sslMode设置为allowSSL ,将选项--clusterAuthMode设置为sendKeyFile ,并将选项--sslClusterFile设置为节点证书的相应路径。 包括其他TLS/SSL 选项以及特定配置所需的任何其他选项。 例如:

    mongod --replSet <name> --sslMode allowSSL --clusterAuthMode sendKeyFile --sslClusterFile <path to membership certificate and key PEM file> --sslPEMKeyFile <path to TLS/SSL certificate and key PEM file> --sslCAFile <path to root CA PEM file> --bind_ip localhost,<hostname(s)|ip address(es)>

    --sslMode allowSSL设置允许节点接受 TLS/SSL 和非 TLS/非 SSL 传入连接。 节点的传出连接不使用 TLS/SSL。

    --clusterAuthMode sendKeyFile 设置允许每个节点继续使用其密钥文件来验证自身的成员身份。不过,每个节点现在都可以接受来自其他成员的密钥文件或 X.509 证书,以对这些成员进行身份验证。

    将集群的所有节点升级到这些设置。

  2. Then, for each node of a cluster, connect to the node and use the setParameter command to update the sslMode to preferSSL and the clusterAuthMode to sendX509. [1] For example:

    db.adminCommand( { setParameter: 1, sslMode: "preferSSL", clusterAuthMode: "sendX509" } )

    将sslMode设置为preferSSL时,该节点接受 TLS/SSL 和非 TLS/非 SSL 传入连接,并且其传出连接使用 TLS/SSL。

    将 clusterAuthMode 设立为 sendX509 时,每个节点都会使用其 X.509 证书(在上一步中使用 --sslClusterFile 选项指定)来验证自身的成员身份。但是,每个节点会继续接受来自其他节点的密钥文件或 X.509 证书,以对这些节点进行身份验证。

    将集群的所有节点升级到这些设置。

  3. Optional but recommended. Finally, for each node of the cluster, connect to the node and use the setParameter command to update the sslMode to requireSSL and the clusterAuthMode to x509. [1] For example:

    db.adminCommand( { setParameter: 1, sslMode: "requireSSL", clusterAuthMode: "x509" } )

    将sslMode设置为requireSSL时,节点仅使用 TLS/SSL 连接。

    将 clusterAuthMode 设立为 x509 时,节点仅使用 X.509 证书进行身份验证。

  4. 升级所有节点后,使用适当的 TLS/SSL 和 X.509 设置编辑配置文件,以确保在后续重新启动时,集群使用 X.509 身份验证。

有关各种模式及其说明,请参阅--clusterAuthMode 。

[1](1, 2, 3, 4, 5, 6, 7) As an alternative to using the setParameter command, you can also restart the nodes with the appropriate TLS/SSL and x509 options and values.