跳到主要内容
版本:v2.4.x

管理数据库

与传统数据库引擎类似,您也可以在 Milvus 中创建数据库,并为某些用户分配管理这些数据库的权限。然后,这些用户就有权管理数据库中的 Collection。一个 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")

上述代码片段连接到默认数据库,并创建一个名为my_database 的新数据库。

使用数据库

Milvus 集群随附一个默认数据库,名为 "default"。除非另有说明,否则将在默认数据库中创建 Collection。

更改默认数据库的步骤如下:

db.using_database("my_database")

您还可以设置一个数据库,以便在连接到 Milvus 集群时使用,方法如下:

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

列出数据库

要查找 Milvus 集群中的所有现有数据库,请使用list_database()方法:

要查找 Milvus 集群中的所有现有数据库,请使用listDatabases()方法:

要查找 Milvus 集群中的所有现有数据库,请使用listDatabases()方法:

db.list_database()

# Output
['default', 'my_database']

删除数据库

要丢弃一个数据库,必须先丢弃它的所有 Collection。否则,删除将失败。

要丢弃数据库,请使用drop_database()方法:

要丢弃数据库,请使用dropDatabase()方法:

要停用数据库,请使用dropDatabase()方法:

db.drop_database("my_database")

db.list_database()

# Output
['default']

在数据库中使用 RBAC

RBAC 还涵盖数据库操作,并确保向前兼容。权限 API(授予/撤销/列表授予)中的数据库一词有以下含义:

  • 如果 Milvus 连接和权限 API 调用都没有指定db_name数据库指的是默认数据库。
  • 如果 Milvus 连接指定了db_name ,但之后的 Permission 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
)
  • 如果 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)
  • 如果 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)
  • 如果在 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)

下一步