知识库

如何解决 Python Bolt 驱动程序执行时报错“("Failed to establish connection to {!r}".format(address))”

以 Python 和最新 Bolt 驱动程序 1.2 为例。

以下是示例代码

from neo4j.v1 import GraphDatabase
uri = "bolt://localhost:7687"
driver = GraphDatabase.driver(uri, auth=("neo4j", "Password"))

运行时出现以下错误

Traceback (most recent call last):
  File "/Users/rk/Documents/Work/Python-Bolt/boltTest.py", line 3, in <module>
    driver = GraphDatabase.driver(uri, auth=("neo4j", "Password"))
  File "/Library/Python/2.7/site-packages/neo4j/v1/api.py", line 112, in driver
    return driver_class(uri, **config)
  File "/Library/Python/2.7/site-packages/neo4j/v1/direct.py", line 56, in __init__
    pool.acquire()
  File "/Library/Python/2.7/site-packages/neo4j/v1/direct.py", line 37, in acquire
    return self.acquire_direct(resolved_addresses[0])
  File "/Library/Python/2.7/site-packages/neo4j/bolt/connection.py", line 386, in acquire_direct
    connection = self.connector(address)
  File "/Library/Python/2.7/site-packages/neo4j/v1/direct.py", line 55, in <lambda>
    pool = DirectConnectionPool(lambda a: connect(a, security_plan.ssl_context, **config), self.address)
  File "/Library/Python/2.7/site-packages/neo4j/bolt/connection.py", line 457, in connect
    raise ServiceUnavailable("Failed to establish connection to {!r}".format(address))
neo4j.exceptions.ServiceUnavailable: Failed to establish connection to ('::1', 7687, 0, 0)

有两种方法可以解决此问题

  • 要么将 localhost 更改为 127.0.0.1,然后使用当前设置运行它。如下所示

    from neo4j.v1 import GraphDatabase
    uri = "bolt://127.0.0.1:7687"
    driver = GraphDatabase.driver(uri, auth=("neo4j", "Password"))
  • 第二种方法是在 conf/neo4j.conf 文件中设置以下参数,如下所示

    dbms.connector.bolt.listen_address=0.0.0.0:7687

    我们将其定义为从任何地址(即 0.0.0.0)连接。重新启动数据库并运行代码,如下所示

    from neo4j.v1 import GraphDatabase
    uri = "bolt://localhost:7687"
    driver = GraphDatabase.driver(uri, auth=("neo4j", "Password"))