VS Code连接Neo4j数据库报错:Unable to retrieve routing information
Neo4j连接报错「无法获取路由信息」的解决办法
我在VS Code中使用Neo4j官方模板代码创建连接类,尝试连接Neo4j数据库并执行查询时,遇到如下报错:
无法获取路由信息
查询失败:无法获取路由信息
使用的连接类代码:
from neo4j import GraphDatabase import logging from neo4j.exceptions import ServiceUnavailable class Neo4jConnection: def __init__(self, uri, user, pwd): self.__uri = uri self.__user = user self.__pwd = pwd self.__driver = None try: self.__driver = GraphDatabase.driver(self.__uri, auth=(self.__user, self.__pwd)) except Exception as e: print("Failed to create the driver:", e) def close(self): if self.__driver is not None: self.__driver.close() def query(self, query, db=None): assert self.__driver is not None, "Driver not initialized!" session = None response = None try: session = self.__driver.session(database=db) if db is not None else self.__driver.session() response = list(session.run(query)) except Exception as e: print("Query failed:", e) finally: if session is not None: session.close() return response
连接与查询代码:
# 初始化连接 conn = Neo4jConnection(uri="neo4j+s://7022d007.databases.neo4j.io", user="neo4j", pwd="****") # 执行查询 query_string = ''' CALL db.schema.visualization() ''' conn.query(query_string, db='MARA')
以下是针对性的解决方法:
核对URI协议与端点正确性
确认使用的URI协议匹配数据库类型:Neo4j Aura实例需使用neo4j+s://或neo4j+ssc://,旧版Aura可能需要bolt+s://。同时检查URI中的实例ID(7022d007)是否完全正确,避免拼写错误。验证目标数据库的存在性与权限
确认指定的MARA数据库确实存在于你的Neo4j实例中,且用户neo4j拥有访问权限。可通过Neo4j Browser连接实例后,执行SHOW DATABASES命令查看所有可用数据库。排查网络连通性问题
确保VS Code所在网络能访问Neo4j的7687端口,未被本地防火墙、代理或企业网络策略拦截。可在终端执行telnet 7022d007.databases.neo4j.io 7687测试连通性,若无法连接需调整网络设置。更新Neo4j Python驱动版本
旧版驱动可能与新版Neo4j实例存在兼容性问题,执行pip install --upgrade neo4j升级到最新稳定版驱动。优化连接类的连接验证逻辑
当前连接类仅在创建驱动时捕获异常,建议添加连接验证步骤,提前检测连接问题。修改__init__方法如下:def __init__(self, uri, user, pwd): self.__uri = uri self.__user = user self.__pwd = pwd self.__driver = None try: self.__driver = GraphDatabase.driver(self.__uri, auth=(self.__user, self.__pwd)) # 添加连接验证,提前检测连接问题 self.__driver.verify_connectivity() print("连接成功") except Exception as e: print("创建驱动失败:", e)
内容的提问来源于stack exchange,提问作者Owen
相关产品推荐
相关产品推荐

