当前位置: 首页 > 工具软件 > bolt-python > 使用案例 >

python连接neo4j-4.0.1

沈博延
2023-12-01

python连接neo4j数据库:

 

https://neo4j.com/docs/api/python-driver/current/#example-application

 

系统版本:window10(推荐linux系统来安装来减少一些错误的概率)

neo4j数据库版本:neo4j  version-4.0.1             python版本:3.7.6

 

 

其他版本匹配:neo4j图数据库版本==3.5          python==3.7           neo4j-driver==1.7.6

 

 

from neo4j import GraphDatabase, basic_auth  #安装pip install neo4j-driver==4.0.0a4

 

 

错误1

        neo4jAddress = '''bolt://127.0.0.1:7474'''      #本地地址
        neo4jUser = "neo4j"                             #用户名
        neo4jPassword = "123456"                        #密码


        # 加载中心词库
        self.lexiconDriver = GraphDatabase.driver(neo4jAddress, auth=basic_auth(neo4jUser, neo4jPassword))
        self.cqGenerator = ChoiceQuestionGenerator(driver=self.lexiconDriver)

 

python使用neo4j库连接neo4j图数据库时出现[SSL: WRONG_VERSION_NUMBER]
我的neo4j图数据库是较新版本:4.0.1,在使用neo4j官方驱动时,发生neobolt.exceptions.SecurityError: Failed to establish secure connection to ‘[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1076)’

原因:版本错误
在安装neo4j驱动时直接使用pip install neo4j,安装的是neo4j-1.7.6版本,然而4.0.4需要neo4j 4.0版本的驱动

 

解决办法:
先pip uninstall neo4j
然后pip install neo4j-driver==4.0.0a4

 

 

错误2

继续执行上述代码:

 

        neo4jAddress = '''bolt://127.0.0.1:7474'''      #本地地址
        neo4jUser = "neo4j"                             #用户名
        neo4jPassword = "123456"                        #密码


        # 加载中心词库
        self.lexiconDriver = GraphDatabase.driver(neo4jAddress, auth=basic_auth(neo4jUser, neo4jPassword))
        self.cqGenerator = ChoiceQuestionGenerator(driver=self.lexiconDriver)

        neo4jAddress = '''bolt://127.0.0.1:7474'''     #本地地址,应该是7687
        neo4jUser = "neo4j"                                    #用户名
        neo4jPassword = "123456"                        #密码

 

然后又会出现如下错误:这是因为上面的本地地址配置错误了。

 

Traceback (most recent call last):
  File "", line 184, in <module>
    app = FlaskApp(__name__)
  File "main.py", line 98, in __init__
    self.itemGenerator = AutoItemGenerator()
  File "main.py", line 66, in __init__
    self.lexiconDriver = GraphDatabase.driver(neo4jAddress, auth=basic_auth(neo4jUser, neo4jPassword))
  File "__init__.py", line 197, in driver
    return cls.bolt_driver(parsed.netloc, auth=auth, **config)
  File "C:\myappinstall\anaconda3\lib\site-packages\neo4j\__init__.py", line 210, in bolt_driver
    return BoltDriver.open(target, auth=auth, **config)
  File "C:\myappinstall\anaconda3\lib\site-packages\neo4j\__init__.py", line 380, in open
    pool = BoltPool.open(address, auth=auth, pool_config=pool_config, workspace_config=default_workspace_config)
  File "C:\myappinstall\anaconda3\lib\site-packages\neo4j\io\__init__.py", line 497, in open
    seeds = [pool.acquire() for _ in range(pool_config.init_size)]
  File "C:\myappinstall\anaconda3\lib\site-packages\neo4j\io\__init__.py", line 497, in <listcomp>
    seeds = [pool.acquire() for _ in range(pool_config.init_size)]
  File "C:\myappinstall\anaconda3\lib\site-packages\neo4j\io\__init__.py", line 510, in acquire
    return self._acquire(self.address, timeout)
  File "C:\myappinstall\anaconda3\lib\site-packages\neo4j\io\__init__.py", line 391, in _acquire
    connection = self.opener(address, timeout)
  File "C:\myappinstall\anaconda3\lib\site-packages\neo4j\io\__init__.py", line 494, in opener
    return Bolt.open(addr, auth=auth, timeout=timeout, **pool_config)
  File "C:\myappinstall\anaconda3\lib\site-packages\neo4j\io\__init__.py", line 191, in open
    keep_alive=pool_config.keep_alive,
  File "C:\myappinstall\anaconda3\lib\site-packages\neo4j\io\__init__.py", line 1013, in connect
    raise last_error
  File "C:\myappinstall\anaconda3\lib\site-packages\neo4j\io\__init__.py", line 1005, in connect
    return _handshake(s, address)
  File "C:\myappinstall\anaconda3\lib\site-packages\neo4j\io\__init__.py", line 983, in _handshake
    "(looks like HTTP)".format(resolved_address))

neo4j.exceptions.ServiceUnavailable: Cannot to connect to Bolt service on IPv4Address(('127.0.0.1', 7474)) (looks like HTTP)

 

解决以上错误: 需要把本地地址端口改为7687

        neo4jAddress = '''bolt://127.0.0.1:7687'''      #本地地址
        neo4jUser = "neo4j"                             #用户名
        neo4jPassword = "123456"                        #密码


        # 加载中心词库
        self.lexiconDriver = GraphDatabase.driver(neo4jAddress, auth=basic_auth(neo4jUser, neo4jPassword))
        self.cqGenerator = ChoiceQuestionGenerator(driver=self.lexiconDriver)

 

再次运行之后程序就可以正常运行了。

 

 

 

 

 

 类似资料: