資料庫
Milvus 在集合之上引進資料庫層,提供更有效率的方式來管理和組織資料,同時支援多租用。
什麼是資料庫
在 Milvus 中,資料庫是組織和管理資料的邏輯單位。為了增強資料安全性並實現多租戶,您可以建立多個資料庫,為不同的應用程式或租戶在邏輯上隔離資料。例如,您建立一個資料庫儲存使用者 A 的資料,另一個資料庫則儲存使用者 B 的資料。
建立資料庫
您可以使用 Milvus RESTful API 或 SDK 程式化地建立資料。
from pymilvus import MilvusClient
client = MilvusClient(
uri="http://localhost:19530",
token="root:Milvus"
)
client.create_database(
db_name="my_database_1"
)
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.service.database.request.*;
ConnectConfig config = ConnectConfig.builder()
.uri("http://localhost:19530")
.token("root:Milvus")
.build();
MilvusClientV2 client = new MilvusClientV2(config);
CreateDatabaseReq createDatabaseReq = CreateDatabaseReq.builder()
.databaseName("my_database_1")
.build();
client.createDatabase(createDatabaseReq);
import {MilvusClient} from '@zilliz/milvus2-sdk-node';
const client = new MilvusClient({
address: "http://localhost:19530",
token: 'root:Milvus'
});
await client.createDatabase({
db_name: "my_database_1"
});
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: "localhost:19530",
Username: "Milvus",
Password: "root",
})
if err != nil {
// handle err
}
err = cli.CreateDatabase(ctx, milvusclient.NewCreateDatabaseOption("my_database_1"))
if err != nil {
// handle err
}
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/databases/create" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"dbName": "my_database_1"
}'
您也可以在建立資料庫時設定資料庫的屬性。以下範例設定資料庫的複製本數量。
client.create_database(
db_name="my_database_2",
properties={
"database.replica.number": 3
}
)
Map<String, String> properties = new HashMap<>();
properties.put("database.replica.number", "3");
CreateDatabaseReq createDatabaseReq = CreateDatabaseReq.builder()
.databaseName("my_database_2")
.properties(properties)
.build();
client.createDatabase(createDatabaseReq);
await client.createDatabase({
db_name: "my_database_2",
properties: {
"database.replica.number": 3
}
});
err := cli.CreateDatabase(ctx, milvusclient.NewCreateDatabaseOption("my_database_2").WithProperty("database.replica.number", 3))
if err != nil {
// handle err
}
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/databases/create" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"dbName": "my_database_2",
"properties": {
"database.replica.number": 3
}
}'
檢視資料庫
您可以使用 Milvus RESTful API 或 SDK 來列出所有現有資料庫,並檢視其詳細資料。
# List all existing databases
client.list_databases()
# Output
# ['default', 'my_database_1', 'my_database_2']
# Check database details
client.describe_database(
db_name="default"
)
# Output
# {"name": "default"}
import io.milvus.v2.service.database.response.*;
ListDatabasesResp listDatabasesResp = client.listDatabases();
DescribeDatabaseResp descDBResp = client.describeDatabase(DescribeDatabaseReq.builder()
.databaseName("default")
.build());
await client.describeDatabase({
db_name: 'default'
});
// List all existing databases
databases, err := cli.ListDatabase(ctx, milvusclient.NewListDatabaseOption())
if err != nil {
// handle err
}
log.Println(databases)
db, err := cli.DescribeDatabase(ctx, milvusclient.NewDescribeDatabaseOption("default"))
if err != nil {
// handle err
}
log.Println(db)
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/databases/describe" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"dbName": "default"
}'
管理資料庫屬性
每個資料庫都有自己的屬性,您可以在建立資料庫時設定資料庫的屬性,如建立資料庫所述,也可以更改和刪除任何現有資料庫的屬性。
下表列出了可能的資料庫屬性。
屬性名稱 |
類型 |
屬性 描述 |
|---|---|---|
|
整數 |
指定資料庫的複製數。 |
|
字串 |
與指定資料庫關聯的資源群組的名稱,以逗號分隔的清單。 |
|
整數 |
指定資料庫的最大磁碟空間大小,單位為 MB。 |
|
整數 |
指定資料庫允許的最大收藏集數量。 |
|
布林 |
是否強制指定資料庫拒絕寫入作業。 |
|
布林 |
是否強制指定資料庫拒絕讀取操作。 |
|
字串 |
指定應用於資料庫內時間敏感作業的預設時區,尤其是 |
變更資料庫的屬性
您可以按以下方式更改現有資料庫的屬性。以下範例限制您可以在資料庫中建立的集合數量。
client.alter_database_properties(
db_name="my_database_1",
properties={
"database.max.collections": 10
}
)
client.alterDatabaseProperties(AlterDatabasePropertiesReq.builder()
.databaseName("my_database_1")
.property("database.max.collections", "10")
.build());
await milvusClient.alterDatabaseProperties({
db_name: "my_database_1",
properties: {"database.max.collections", "10" },
})
err := cli.AlterDatabaseProperties(ctx, milvusclient.NewAlterDatabasePropertiesOption("my_database_1").
WithProperty("database.max.collections", 1))
if err != nil {
// handle err
}
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/databases/alter" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"dbName": "my_database",
"properties": {
"database.max.collections": 10
}
}'
刪除資料庫屬性
您也可以透過以下方式丟除資料庫的屬性,重新設定資料庫的屬性。以下範例移除您可以在資料庫中建立的集合數量限制。
client.drop_database_properties(
db_name="my_database_1",
property_keys=[
"database.max.collections"
]
)
client.dropDatabaseProperties(DropDatabasePropertiesReq.builder()
.databaseName("my_database_1")
.propertyKeys(Collections.singletonList("database.max.collections"))
.build());
await milvusClient.dropDatabaseProperties({
db_name: my_database_1,
properties: ["database.max.collections"],
});
err := cli.DropDatabaseProperties(ctx, milvusclient.NewDropDatabasePropertiesOption("my_database_1", "database.max.collections"))
if err != nil {
// handle err
}
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/databases/alter" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"dbName": "my_database",
"propertyKeys": [
"database.max.collections"
]
}'
使用資料庫
您可以從一個資料庫切換到另一個資料庫,而不用中斷 Milvus 的連線。
RESTful API 不支援此操作。
client.use_database(
db_name="my_database_2"
)
client.useDatabase("my_database_2");
await milvusClient.useDatabase({
db_name: "my_database_2",
});
err = cli.UseDatabase(ctx, milvusclient.NewUseDatabaseOption("my_database_2"))
if err != nil {
// handle err
}
# This operation is unsupported because RESTful does not provide a persistent connection.
# As a workaround, initiate the required request again with the target database.
刪除資料庫
一旦不再需要資料庫,您可以丟棄資料庫。請注意
無法丟棄預設資料庫。
在丟棄資料庫之前,您需要先丟棄資料庫中的所有集合。
您可以使用 Milvus RESTful API 或 SDK 程式化建立資料。
client.drop_database(
db_name="my_database_2"
)
client.dropDatabase(DropDatabaseReq.builder()
.databaseName("my_database_2")
.build());
await milvusClient.dropDatabase({
db_name: "my_database_2",
});
err = cli.DropDatabase(ctx, milvusclient.NewDropDatabaseOption("my_database_2"))
if err != nil {
// handle err
}
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/databases/drop" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"dbName": "my_database"
}'
常見問題
如何管理資料庫的權限?
Milvus 使用基於角色的存取控制 (RBAC) 來管理權限。您可以創建具有特定權限的角色,並將其分配給用戶,從而控制他們對不同資料庫的訪問。如需詳細資訊,請參閱RBAC 文件。
資料庫有配額限制嗎?
是的,Milvus允許您為數據庫設置配額限制,例如最大收藏集數量。有關限制的全面列表,請參閱Milvus Limits 文檔。