🚀 免費嘗試 Zilliz Cloud,完全托管的 Milvus,體驗速度提升 10 倍!立即嘗試

milvus-logo
LFAI
主頁
  • 使用者指南

管理資料庫

類似於傳統的資料庫引擎,您也可以在 Milvus 中建立資料庫,並分配權限給特定的使用者來管理資料庫。這樣,這些用戶就有權管理資料庫中的集合。一個 Milvus 集群最多支援 64 個資料庫。

本頁的程式碼片段使用PyMilvus ORM 模組與 Milvus 互動。使用新的MilvusClient SDK的程式碼片段即將推出。

建立資料庫

使用connect()連接到 Milvus 伺服器,並使用create_database()建立新資料庫:

使用MilvusClient連線至 Milvus 伺服器,並使用createDatabase()建立新資料庫:

使用MilvusClient連線到 Milvus 伺服器,並createDatabase()來建立新資料庫:

from pymilvus import connections, db

conn = connections.connect(host="127.0.0.1", port=19530)

database = db.create_database("my_database")
import io.milvus.client.MilvusServiceClient;
import io.milvus.param.ConnectParam;
import io.milvus.param.collection.CreateDatabaseParam;

// 1. Connect to Milvus server
ConnectParam connectParam = ConnectParam.newBuilder()
    .withUri(CLUSTER_ENDPOINT)
    .withToken(TOKEN)
    .build();

MilvusServiceClient client = new MilvusServiceClient(connectParam);

// 3. Create a new database
CreateDatabaseParam createDatabaseParam = CreateDatabaseParam.newBuilder()
    .withDatabaseName("")
    .build();

R<RpcStatus> response = client.createDatabase(createDatabaseParam);
const address = "http://localhost:19530";

// 1. Set up a Milvus Client
client = new MilvusClient({ address });

// 3. Create a database
res = await client.createDatabase({
    db_name: "my_database",
});

console.log(res);

// {
//   error_code: 'Success',
//   reason: '',
//   code: 0,
//   retriable: false,
//   detail: ''
// }

上面的程式碼片段連接到預設資料庫,並建立一個名為my_database 的新資料庫。

使用資料庫

Milvus 集群隨附一個預設資料庫,命名為 'default'。除非另有指定,否則會在預設資料庫中建立集合。

要變更預設資料庫,請執行下列步驟:

db.using_database("my_database")
// No equivalent method is available.
// 4. Activate another database
res = await client.useDatabase({
    db_name: "my_database",
});

console.log(res);

您也可以設定在連線到 Milvus 叢集時使用的資料庫,如下所示:

conn = connections.connect(
    host="127.0.0.1",
    port="19530",
    db_name="my_database"
)
ConnectParam connectParam = ConnectParam.newBuilder()
    .withDatabaseName("my_database")
    .withUri(CLUSTER_ENDPOINT)
    .withToken(TOKEN)
    .build();

MilvusServiceClient client = new MilvusServiceClient(connectParam);
const address = "http://localhost:19530";
const db_name = "my_database";

// 1. Set up a Milvus Client
client = new MilvusClient({ address, db_name });

列出資料庫

要查找 Milvus 集群中所有現有的資料庫,請使用list_database()方法:

要找到 Milvus 集群中所有現有的資料庫,請使用listDatabases()方法:

要在您的 Milvus 集群中找到所有現有資料庫,請使用listDatabases()方法:

db.list_database()

# Output
['default', 'my_database']
import io.milvus.grpc.ListDatabasesResponse;
import io.milvus.param.R;

// 2. List all databases
R<ListDatabasesResponse> listDatabasesResponse = client.listDatabases();
System.out.println(listDatabasesResponse.getData());

// status {
// }
// db_names: "default"
// db_names: "my_database"
// created_timestamp: 1716794498117757990
// created_timestamp: 1716797196479639477
res = await client.listDatabases();

console.log(res.db_names);

// [ 'default', 'my_database' ]

刪除資料庫

要丟棄資料庫,您必須先丟棄它的所有集合。否則,刪除會失敗。

要丟棄資料庫,請使用drop_database()方法:

要丟棄資料庫,請使用dropDatabase()方法:

要丟棄資料庫,請使用dropDatabase()方法:

db.drop_database("my_database")

db.list_database()

# Output
['default']
import io.milvus.param.collection.DropDatabaseParam;

DropDatabaseParam dropDatabaseParam = DropDatabaseParam.newBuilder()
    .withDatabaseName("my_database")
    .build();

response = client.dropDatabase(dropDatabaseParam);
res = await client.dropDatabase({
    db_name: "my_database",
});

在資料庫中使用 RBAC

RBAC 也涵蓋資料庫操作,並確保向前相容。權限 API (Grant / Revoke / List Grant) 中的資料庫字元有以下含義:

  • 如果 Milvus 連線或權限 API 呼叫都沒有指定db_name資料庫就是指預設資料庫。
  • 如果 Milvus 連線指定了db_name ,但之後的許可 API 呼叫沒有指定,資料庫就指 Milvus 連線中指定的資料庫名稱。
  • 如果在 Milvus 連線時呼叫許可 API,不論是否指定db_name資料庫都是指在許可 API 呼叫中指定其名稱的資料庫。

