您可以在MongoDB Community Edition中安装搜索进程mongot。搜索进程可作为映像部署在Docker中。mongot进程支持使用您自己的嵌入或自动嵌入进行全文搜索和语义搜索。
使用 Docker 安装 MongoDB Search 和向量搜索
注意
要更快地使用用于实验的部署,请参阅 MongoDB Search 和 MongoDB Vector Search 本地开发快速入门。
开始之前
重要
mongot 同步 mongod 的数据,并需要 MongoDB 副本集。独立运行的 mongod 部署不支持 mongot。
在部署 mongot 之前,请完成以下步骤:
安装Docker和Docker Compose。
如果您希望MongoDB Vector Search 自动为集合中的文本数据生成嵌入,请创建终结点服务API密钥。要学习更多信息,请参阅自动嵌入。
重要
自动嵌入处于预览状态。在预览期间,功能和相应的文档可能随时更改。要学习;了解更多信息,请参阅预览功能。
如果您还没有密钥,可以从 Atlas 用户界面 创建终结点服务 API 密钥。我们建议您从两个 Atlas 项目创建两个密钥,一个用于在索引时生成嵌入,另一个用于在查询时生成嵌入。
注意
用于生成嵌入的提供商终结点取决于您是从Atlas用户界面创建API密钥,还是直接从 Voyage AI创建 API 密钥。
步骤
警告
此过程提供一个最小且不安全的默认配置,该配置不启用访问控制。这些步骤创建一个部署,仅用于本地实验。
在曝露这些容器并在生产环境中使用您的设置之前,请参阅以下资源以保护您的部署:
下载 MongoDB Docker 映像。
要下载MongoDB Docker映像,运行以下命令:
docker pull mongodb/mongodb-community-server:latest
注意
您必须至少拥有MongoDB 8.2+ 才能将MongoDB Search 与本地部署结合使用。有关更多信息,请参阅 MongoDB Search 兼容性。
有关使用Docker安装MongoDB的更多详细信息,请参阅使用Docker安装MongoDB Community。
创建 mongot 同步用户密码文件。
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 标志可防止出现尾随换行符。
创建mongod配置文件。
要创建配置文件,请将以下代码保存到 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
启动 mongod。
要启动 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
为 MongoDB 部署上的 mongot进程创建用户。
mongot 必须能够通过具有searchCoordinator角色的用户连接到MongoDB 部署。
运行以下命令以连接到 admin数据库:
use admin
要创建具有 searchCoordinator角色的用户:
将
<mongot_username>替换为mongot用户的用户名。将
<mongot_password>替换为您在步骤 4 中的passwordFile中指定的密码。运行以下命令:
db.createUser( { user: "<mongot_username>", pwd: "<mongot_password>", roles: [ "searchCoordinator" ] } )
有关创建用户的更多信息,请参阅在自管理部署上创建用户。
指定搜索配置选项。
您可以使用 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网络上运行。
启动mongot进程。
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.passwordFilein yourmongotconfiguration file to"/secrets/passwordFile"instead of"/passwordFile". The command mounts themongot-secretsvolume that you created in step 4 at/secretsin 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-communityDocker网络上启动容器。
验证 mongot 进程的运行状况。
要验证,请使用 HTTP 客户端或 curl 向 /health 终结点发送请求。例如,发送类似以下示例请求的 curl 请求:
curl localhost:8080/health
端点在响应中返回以下内容之一:
SERVING, 如果mongot进程正在运行mongot可能无法提供服务查询,因为它不检查索引的状态,而索引必须处于Ready状态才能提供服务查询。NOT_SERVING,如果mongot进程未运行
有关详细信息,请参阅验证 mongot 连接。
使用 Docker 卸载 MongoDB Search 和 MongoDB 向量搜索
要从系统中完全删除 MongoDB Search 和 MongoDB 向量搜索,请删除容器映像、配置文件以及任何数据或日志目录。以下部分将指导您完成必要步骤。
删除 mongot 用户。
要删除 mongot 用户,请运行以下命令,并将 <mongot_username> 替换为为 mongot 创建的用户名:
use admin db.dropUser("<mongot_username>")
有关详情,请参阅 db.dropUser()。
后续步骤
创建MongoDB Search 索引