跳到主要内容
版本:v3.0.x

连接 Milvus 服务器

本主题介绍如何建立与 Milvus 服务器的客户端连接并配置常用连接选项。

前提条件

通过 URI 连接(禁用身份验证)

使用 Milvus 服务器地址(如http://localhost:19530 )建立连接。

from pymilvus import MilvusClient

client = MilvusClient("http://localhost:19530")

使用凭证连接(启用身份验证)

提供"username:password"userpassword 形式的令牌。默认内置管理员为root:Milvus (生产时可更改)。

from pymilvus import MilvusClient

# Token form
client = MilvusClient(
uri="http://localhost:19530",
token="root:Milvus",
)

# Or explicit user/password
client = MilvusClient(
uri="http://localhost:19530",
user="root",
password="Milvus",
)
说明

令牌格式为"<username>:<password>" 。文档明确指出root:Milvus 为默认凭据,而《创建用户和角色》指南则涉及用户管理。

配置超时

设置客户端连接的默认超时时间:

from pymilvus import MilvusClient

client = MilvusClient(uri="http://localhost:19530", timeout=1000) # If not set, the timeout defaults to 10s
说明
  • 对于上面列出的 SDK,该超时仅在建立连接时使用,并不作为其他 API 操作符的默认超时。

  • 对于 RESTful API,Request-Timeout 是以秒为单位的每个请求的截止时间(与 Java 的rpcDeadlineMs 和 Node.jstimeout 不同,后者以毫秒为单位),因此请在每个需要截止时间的调用中包含它。

连接到特定数据库

在构建过程中,使用db_name 选择目标数据库。也可以稍后使用using_database() 进行切换。

from pymilvus import MilvusClient

# Set the database when creating the client
client = MilvusClient(
uri="http://localhost:19530",
token="root:Milvus",
db_name="analytics",
)

# (Optional) Switch the active database later
# client.using_database("reports")
说明

有关创建、列出和描述数据库以及更广泛的数据库管理任务,请参阅数据库指南。

下一步