问题描述
我正在尝试在docker容器中运行neo4j,并通过未在容器中运行的python脚本连接到它,但是我遇到了AuthError。
我正在按照here
的说明进行操作启动neo4j
docker run \
--publish=7474:7474 --publish=7687:7687 \
--volume=$HOME/neo4j/data:/data \
--volume=$HOME/neo4j/logs:/logs \
neo4j:3.5
我还通过添加了这些指示来完成此操作:“默认情况下,Neo4j需要身份验证,并要求您在第一个连接时使用neo4j / neo4j登录并设置新密码。您可以通过指定来直接设置Docker容器的密码--env NEO4J_AUTH = neo4j /在您的运行指令中。”
启动neo4j
docker run \
--publish=7474:7474 --publish=7687:7687 \
--volume=$HOME/neo4j/data:/data \
--volume=$HOME/neo4j/logs:/logs \
--env NEO4J_AUTH=neo4j/neo \
neo4j:3.5
完成上述任一操作后,我可以通过Web界面(位于http:// localhost:7474 /
)进行连接现在,我要按照here所述连接python脚本,让我运行此代码(请注意,我已更改密码,以匹配docker命令中的NEO4J_AUTH设置)。
从neo4j导入GraphDatabase
class HelloWorldExample:
def __init__(self,uri,user,password):
self.driver = GraphDatabase.driver(uri,auth=(user,password))
def close(self):
self.driver.close()
def print_greeting(self,message):
with self.driver.session() as session:
greeting = session.write_transaction(self._create_and_return_greeting,message)
print(greeting)
@staticmethod
def _create_and_return_greeting(tx,message):
result = tx.run("CREATE (a:Greeting) "
"SET a.message = $message "
"RETURN a.message + ',from node ' + id(a)",message=message)
return result.single()[0]
if __name__ == "__main__":
greeter = HelloWorldExample("bolt://localhost:7687","neo4j","neo")
greeter.print_greeting("hello,world")
greeter.close()
Traceback (most recent call last):
File "/Users/j/projects/neotest/neo.py",line 25,in <module>
greeter = HelloWorldExample("bolt://localhost:7687","neo4j")
File "/Users/j/projects/neotest/neo.py",line 6,in __init__
self.driver = GraphDatabase.driver(uri,password))
File "/usr/local/lib/python3.7/site-packages/neo4j/__init__.py",line 183,in driver
return cls.bolt_driver(parsed.netloc,auth=auth,**config)
File "/usr/local/lib/python3.7/site-packages/neo4j/__init__.py",line 196,in bolt_driver
return BoltDriver.open(target,line 359,in open
pool = BoltPool.open(address,pool_config=pool_config,workspace_config=default_workspace_config)
File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py",line 531,in open
seeds = [pool.acquire() for _ in range(pool_config.init_size)]
File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py",in <listcomp>
seeds = [pool.acquire() for _ in range(pool_config.init_size)]
File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py",line 545,in acquire
return self._acquire(self.address,timeout)
File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py",line 409,in _acquire
connection = self.opener(address,line 528,in opener
return Bolt.open(addr,timeout=timeout,routing_context=routing_context,**pool_config)
File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py",line 227,in open
raise error
File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py",line 222,in open
connection.hello()
File "/usr/local/lib/python3.7/site-packages/neo4j/io/_bolt3.py",line 148,in hello
self.fetch_all()
File "/usr/local/lib/python3.7/site-packages/neo4j/io/_bolt3.py",line 393,in fetch_all
detail_delta,summary_delta = self.fetch_message()
File "/usr/local/lib/python3.7/site-packages/neo4j/io/_bolt3.py",line 339,in fetch_message
response.on_failure(summary_Metadata or {})
File "/usr/local/lib/python3.7/site-packages/neo4j/io/_bolt3.py",line 544,in on_failure
raise AuthError(message)
neo4j.exceptions.AuthError: {code: None} {message: None}
解决方法
您的错误确实表明代码有所不同:
Traceback (most recent call last):
File "/Users/j/projects/neotest/neo.py",line 25,in <module>
greeter = HelloWorldExample("bolt://localhost:7687","neo4j","neo4j")
此处显示您使用密码“ neo4j”而不是“ neo”启动了HelloWorldClass