31 lines
966 B
Python
31 lines
966 B
Python
# 通过pymilvus sdk来操作数据库
|
|
|
|
from pymilvus import connections, utility
|
|
|
|
|
|
def test_connection(
|
|
host: str = "49.233.56.55", port: str = "19530", db_name: str = "default"
|
|
) -> bool:
|
|
try:
|
|
# 建立与milvus的连接
|
|
connections.connect(
|
|
"default", host=host, port=port, timeout=10, db_name=db_name
|
|
)
|
|
# 打印当前连接到的数据库名称
|
|
print(f"当前连接到的数据库为: {db_name}")
|
|
# 获取搜友集合并打印
|
|
collections = utility.list_collections()
|
|
print(f"数据库{db_name}下的所有数据集合{collections}")
|
|
# 连接成功提示
|
|
print("连接成功:已成功连接到 Milvus 服务器")
|
|
return True
|
|
|
|
except Exception as exc:
|
|
print(f"连接失败,无法连接到Milvus服务器的数据库{db_name}", exc)
|
|
return False
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if not test_connection():
|
|
raise SystemExit(1)
|