Python连接Neo4j触发BoltSecurityError[SSLCertVerificationError]问题咨询
问题场景
编写的Python脚本无法连接Neo4j Aura实例,核心错误为SSL证书验证失败,但使用相同URI和凭据的Neo4j Desktop可正常连接。
用户代码
from neo4j import GraphDatabase from neo4j.debug import watch watch("neo4j") uri = "neo4j+s://XXX.databases.neo4j.io:7687" driver = GraphDatabase.driver(uri, auth=("neo4j", "password"))
核心错误日志
BoltSecurityError: [SSLCertVerificationError] Connection Failed. Please ensure that your database is listening on the correct host and port and that you have enabled encryption if required. Note that the default encryption setting has changed in Neo4j 4.0. See the docs for more information. Failed to establish encrypted connection. (code 1: Operation not permitted)
可能的故障原因及解决方法
Python环境CA证书配置差异
Neo4j Desktop内置了完整的信任证书库,而你的Python运行环境可能未信任Neo4j Aura使用的SSL证书。
解决:从Neo4j Desktop导出对应实例的SSL证书,在初始化driver时指定证书路径:driver = GraphDatabase.driver(uri, auth=("neo4j", "password"), cert_file="/path/to/exported/cert.crt")或更新系统级CA证书库,确保包含Neo4j Aura证书的信任链。
网络权限限制
系统防火墙、杀毒软件或代理规则可能拦截了Python程序的SSL连接请求,而Neo4j Desktop拥有更高的网络权限。
解决:检查防火墙规则,允许Python进程访问7687端口;若使用代理,确保Python环境配置了正确的代理参数。Neo4j Python驱动版本兼容问题
旧版本的Neo4j驱动可能对新的SSL验证逻辑支持不足,而Neo4j Desktop使用的是适配性更好的驱动版本。
解决:升级驱动到最新版本:pip install --upgrade neo4j
内容的提问来源于stack exchange,提问作者chris

