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

在 Docker 容器中安装或卸载 Community 上的 MongoDB Search

您可以在MongoDB Community Edition中安装搜索进程mongot。搜索进程可作为映像部署在Docker中。mongot进程支持使用您自己的嵌入或自动嵌入进行全文搜索和语义搜索。

注意

要更快地使用用于实验的部署,请参阅 MongoDB Search 和 MongoDB Vector Search 本地开发快速入门。

重要

mongot 同步 mongod 的数据,并需要 MongoDB 副本集。独立运行的 mongod 部署不支持 mongot

在部署 mongot 之前,请完成以下步骤:

  1. 安装Docker和Docker Compose。

  2. 如果您希望MongoDB Vector Search 自动为集合中的文本数据生成嵌入,请创建终结点服务API密钥。要学习更多信息,请参阅自动嵌入。

    重要

    自动嵌入处于预览状态。在预览期间,功能和相应的文档可能随时更改。要学习;了解更多信息,请参阅预览功能。

    如果您还没有密钥,可以从 Atlas 用户界面 创建终结点服务 API 密钥。我们建议您从两个 Atlas 项目创建两个密钥,一个用于在索引时生成嵌入,另一个用于在查询时生成嵌入。

    注意

    用于生成嵌入的提供商终结点取决于您是从Atlas用户界面创建API密钥,还是直接从 Voyage AI创建 API 密钥。

警告

此过程提供一个最小且不安全的默认配置,该配置不启用访问控制。这些步骤创建一个部署,仅用于本地实验。

在曝露这些容器并在生产环境中使用您的设置之前,请参阅以下资源以保护您的部署:

1

要拉取 mongot Docker映像,运行以下命令:

docker pull mongodb/mongodb-community-search:latest

要验证映像是否位于Docker桌面上,运行以下命令:

docker image ls mongodb/mongodb-community-search
2

要为数据库和搜索容器之间的容器间通信创建 Docker 网络,运行以下命令:

docker network create search-community
3

要下载MongoDB Docker映像,运行以下命令:

docker pull mongodb/mongodb-community-server:latest

注意

您必须至少拥有MongoDB 8.2+ 才能将MongoDB Search 与本地部署结合使用。有关更多信息,请参阅 MongoDB Search 兼容性。

有关使用Docker安装MongoDB的更多详细信息,请参阅使用Docker安装MongoDB Community

4

Create a password file for mongot to connect to mongod. Select your operating system and run the command:

Replace <mongot_password> with your password and run the following command to create a file called passwordFile in your current directory:

echo -n "<mongot_password>" > passwordFile
chmod 400 passwordFile

Replace <mongot_password> with your password and run the following command to create a file called passwordFile inside a Docker-managed named volume called mongot-secrets. You reference this volume instead of a host path when you start mongot in a later step.

docker volume create mongot-secrets
docker run --rm -v mongot-secrets:/secrets busybox sh -c "echo -n '<mongot_password>' > /secrets/passwordFile && chmod 400 /secrets/passwordFile"

On Windows, a bind-mounted host file does not preserve Unix permission bits inside a Linux container. Docker Desktop's file-sharing layer overrides the permissions that you set on the NTFS side, and Windows has no native chmod equivalent. The throwaway container in the preceding command sets the permissions correctly on the Linux side.

注意

-n 标志可防止出现尾随换行符。

5

要创建配置文件,请将以下代码保存到 mongod.conf 或您的首选位置。

# MongoDB Configuration File
# Network configuration
net:
port: 27017
bindIpAll: true # Equivalent to --bind_ip_all
# Replica set configuration
replication:
replSetName: rs0
# Search configuration parameters
setParameter:
# Server parameters to advise mongod of mongot availability for search index management and querying
searchIndexManagementHostAndPort: mongot-community.search-community:27028
mongotHost: mongot-community.search-community:27028
skipAuthenticationToSearchIndexManagementServer: false
useGrpcForSearch: true
searchTLSMode: disabled
6

要启动 mongod

  • </path/to/data/db> 替换为已挂载卷的本地目录路径。

  • </path/to/mongod.conf> 替换为上面创建的配置文件的路径。

  • On Windows, replace the backslash (\) at the end of each line with the line continuation character for your shell. Command Prompt uses a caret (^), and PowerShell uses a backtick.

docker run --rm \
--name mongod \
-v </path/to/mongod.conf>:/etc/mongod.conf:ro \
-v </path/to/data/db>:/data/db \
-p 27017:27017 \
--network search-community \
mongodb/mongodb-community-server:latest \
--config /etc/mongod.conf \
--replSetMember=mongod.search-community:27017
7

Run the following command to connect to the mongod instance you started on port 27017:

docker exec -it mongod mongosh --port 27017
8

mongot 必须能够通过具有searchCoordinator角色的用户连接到MongoDB 部署。

