我在尝试运行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()
我的Bolt端口号为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"]