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

配置身份验证和授权 mongot

使用此页配置您的 mongot 和 MongoDB Community Edition 部署的身份验证和授权。有关将 mongot 与 结合使用的信息,请参阅 MongoDB Search 和带有 Kubernetes 的向量搜索。

mongot 通过两个独立的渠道进行通信,每个渠道都需要自己的安全配置:

  • 同步源 (syncSource.replicaSet、syncSource.router):mongot出站连接到您的 MongoDB 部署以进行复制。您在此处配置身份验证机制,以便 mongot 可以对 mongod 或 mongos 进行身份验证。

  • gRPC 服务器 (server.grpc):入站渠道,mongod 通过该渠道连接到 mongot 以执行搜索查询和索引管理命令。mongod 和 mongot 通过 mTLS 相互验证。

注意

健康检查 (healthCheck) 和指标 (metrics) 终结点不支持 TLS 或身份验证。在网络层限制对这些端口的访问。

在配置身份验证之前,请查看以下几点:

  • 必须为同步源配置一个身份验证机制:即 SCRAM 或 X.509。这两种机制在每个连接块中互相排斥。

  • 对于分片集群,必须在 mongot 配置文件中指定 syncSource.replicaSet 和 syncSource.router,并在每个连接上独立配置身份验证。syncSource.router 不是由 syncSource.replicaSet 隐含的,因此您必须显式配置它。每个分片都需要自己的专用 mongot 实例。

    • syncSource.replicaSet 将 mongot 实例映射到其分片的副本集。

    • syncSource.router 为集群范围协调提供 mongos 连接。

  • 在 mongot 能够进行身份验证之前,searchCoordinator 内置角色必须存在于 MongoDB 部署中。此角色在 MongoDB 8.2 及更高版本中可用。

将以下 setParameter 选项添加到 mongod 配置文件中,然后重启 mongod:

setParameter: mongotHost: <host>:<port> searchIndexManagementHostAndPort: <host>:<port> skipAuthenticationToSearchIndexManagementServer: false useGrpcForSearch: true

setParameter 选项设置服务器参数,告诉 mongod 服务器如何定位和与 mongot 通信。在 mongot 连接到部署之前,必须设置这些参数。

若要了解有关 mongod 配置文件的更多信息,请参阅 自管理配置文件选项。

在同步源连接上配置以下身份验证机制之一,以便 mongot 可以对 MongoDB 部署进行身份验证。

SCRAM 使用用户名和密码对 mongot 到您的 MongoDB 部署进行认证。

要为 mongot 配置 SCRAM,请完成以下步骤:

1

在您的 MongoDB 副本集上创建具有 searchCoordinator 角色的用户。

在mongosh中运行以下命令:

use admindb.createUser({ user: "mongotUser", pwd: "<password>", roles: [ { role: "searchCoordinator", db: "admin" } ]})
2

将 <password> 替换为您的密码,然后为您的操作系统运行命令以创建密码文件。该文件不得包含尾随换行符。

适用于 Linux 或 macOS:

echo -n "<password>" > /etc/mongot/secrets/passwordFilechmod 400 /etc/mongot/secrets/passwordFile

对于 Windows 命令提示符:

echo|set /p="<password>" > passwordFile

对于 Windows Powershell:

[System.IO.File]::WriteAllText("passwordFile", "<password>")
3

在 syncSource.replicaSet 下添加 scramAuth 块。对于分片集群,还要在 syncSource.router 下添加相同的块。在 hostAndPort 中列出多个托管,以便在一个托管不可用时 mongot 仍可以发现副本集:

syncSource: replicaSet: hostAndPort: - "<host1>:27017" - "<host2>:27017" scramAuth: username: mongotUser authSource: admin passwordFile: /etc/mongot/secrets/passwordFile tls: enabled: false

对于分片集群,请在 syncSource.replicaSet 和 syncSource.router 下配置 scramAuth。syncSource.replicaSet 对 mongot 进行身份验证以连接到分片的副本集,syncSource.router 对 mongot 进行身份验证以连接到 mongos 进行集群范围协调。您可以对这两个连接使用相同的凭证,也可以在独立管理时使用不同的凭证:

syncSource: replicaSet: hostAndPort: - "<shard-host1>:27017" - "<shard-host2>:27017" scramAuth: username: mongotShardUser authSource: admin passwordFile: /etc/mongot/secrets/shard-passwordFile tls: enabled: false router: hostAndPort: - "<mongos1>:27017" - "<mongos2>:27017" scramAuth: username: mongotRouterUser authSource: admin passwordFile: /etc/mongot/secrets/router-passwordFile tls: enabled: false

有关 mongot 配置文件的更多信息,请参阅配置 mongot。

4

如果您的 MongoDB 部署已启用 TLS,请将 tls.enabled 设置为 true,并根据需要向 mongot 配置文件提供以下字段:

字段
说明

caFile

包含一个或多个 X.509 的 PEM 文件验证 mongod 服务器证书皈的 CA 证书。如果您忽略此字段,mongot 将使用 Java 虚拟机(JVM)默认信任存储。

tlsCertificateKeyFile

包含客户端 X.509 证书及其私钥的 PEM 文件。仅当 mongod 验证客户端证书时才需要。

tlsCertificateKeyFilePasswordFile

解密客户端证书密钥的密码。仅当 tlsCertificateKeyFile 受密码保护时才需要。

gRPC 服务器是入站渠道,mongod 通过该渠道连接到 mongot 以进行搜索查询和索引管理命令。您可以使用 TLS 在传输层保护此渠道。要相互认证 mongod 和 mongot,请使用 mTLS。

您在 mongot 上使用 server.grpc.tls.mode 选项,并在 mongod 上使用 searchTLSMode 参数来配置 TLS 模式。当您未设置 searchTLSMode 时,mongod 会从 net.tls.mode 继承其连接到 mongot 的 TLS 模式,因为 searchTLSMode 默认采用 globalTLS。

您可以在 mongot 配置文件中配置以下其中一种 TLS 模式:

无 TLS。仅对 mongod 和 mongot 在同一托管上运行的部署使用此模式:

server: grpc: address: "localhost:27028" tls: mode: "disabled"

mongot 提供服务器证书,mongod 在建立连接前对其进行验证。此模式需要 certificateKeyFile:

server: grpc: address: "0.0.0.0:27028" tls: mode: "tls" certificateKeyFile: /etc/mongot/tls/mongot-server.pem # certificateKeyFilePasswordFile: /etc/mongot/secrets/server-key-password

mongot 和 mongod 都会呈现并验证对方的证书。此模式需要 certificateKeyFile 和 caFile。使用 mtls 时,还必须为 mongod 配置对应的 TLS 设置,以便它连接到 mongot:

server: grpc: address: "0.0.0.0:27028" tls: mode: "mtls" certificateKeyFile: /etc/mongot/tls/mongot-server.pem caFile: /etc/mongot/tls/ca.pem