3

我在尝试运行 neo4j 代码时遇到了 neo4j 连接问题;在 docker 外部也可以正常工作,但在 docker 内部运行时无法与 neo4j 数据库建立连接。

错误信息:

    Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/neobolt/direct.py", line 835, in _connect
    s.connect(resolved_address)
OSError: [Errno 99] Cannot assign requested address

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    driver = GraphDatabase.driver(uri, auth=("neo4j", "admin"))
  File "/usr/local/lib/python3.7/site-packages/neo4j/__init__.py", line 116, in driver
    return Driver(uri, **config)
  File "/usr/local/lib/python3.7/site-packages/neo4j/__init__.py", line 157, in __new__
    return subclass(uri, **config)
  File "/usr/local/lib/python3.7/site-packages/neo4j/__init__.py", line 231, in __new__
    pool.release(pool.acquire())
  File "/usr/local/lib/python3.7/site-packages/neobolt/direct.py", line 719, in acquire
    return self.acquire_direct(self.address)
  File "/usr/local/lib/python3.7/site-packages/neobolt/direct.py", line 612, in acquire_direct
    connection = self.connector(address, error_handler=self.connection_error_handler)
  File "/usr/local/lib/python3.7/site-packages/neo4j/__init__.py", line 228, in connector
    return connect(address, **dict(config, **kwargs))
  File "/usr/local/lib/python3.7/site-packages/neobolt/direct.py", line 976, in connect
    raise last_error
  File "/usr/local/lib/python3.7/site-packages/neobolt/direct.py", line 966, in connect
    s = _connect(resolved_address, **config)
  File "/usr/local/lib/python3.7/site-packages/neobolt/direct.py", line 847, in _connect
    raise ServiceUnavailable("Failed to establish connection to {!r} (reason {})".format(resolved_address, error))
neobolt.exceptions.ServiceUnavailable: Failed to establish connection to ('::1', 11010, 0, 0) (reason [Errno 99] Cannot assign requested address)

我使用的代码如下:

from neo4j import GraphDatabase
uri = "bolt://localhost:11010/"
driver = GraphDatabase.driver(uri, auth=("neo4j", "admin"))
with driver.session() as session:
    result = session.run("MATCH (n) RETURN count(n)")
    print(result)
    session.close()

我的螺栓端口是代码中的 11010。我在这里错过了什么吗?我尝试了以下事情,但没有运气
1)使用 127.0.0.1 而不是 localhost
2)关闭会话

我的docker文件如下:

FROM python:3
ADD test.py /
RUN pip install pandas
RUN pip install pymysql
RUN pip install sqlalchemy
RUN pip install neo4j
CMD ["python","test.py"]
4

2 回答 2

2

如果您在容器内运行 python,请使用:

    GraphDatabase.driver(host="<contaniner>", auth=basic_auth("neo4j", "myPass"))
于 2019-12-11T19:27:02.580 回答
2

从评论中,我假设 neo4j 数据库正在其他容器中运行。

您不能使用 URL 从一个容器连接到另一个容器localhost

localhost 总是指当前容器的本地主机,而不是另一个容器称为 B。

所以可能的替代方案和解决方案

  • 如果两者都在两个不同的容器中运行,则使用 Docker-compose,您将能够像这样从容器名称访问 new4j 数据库neo4j:port
  • 如果 new4j 在主机上运行,​​那么您需要使用python 容器内的主机ip 访问 new4j。
  • 如果你不想使用 docker-compose,你可以使用 lagacylinking
 docker run -d -P --name web --link newdb:db training/webapp python app.py

现在你从 python 容器内部引用它,就像db一个主机一样。

于 2019-09-19T07:23:10.857 回答