Managing Databases
Similar to traditional database engines, you can create databases in Milvus and assign permissions to specific users to manage them. These users will then have the authority to manage collections within the databases. The Milvus cluster supports up to 64 databases.
Creating a Database
To create a database, you first need to connect to the Milvus cluster and prepare a name:
from pymilvus import connections, db
conn = connections.connect(host="127.0.0.1", port=19530)
database = db.create_database("book")
Using a Database
The Milvus cluster comes with a default database named "default". Unless specified otherwise, collections will be created in the default database.
To change the default database, follow these steps:
db.using_database("book")
You can also set the database to use when connecting to the Milvus cluster, as shown below:
conn = connections.connect(
host="127.0.0.1",
port="19530",
db_name="default"
)
Listing Databases
To retrieve all existing databases in the Milvus cluster, follow these steps:
db.list_database()
['default', 'book']
Deleting a Database
To delete a database, you must first delete all its collections. Otherwise, the deletion will fail.
db.drop_database("book")
db.list_database()
['default']
Using RBAC in the Database
RBAC also covers database operations and ensures forward compatibility. In the Permission API (Grant / Revoke / List Grant), the term database has the following meanings:
- If both the Milvus connection and the Permission API call do not specify
db_name
, database refers to the default database. - If the Milvus connection specifies
db_name
, but subsequent Permission API calls do not, database refers to the database name specified in the Milvus connection. - If a Permission API call is made on the Milvus connection, regardless of whether
db_name
is specified, database refers to the database name specified in the Permission API call.
The following code snippet is shared between the blocks below.
from pymilvus import connections, Role
_HOST = '127.0.0.1'
_PORT = '19530'
_ROOT = "root"
_ROOT_PASSWORD = "Milvus"
_ROLE_NAME = "test_role"
_PRIVILEGE_INSERT = "insert"
def connect_to_milvus(db_name="default"):
print(f"Connecting to Milvus\n")
connections.connect(host=_HOST, port=_PORT, user=_ROOT, password=_ROOT_PASSWORD, db_name=db_name)
- If both the Milvus connection and the Permission API call do not specify
db_name
, database refers to the default database.
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)
- If the Milvus connection specifies
db_name
, but subsequent Permission API calls do not, database refers to the database name specified in the Milvus connection.
connect_to_milvus(db_name="foo")
role.grant("Collection", "*", _PRIVILEGE_INSERT)
print(role.list_grants())
print(role.list_grant("Collection", "*"))
role.revoke("Global", "*", _PRIVILEGE_INSERT)
- If a Permission API call is made on the Milvus connection, regardless of whether
db_name
is specified, database refers to the database name specified in the Permission API call.
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)