运行以下命令以连接到 admin数据库:

use admin

要创建具有 searchCoordinator角色的用户:

  • <mongot_username> 替换为 mongot 用户的用户名。

  • <mongot_password> 替换为您在步骤 4 中的 passwordFile 中指定的密码。

  • 运行以下命令:

db.createUser(
{
user: "<mongot_username>",
pwd: "<mongot_password>",
roles: [ "searchCoordinator" ]
}
)

有关创建用户的更多信息,请参阅在自管理部署上创建用户。

9

您可以使用 YAML 配置文件配置 mongot。您必须将在上一步中指定的用户名指定为 syncSource.replicaSet.scramAuth.username。您还必须将上一步中创建的 passwordFile 指定为 syncSource.replicaSet.scramAuth.passwordFile

有关 mongot 配置选项的更多信息,请参阅 mongot 选项。

示例,您可以将设置调整为本地配置,如下所示:

# mongot.conf
syncSource:
replicaSet:
hostAndPort: "mongod.search-community:27017"
scramAuth:
username: "mongotUser"
passwordFile: "/passwordFile"
authSource: "admin"
tls:
enabled: false
replicationReader:
readPreference: "secondaryPreferred"
storage:
dataPath: "/data/mongot"
server:
grpc:
address: "mongot-community.search-community:27028"
tls:
mode: "disabled"
metrics:
enabled: true
address: "mongot-community.search-community:9946"
healthCheck:
address: "mongot-community.search-community:8080"
logging:
verbosity: INFO

将文件保存到 mongot.config 或您的首选文件位置。

两个容器在同一 search-community Docker网络上运行。

10

To start the Search in Community binary (mongot), select your operating system and run the command:

  • </path/to/data/mongot> 替换为已挂载卷的本地目录路径,用于存储 mongot 数据。

  • </path/to/mongot.conf> 替换为您在上一步中创建的 mongot 配置文件的路径。

  • Replace </path/to/passwordFile> with the path to the password file that you created.

docker run --rm \
--name mongot-community \
-v </path/to/data/mongot>:/data/mongot \
-v </path/to/mongot.conf>:/mongot-community/config.default.yml \
-v </path/to/passwordFile>:/passwordFile:ro \
--network search-community \
-p 8080:8080 \
-p 9946:9946 \
mongodb/mongodb-community-search:latest
  • </path/to/data/mongot> 替换为已挂载卷的本地目录路径,用于存储 mongot 数据。

  • </path/to/mongot.conf> 替换为您在上一步中创建的 mongot 配置文件的路径。

  • Set syncSource.replicaSet.scramAuth.passwordFile in your mongot configuration file to "/secrets/passwordFile" instead of "/passwordFile". The command mounts the mongot-secrets volume that you created in step 4 at /secrets in the container.

  • In PowerShell, replace each caret (^) with a backtick. The command uses the caret (^) line continuation character for Command Prompt.

docker run --rm ^
--name mongot-community ^
-v </path/to/data/mongot>:/data/mongot ^
-v </path/to/mongot.conf>:/mongot-community/config.default.yml ^
-v mongot-secrets:/secrets:ro ^
--network search-community ^
-p 8080:8080 ^
-p 9946:9946 ^
mongodb/mongodb-community-search:latest

此命令:

  • 挂载卷。

  • 从本地卷挂载配置文件。

  • 指定端口范围。

  • 公开指标端口。

  • 使用名为 mongot-community 的容器在 search-community Docker网络上启动容器。

11

要验证,请使用 HTTP 客户端或 curl/health 终结点发送请求。例如,发送类似以下示例请求的 curl 请求:

curl localhost:8080/health

端点在响应中返回以下内容之一:

  • SERVING, 如果 mongot 进程正在运行

    mongot 可能无法提供服务查询,因为它不检查索引的状态,而索引必须处于 Ready 状态才能提供服务查询。

  • NOT_SERVING,如果 mongot 进程未运行

有关详细信息,请参阅验证 mongot 连接。

要从系统中完全删除 MongoDB Search 和 MongoDB 向量搜索,请删除容器映像、配置文件以及任何数据或日志目录。以下部分将指导您完成必要步骤。

1

要删除 mongot 用户,请运行以下命令,并将 <mongot_username> 替换为为 mongot 创建的用户名:

use admin
db.dropUser("<mongot_username>")

有关详情,请参阅 db.dropUser()

2

要停止 mongot进程,请停止在Docker中运行的容器映像。

3

要删除mongotmongod 配置文件,请使用配置文件的完整路径运行以下命令:

rm /path/to/config.default.yml
rm /path/to/mongod.conf
4

要删除密码文件,请使用密码文件的完整路径运行以下命令:

rm /path/to/passwordFile
5

要删除存储 mongot 数据的数据目录,请使用 mongot数据目录的完整路径运行以下命令:

rm /path/to/mongot/data