我在尝试运行 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"]