下面的程式碼片段在以下列出的區塊中共用。

from pymilvus import connections, Role

_URI = "http://localhost:19530"
_TOKEN = "root:Milvus"
_DB_NAME = "default"


def connect_to_milvus(db_name="default"):
    print(f"connect to milvus\n")
    connections.connect(
        uri=_URI,
        token=_TOKEN,
        db_name=db_name
    )
String URI = "http://localhost:19530";
String TOKEN = "root:Milvus";

public class ConnectToMilvus {
    private String _dbName = "default";

    public newBuilder() {}

    public MilvusServiceClient build() {
        ConnectParam connectParam = ConnectParam.newBuilder()
            .withUri(URI)
            .withToken(TOKEN)
            .withDatabaseName(_dbNAME)
            .build();

        return new MilvusServiceClient(connectParam);
    }

    public newBuilder withDbName(String dbName) {
        this._dbName = dbName;
        return this;
    }
}
const address = "http://localhost:19530";
const token = "root:Milvus";

function connectToMilvus(dbName = "default") {
    const client = new MilvusClient({
        address,
        token,
        dbName,
    });

    return client;
}
  • 如果 Milvus 連線或 Permission API 呼叫都沒有指定db_name資料庫會指向預設資料庫。

    _ROLE_NAME = "test_role"
    _PRIVILEGE_INSERT = "Insert"
    
    connect_to_milvus()
    role = Role(_ROLE_NAME)
    role.create()
    
    connect_to_milvus()
    role.grant("Collection", "*", _PRIVILEGE_INSERT)
    print(role.list_grants())
    print(role.list_grant("Collection", "*"))
    role.revoke("Global", "*", _PRIVILEGE_INSERT)
    
    String ROLE_NAME = "test_role";
    String PRIVILEGE_INSERT = "Insert";
    
    MilvusServiceClient client = new ConnectToMilvus().build();
    R<RpcStatus> response = client.createRole(CreateRoleParam.newBuilder()
        .withRoleName(ROLE_NAME)
        .build());
    
    if (response.getStatus() != R.Status.Success.getCode()) {
        throw new RuntimeException(response.getMessage());
    }
    
    response = client.grantRolePrivilege(GrantRolePriviledgeParam.newBuilder()
        .withRoleName(ROLE_NAME)
        .withObject("Collection")
        .withObjectName("*")
        .withPrivilege(PRIVILEGE_INSERT)
        .build());
    
    if (response.getStatus() != R.Status.Success.getCode()) {
        throw new RuntimeException(response.getMessage());
    }
    
    R<SelectGrantResponse> grants = client.selectGrantForRole(SelectGrantForRoleParam.newBuilder()
        .withRoleName(ROLE_NAME)
        .build());
    
    if (grants.getStatus() != R.Status.Success.getCode()) {
        throw new RuntimeException(grants.getMessage());
    }
    
    System.out.println(grants.getData());
    
    grants = client.selectGrantForRoleAndObject(SelectGrantForRoleAndObjectParam.newBuilder()
        .withRoleName(ROLE_NAME)
        .withObject("Collection")
        .withObjectName("*")
        .build());
    
    if (grants.getStatus() != R.Status.Success.getCode()) {
        throw new RuntimeException(grants.getMessage());
    }
    
    System.out.println(grants.getData());
    
    response = client.revokeRolePrivilege(RevokeRolePrivilegeParam.newBuilder()
        .withRoleName(ROLE_NAME)
        .withObject("Global")
        .withObjectName("*")
        .withPrivilege(PRIVILEGE_INSERT)
        .build());
    
    if (response.getStatus() != R.Status.Success.getCode()) {
        throw new RuntimeException(response.getMessage());
    }
    
    response = client.revokeRolePrivilege(RevokeRolePrivilegeParam.newBuilder()
        .withRoleName(ROLE_NAME)
        .withObject("Global")
        .withObjectName("*")
        .withPrivilege(PRIVILEGE_INSERT)
        .build());
    
    if (response.getStatus() != R.Status.Success.getCode()) {
        throw new RuntimeException(response.getMessage());
    }
    
    const ROLE_NAME = "test_role";
    const PRIVILEGE_INSERT = "Insert";
    
    const client = connectToMilvus();
    
    async function demo() {}
    await client.createRole({
      roleName: ROLE_NAME,
    });
    
    const grants = await client.listGrants({
      roleName: ROLE_NAME,
    });
    
    console.log(grants.grants);
    
    await client.revokePrivilege({
      roleName: ROLE_NAME,
      object: "Global",
      objectName: "*",
      privilege: PRIVILEGE_INSERT,
    });
    
  • 如果 Milvus 連線指定了db_name ,但之後的 Permission API 呼叫沒有指定,資料庫會指向 Milvus 連線中指定名稱的資料庫。

    # NOTE: please make sure the 'foo' db has been created
    connect_to_milvus(db_name="foo")
    
    # This role will have the insert permission of all collections under foo db,
    # excluding the insert permissions of collections under other dbs
    role.grant("Collection", "*", _PRIVILEGE_INSERT)
    print(role.list_grants())
    print(role.list_grant("Collection", "*"))
    role.revoke("Global", "*", _PRIVILEGE_INSERT)
    
    // NOTE: please make sure the 'foo' db has been created
    MilvusServiceClient client = new ConnectToMilvus().withDbName("foo").build();
    
    // This role will have the insert permission of all collections under foo db,
    // excluding the insert permissions of collections under other dbs
    R<RpcStatus> response = client.grantRolePrivilege(GrantRolePriviledgeParam.newBuilder()
        .withRoleName(ROLE_NAME)
        .withObject("Collection")
        .withObjectName("*")
        .withPrivilege(PRIVILEGE_INSERT)
        .build());
    
    if (response.getStatus() != R.Status.Success.getCode()) {
        throw new RuntimeException(response.getMessage());
    }
    
    R<SelectGrantResponse> grants = client.selectGrantForRole(SelectGrantForRoleParam.newBuilder()
        .withRoleName(ROLE_NAME)
        .build());
    
    if (grants.getStatus() != R.Status.Success.getCode()) {
        throw new RuntimeException(grants.getMessage());
    }
    
    System.out.println(grants.getData());
    
    grants = client.selectGrantForRoleAndObject(SelectGrantForRoleAndObjectParam.newBuilder()
        .withRoleName(ROLE_NAME)
        .withObject("Collection")
        .withObjectName("*")
        .build());
    
    if (grants.getStatus() != R.Status.Success.getCode()) {
        throw new RuntimeException(grants.getMessage());
    }
    
    System.out.println(grants.getData());
    
    response = client.revokeRolePrivilege(RevokeRolePrivilegeParam.newBuilder()
        .withRoleName(ROLE_NAME)
        .withObject("Global")
        .withObjectName("*")
        .withPrivilege(PRIVILEGE_INSERT)
        .build());
    
    if (response.getStatus() != R.Status.Success.getCode()) {
        throw new RuntimeException(response.getMessage());
    }
    
    const client = connectToMilvus("foo");
    
    async function demo() {}
    await client.createRole({
      roleName: ROLE_NAME,
    });
    
    const grants = await client.listGrants({
      roleName: ROLE_NAME,
    });
    
    console.log(grants.grants);
    
    await client.revokePrivilege({
      roleName: ROLE_NAME,
      object: "Global",
      objectName: "*",
      privilege: PRIVILEGE_INSERT,
    });
    
  • 如果在 Milvus 連線時呼叫 Permission API,不論是否指定db_name資料庫都是指其名稱在 Permission API 呼叫中指定的資料庫。

    # NOTE: please make sure the 'foo' db has been created
    
    db_name = "foo"
    connect_to_milvus()
    role.grant("Collection", "*", _PRIVILEGE_INSERT, db_name=db_name)
    print(role.list_grants(db_name=db_name))
    print(role.list_grant("Collection", "*", db_name=db_name))
    role.revoke("Global", "*", _PRIVILEGE_INSERT, db_name=db_name)
    
    // NOTE: please make sure the 'foo' db has been created
    
    String dbName = "foo";
    MilvusServiceClient client = new ConnectToMilvus().build();
    
    R<RpcStatus> response = client.grantRolePrivilege(GrantRolePriviledgeParam.newBuilder()
        .withRoleName(ROLE_NAME)
        .withObject("Collection")
        .withObjectName("*")
        .withPrivilege(PRIVILEGE_INSERT)
        .withDatabaseName(dbName)
        .build());
    
    if (response.getStatus() != R.Status.Success.getCode()) {
        throw new RuntimeException(response.getMessage());
    }
    
    R<SelectGrantResponse> grants = client.selectGrantForRole(SelectGrantForRoleParam.newBuilder()
        .withRoleName(ROLE_NAME)
        .withDatabaseName(dbName)
        .build());
    
    if (grants.getStatus() != R.Status.Success.getCode()) {
        throw new RuntimeException(grants.getMessage());
    }
    
    System.out.println(grants.getData());
    
    grants = client.selectGrantForRoleAndObject(SelectGrantForRoleAndObjectParam.newBuilder()
        .withRoleName(ROLE_NAME)
        .withObject("Collection")
        .withObjectName("*")
        .withDatabaseName(dbName)
        .build());
    
    if (grants.getStatus() != R.Status.Success.getCode()) {
        throw new RuntimeException(grants.getMessage());
    }
    
    System.out.println(grants.getData());
    
    response = client.revokeRolePrivilege(RevokeRolePrivilegeParam.newBuilder()
        .withRoleName(ROLE_NAME)
        .withObject("Global")
        .withObjectName("*")
        .withPrivilege(PRIVILEGE_INSERT)
        .withDatabaseName(dbName)
        .build());
    
    if (response.getStatus() != R.Status.Success.getCode()) {
        throw new RuntimeException(response.getMessage());
    }
    
    // The Node.js SDK currently cannot support this case.
    

下一步

免費嘗試托管的 Milvus

Zilliz Cloud 無縫接入,由 Milvus 提供動力,速度提升 10 倍。

開始使用
反饋

這個頁面有幫助嗎?