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(授予/撤销/列表授予)中的数据库一词有以下含义:

  • 如果 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 连接或权限 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.
    

下一步

翻译自DeepLogo

反馈

此页对您是否有帮助?