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

配置身份验证和授权 mongot

使用此页面为 mongot 和 MongoDB Community Edition 部署配置身份验证和授权。有关将 mongot 与 Kubernetes 操作符的 MongoDB 控制器一起使用的信息,请参阅 将 MongoDB Search 和向量搜索与 Kubernetes 结合使用。

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

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

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

注意

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

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

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

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

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

    • 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 admin
db.createUser({
user: "mongotUser",
pwd: "<password>",
roles: [ { role: "searchCoordinator", db: "admin" } ]
})
2

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

适用于 Linux 或 macOS:

echo -n "<password>" > /etc/mongot/secrets/passwordFile
chmod 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.replicaSetsyncSource.router 下配置 scramAuthsyncSource.replicaSetmongot 进行身份验证以连接到分片的副本集,syncSource.routermongot 进行身份验证以连接到 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 受密码保护时才需要。

X.509 身份验证允许 mongot 使用 TLS 客户端证书而不是用户名和密码对 MongoDB 部署进行身份验证。使用 X.509 时始终启用 TLS。

要为 mongot 配置 X.509,请完成以下步骤:

1

在您的 MongoDB 副本集上创建具有 searchCoordinator 角色的用户。用户名必须与客户端证书的主题完全匹配。

mongosh中运行以下命令:

use $external
db.createUser({
user: "CN=mongot,OU=...",
roles: [ { role: "searchCoordinator", db: "admin" } ]
})
2

syncSource.replicaSet 下添加 x509 块。对于分片集群,请在 syncSource.router 下添加相同的块。

x509 块中,tlsCertificateKeyFilecaFile 是必需的。在 hostAndPort 中列出多个主机,以便在一个主机不可用时 mongot 仍然可以发现副本集:

syncSource:
replicaSet:
hostAndPort:
- "<host1>:27017"
- "<host2>:27017"
x509:
tlsCertificateKeyFile: /etc/mongot/tls/mongot-client.pem
caFile: /etc/mongot/tls/ca.pem
# tlsCertificateKeyFilePasswordFile: /etc/mongot/secrets/cert-key-password
字段
说明

tlsCertificateKeyFile

必填。包含 X.509 证书和其私钥的 PEM 文件。

caFile

必填。包含一个或多个 X.509 的 PEM 文件验证 mongod 证书的 CA 证书。

tlsCertificateKeyFilePasswordFile

可选.包含密码的文件,如果密钥已加密,则该密码将解密 tlsCertificateKeyFile 中的私钥。

对于分片集群,请在 syncSource.replicaSetsyncSource.router 下配置 x509。对于这两个连接使用相同的字段结构,并在 syncSource.router.hostAndPort 中提供 mongos 终结点:

syncSource:
replicaSet:
hostAndPort:
- "<shard-host1>:27017"
- "<shard-host2>:27017"
x509:
tlsCertificateKeyFile: /etc/mongot/tls/mongot-client.pem
caFile: /etc/mongot/tls/ca.pem
# tlsCertificateKeyFilePasswordFile: /etc/mongot/secrets/cert-key-password
router:
hostAndPort:
- "<mongos1>:27017"
- "<mongos2>:27017"
x509:
tlsCertificateKeyFile: /etc/mongot/tls/mongot-client.pem
caFile: /etc/mongot/tls/ca.pem
# tlsCertificateKeyFilePasswordFile: /etc/mongot/secrets/cert-key-password

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

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

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

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

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

mongotmongod 都会呈现并验证对方的证书。此模式需要 certificateKeyFilecaFile。使用 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