Milvus向量数据库
核心概念
- Collection:Collection 是 Milvus 中数据组织的基本单位,对应 MySQL 中的表
- Schema:定义了 Collection 中每条数据包含哪些字段,对应 MySQL 中的表结构
- Index:包含为向量创建的ANN索引、为标量创建的索引
- Partition:Collection 内部的逻辑子集,可以按某个业务维度把数据分到不同的 Partition 里。比如按文档类别分区:退货政策放一个 Partition,物流规则放一个 Partition,促销活动放一个 Partition
- Entity:一条数据
安装
Docker
Milvus依赖两个外部组件:一个对象存储(用来存索引文件和日志)和一个 etcd(用来存元数据)
docker-compose.yml文件rustfs:替代Milvus默认的MinIO,作为底层对象存储,持久化保存所有向量数据、索引文件、数据段文件etcd:作为Milvus的元数据中心,保存集合 Schema、索引配置、节点状态、任务调度信息等attu:Milvus官方的 Web 管理控制台1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95name: milvus-stack
services:
rustfs:
container_name: rustfs
image: rustfs/rustfs:1.0.0-alpha.72
command:
- "--address"
- ":9000"
- "--console-enable"
- "--access-key"
- "rustfsadmin"
- "--secret-key"
- "rustfsadmin"
- "/data"
environment:
- RUSTFS_ACCESS_KEY=rustfsadmin
- RUSTFS_SECRET_KEY=rustfsadmin
- RUSTFS_CONSOLE_ENABLE=true
ports:
- "9000:9000"
- "9001:9001"
volumes:
- rustfs-data:/data
healthcheck:
test: ["CMD", "sh", "-c", "wget -qO- http://localhost:9000/ || exit 1"]
interval: 30s
timeout: 10s
retries: 5
etcd:
container_name: etcd
image: quay.io/coreos/etcd:v3.5.18
environment:
- ETCD_AUTO_COMPACTION_MODE=revision
- ETCD_AUTO_COMPACTION_RETENTION=1000
- ETCD_QUOTA_BACKEND_BYTES=4294967296
- ETCD_SNAPSHOT_COUNT=50000
command: >
etcd
-advertise-client-urls=http://etcd:2379
-listen-client-urls http://0.0.0.0:2379
--data-dir /etcd
volumes:
- etcd-data:/etcd
healthcheck:
test: ["CMD", "etcdctl", "endpoint", "health"]
interval: 30s
timeout: 20s
retries: 3
standalone:
container_name: milvus-standalone
image: milvusdb/milvus:v2.6.6
command: ["milvus", "run", "standalone"]
security_opt:
- seccomp:unconfined
environment:
ETCD_ENDPOINTS: etcd:2379
MINIO_ADDRESS: rustfs:9000
MINIO_ACCESS_KEY_ID: rustfsadmin
MINIO_SECRET_ACCESS_KEY: rustfsadmin
volumes:
- milvus-data:/var/lib/milvus
ports:
- "19530:19530"
- "9091:9091"
depends_on:
- etcd
- rustfs
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:9091/healthz"]
interval: 30s
start_period: 90s
timeout: 20s
retries: 3
attu:
container_name: milvus-attu
image: zilliz/attu:v2.6.3
environment:
MILVUS_URL: milvus-standalone:19530
ports:
- "8000:3000"
depends_on:
- standalone
volumes:
rustfs-data:
etcd-data:
milvus-data:
networks:
default:
name: milvus-net各组件作用
组件 作用 端口 rustfs 对象存储,存储 Milvus 的索引文件和日志 9000(API)、9001(控制台) etcd 元数据存储,管理 Milvus 的集群元信息 2379 standalone Milvus 单机版服务 19530(gRPC)、9091(健康检查) attu Milvus 可视化管理界面 8000 执行
docker compose up -d命令访问
http://localhost:8000/控制台,默认不要账号密码
使用
引入依赖
1 | |
创建Collection
1 | |
插入数据
注意,在没有创建索引前,虽然该单元测试显示插入成功,但是 Milvus 控制台查看依然是 0 条数据。只有在创建索引且加载 Collection 到内存才会正常展示。
1 | |
创建索引
没有索引的话,Milvus 只能做暴力搜索
1 | |
1 | |
检索
1 | |
条件检索
1 | |
Milvus向量数据库
http://xwww12.github.io/2026/06/27/数据库/Milvus向量数据库/