ForcePilot/test/test_neo4j.py

38 lines
1.2 KiB
Python
Raw Normal View History

2024-09-06 12:54:17 +08:00
from neo4j import GraphDatabase
from neo4j.exceptions import AuthError, ServiceUnavailable
2024-09-06 12:54:17 +08:00
2024-09-09 17:07:03 +08:00
# sudo ln -s /snap/core22/1586/usr/sbin/iptables /usr/sbin/iptables
2024-09-06 12:54:17 +08:00
def check_neo4j_status(uri="bolt://localhost:7687", username="neo4j", password="0123456789"):
"""
检查 Neo4j 数据库是否可以连接并正常工作
2024-09-09 17:07:03 +08:00
2024-09-06 12:54:17 +08:00
参数:
uri (str): Neo4j URI默认为 "bolt://localhost:7687"
username (str): 数据库用户名默认为 "neo4j"
password (str): 数据库密码默认为 "0123456789"
2024-09-09 17:07:03 +08:00
2024-09-06 12:54:17 +08:00
返回:
str: "OK" 表示连接成功"UNAVAILABLE" 表示服务不可用"AUTH_FAILED" 表示认证失败
"""
try:
driver = GraphDatabase.driver(uri, auth=(username, password))
with driver.session() as session:
# 简单的查询来测试连接
result = session.run("RETURN 1")
if result.single()[0] == 1:
return "OK"
except ServiceUnavailable:
return "UNAVAILABLE"
except AuthError:
return "AUTH_FAILED"
finally:
# 确保关闭驱动
driver.close()
2024-09-06 12:54:17 +08:00
# 测试函数
status = check_neo4j_status()
print(f"Neo4j status: {status}")