工具 & 效率18 分钟阅读
Milvus 运维命令 100 条
Milvus 是面向向量检索和生成式 AI 场景的分布式向量数据库。它的数据管理对象包括 Database、Collection、Partition、Entity、Index 和 Resource Group,集群部署还涉及 Proxy、QueryNode、DataNode、MixCoord、StreamingNode、etcd、对象存储和消息系统等组件。
2026年8月19日阅读—点赞—收藏—
dba100墨力计划milvus
在知识库中专注阅读,并随时返回相关工具与课程
Milvus 是面向向量检索和生成式 AI 场景的分布式向量数据库。它的数据管理对象包括 Database、Collection、Partition、Entity、Index 和 Resource Group,集群部署还涉及 Proxy、QueryNode、DataNode、MixCoord、StreamingNode、etcd、对象存储和消息系统等组件。
Milvus 是面向向量检索和生成式 AI 场景的分布式向量数据库。它的数据管理对象包括 Database、Collection、Partition、Entity、Index 和 Resource Group,集群部署还涉及 Proxy、QueryNode、DataNode、MixCoord、StreamingNode、etcd、对象存储和消息系统等组件。
下面整理 Milvus 2.6 常用的 100 条开发与运维命令。为了避免新旧接口混用,本文主要采用官方推荐的 PyMilvus MilvusClient 接口,最后一部分补充 Kubernetes、健康检查、监控和 Milvus Backup 命令。
不同 Milvus 版本、Lite/Standalone/Distributed 部署模式及托管服务的功能存在差异。删除 Collection、释放资源、修改索引、压缩和恢复备份前,应确认影响范围。文中的地址、Token、数据库、Collection 和路径均为示例。
1python3 -m pip install -U pymilvusPyMilvus 版本应与 Milvus 服务端大版本兼容。
1python3 -c "import pymilvus; print(pymilvus.__version__)"1from pymilvus import MilvusClient23client = MilvusClient(4 uri="http://localhost:19530",5 token="root:Milvus"6)生产环境应启用 TLS 并替换默认账号密码。
1client = MilvusClient(2 uri="http://localhost:19530",3 token="root:Milvus",4 db_name="appdb"5)1client = MilvusClient(2 uri="https://milvus.example.com:19530",3 token="appuser:password",4 secure=True5)证书参数应根据部署方式配置。
1from pymilvus import connections, utility23connections.connect(4 alias="ops",5 uri="http://localhost:19530",6 token="root:Milvus"7)8print(utility.get_server_version(using="ops"))1print(client.list_collections())1print(client.list_collections(timeout=10))1client.close()1python3 -c \2"from pymilvus import MilvusClient; help(MilvusClient)"1print(client.list_databases())1client.create_database(db_name="appdb")1client.use_database(db_name="appdb")1print(client.describe_database(db_name="appdb"))1client.create_database(2 db_name="tenant_a",3 properties={4 "database.replica.number": 25 }6)属性是否支持取决于当前版本和部署模式。
1client.alter_database_properties(2 db_name="tenant_a",3 properties={"database.replica.number": 1}4)1client.drop_database_properties(2 db_name="tenant_a",3 property_keys=["database.replica.number"]4)1client.use_database("appdb")2print(client.list_collections())1for name in client.list_collections():2 client.drop_collection(collection_name=name)这是破坏性操作,只能在明确需要清空目标数据库时使用。
1client.use_database("default")2client.drop_database(db_name="tenant_a")删除前必须确保数据库中没有 Collection。
1client.create_collection(2 collection_name="documents",3 dimension=768,4 metric_type="COSINE"5)快速创建模式会生成默认主键和向量字段。
1from pymilvus import DataType23schema = MilvusClient.create_schema(4 auto_id=False,5 enable_dynamic_field=False6)1schema.add_field(2 field_name="doc_id",3 datatype=DataType.INT64,4 is_primary=True5)1schema.add_field(2 field_name="embedding",3 datatype=DataType.FLOAT_VECTOR,4 dim=7685)1schema.add_field(2 field_name="title",3 datatype=DataType.VARCHAR,4 max_length=5125)1schema.add_field(2 field_name="metadata",3 datatype=DataType.JSON,4 nullable=True5)1client.create_collection(2 collection_name="documents_v2",3 schema=schema4)未创建向量索引时,Collection 不会自动加载。
1print(client.list_collections())1print(client.has_collection(2 collection_name="documents_v2"3))1print(client.describe_collection(2 collection_name="documents_v2"3))1print(client.get_collection_stats(2 collection_name="documents_v2"3))1client.rename_collection(2 old_name="documents_v2",3 new_name="documents_prod"4)1client.alter_collection_properties(2 collection_name="documents_prod",3 properties={"collection.ttl.seconds": 604800}4)TTL 删除是异步过程,不适合要求立即物理删除的数据。
1client.alter_collection_properties(2 collection_name="documents_prod",3 properties={"mmap.enabled": True}4)1client.drop_collection_properties(2 collection_name="documents_prod",3 property_keys=["collection.ttl.seconds"]4)1client.drop_collection(2 collection_name="documents_prod"3)1print(client.list_partitions(2 collection_name="documents"3))1client.create_partition(2 collection_name="documents",3 partition_name="p_2026"4)1print(client.has_partition(2 collection_name="documents",3 partition_name="p_2026"4))1print(client.get_partition_stats(2 collection_name="documents",3 partition_name="p_2026"4))1schema.add_field(2 field_name="tenant_id",3 datatype=DataType.VARCHAR,4 max_length=64,5 is_partition_key=True6)分区键 Collection 由 Milvus 自动管理分区。
1client.load_partitions(2 collection_name="documents",3 partition_names=["p_2026"]4)1client.release_partitions(2 collection_name="documents",3 partition_names=["p_2026"]4)1client.drop_partition(2 collection_name="documents",3 partition_name="p_2026"4)默认分区不能删除,非空分区删除前应确认数据影响。
1result = client.insert(2 collection_name="documents",3 data=[{4 "id": 1,5 "vector": [0.1] * 768,6 "title": "Milvus"7 }]8)9print(result)字段名应与实际 Collection Schema 一致。
1rows = [2 {"id": i, "vector": [float(i % 10) / 10] * 768}3 for i in range(1, 1001)4]5print(client.insert("documents", rows))1client.insert(2 collection_name="documents",3 partition_name="p_2026",4 data=[{"id": 1001, "vector": [0.2] * 768}]5)1client.upsert(2 collection_name="documents",3 data=[{"id": 1, "vector": [0.3] * 768}]4)1print(client.get(2 collection_name="documents",3 ids=[1, 2, 3],4 output_fields=["id", "title"]5))1print(client.query(2 collection_name="documents",3 filter="id >= 1 and id <= 100",4 output_fields=["id", "title"],5 limit=1006))1print(client.query(2 collection_name="documents",3 filter="",4 output_fields=["count(*)"]5))1print(client.delete(2 collection_name="documents",3 ids=[1, 2, 3]4))1print(client.delete(2 collection_name="documents",3 filter="id < 100"4))1client.flush(collection_name="documents")正常写入由系统自动持久化,手动 Flush 不应高频调用。
1index_params = client.prepare_index_params()1index_params.add_index(2 field_name="vector",3 index_type="AUTOINDEX",4 metric_type="COSINE"5)1index_params.add_index(2 field_name="vector",3 index_name="idx_vector_hnsw",4 index_type="HNSW",5 metric_type="COSINE",6 params={"M": 32, "efConstruction": 200}7)索引参数应通过真实数据压测确定。
1index_params.add_index(2 field_name="vector",3 index_name="idx_vector_ivf",4 index_type="IVF_FLAT",5 metric_type="L2",6 params={"nlist": 1024}7)1client.create_index(2 collection_name="documents",3 index_params=index_params4)1client.create_index(2 collection_name="documents",3 index_params=index_params,4 sync=False5)1print(client.list_indexes(2 collection_name="documents"3))1print(client.describe_index(2 collection_name="documents",3 index_name="idx_vector_hnsw"4))1client.alter_index_properties(2 collection_name="documents",3 index_name="idx_vector_hnsw",4 properties={"mmap.enabled": True}5)1client.drop_index_properties(2 collection_name="documents",3 index_name="idx_vector_hnsw",4 property_keys=["mmap.enabled"]5)1client.drop_index(2 collection_name="documents",3 index_name="idx_vector_hnsw"4)删除前应先释放 Collection,并确认不存在依赖该索引的在线查询。
1client.load_collection(2 collection_name="documents"3)向量字段必须先创建索引。
1print(client.get_load_state(2 collection_name="documents"3))1print(utility.loading_progress(2 collection_name="documents",3 using="ops"4))1client.release_collection(2 collection_name="documents"3)1result = client.search(2 collection_name="documents",3 data=[[0.1] * 768],4 anns_field="vector",5 limit=10,6 output_fields=["id", "title"]7)8print(result)1result = client.search(2 collection_name="documents",3 data=[[0.1] * 768],4 filter="id >= 1000",5 limit=10,6 output_fields=["id", "title"]7)1result = client.search(2 collection_name="documents",3 data=[[0.1] * 768],4 search_params={5 "metric_type": "COSINE",6 "params": {"ef": 128}7 },8 limit=109)1result = client.search(2 collection_name="documents",3 data=[[0.1] * 768],4 search_params={5 "metric_type": "L2",6 "params": {"nprobe": 32}7 },8 limit=109)1result = client.search(2 collection_name="documents",3 partition_names=["p_2026"],4 data=[[0.1] * 768],5 limit=106)1result = client.search(2 collection_name="documents",3 data=[[0.1] * 768],4 search_params={5 "metric_type": "COSINE",6 "params": {"radius": 0.8, "range_filter": 0.95}7 },8 limit=1009)1from pymilvus import AnnSearchRequest, RRFRanker23req1 = AnnSearchRequest(4 data=[[0.1] * 768],5 anns_field="dense_vector",6 param={"metric_type": "COSINE"},7 limit=208)9req2 = AnnSearchRequest(10 data=["database"],11 anns_field="sparse_vector",12 param={"metric_type": "IP"},13 limit=2014)15result = client.hybrid_search(16 "documents", [req1, req2], RRFRanker(), limit=1017)1client.create_alias(2 collection_name="documents",3 alias="documents_online"4)1print(client.list_aliases(2 collection_name="documents"3))1print(client.describe_alias(2 alias="documents_online"3))1client.alter_alias(2 collection_name="documents_v3",3 alias="documents_online"4)1client.drop_alias(alias="documents_online")1job_id = client.compact(2 collection_name="documents"3)4print(job_id)1print(client.get_compact_state(job_id=job_id))PyMilvus 新版本逐步将该方法更名为 get_compaction_state(),应按客户端版本选择。
1print(client.list_resource_groups())1print(client.describe_resource_group(2 resource_group_name="default"3))1client.create_resource_group(2 resource_group_name="rg_search"3)1client.transfer_node(2 source_resource_group="default",3 target_resource_group="rg_search",4 num_nodes=25)1client.transfer_replica(2 source_resource_group="default",3 target_resource_group="rg_search",4 collection_name="documents",5 num_replicas=16)1client.drop_resource_group(2 resource_group_name="rg_search"3)删除前必须先迁出节点和副本。
1client.create_user(2 user_name="appuser",3 password="Replace_With_Strong_Password"4)1print(client.list_users())1client.update_password(2 user_name="appuser",3 old_password="OldPassword",4 new_password="NewStrongPassword"5)1client.create_role(role_name="app_readonly")1client.grant_role(2 user_name="appuser",3 role_name="app_readonly"4)1client.grant_privilege(2 role_name="app_readonly",3 object_type="Collection",4 privilege="Search",5 object_name="documents",6 db_name="appdb"7)1print(client.describe_role(2 role_name="app_readonly"3))1kubectl get pods -n milvus -o wide1kubectl logs -n milvus \2deployment/milvus-proxy --since=30m控制器名称应根据 Helm Release 和实际部署调整。
1curl -s http://localhost:9091/healthz2curl -s http://localhost:9091/metrics | head管理端口、路径和访问方式取决于部署配置。
1./milvus-backup create -n appdb_202607272./milvus-backup list3./milvus-backup restore \4 -n appdb_20260727 \5 -s _recover恢复后应重新检查 Collection、索引、加载状态、实体数量和抽样搜索结果。
Milvus 运维的重点是同时关注数据对象、索引、内存加载、组件资源和底层依赖。搜索变慢时,不能只调整 limit 或 nprobe,还要检查索引类型、数据分布、QueryNode 内存、Segment 状态和对象存储延迟。
生产环境应建立备份恢复演练、指标监控和容量规划,并严格区分 Milvus Lite、Standalone、Distributed 以及托管服务支持的功能边界。