【问题标题】:AuthError trying to connect to neo4j from pythonAuthError 试图从 python 连接到 neo4j
【发布时间】:2020-08-22 19:27:53
【问题描述】:

我正在尝试在 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

完成上述任一操作后,我就可以通过位于 http://localhost:7474/ 的 Web 界面进行连接

现在我想连接一个 Python 脚本,如 here 所述,它让我运行此代码(注意我已更改密码以匹配 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", "neo4j")
File "/Users/j/projects/neotest/neo.py", line 6, in __init__
    self.driver = GraphDatabase.driver(uri, auth=(user, 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, auth=auth, **config)
File "/usr/local/lib/python3.7/site-packages/neo4j/__init__.py", line 359, in open
    pool = BoltPool.open(address, auth=auth, 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", line 531, 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, timeout)
File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 528, in opener
    return Bolt.open(addr, auth=auth, 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}

【问题讨论】:

    标签: python neo4j


    【解决方案1】:

    您的错误确实表明代码有点不同:

    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

    【讨论】:

    • 我用不同的配置运行它,堆栈跟踪来自不同的尝试。当密码为 neo 时,我得到同样的错误。
    • 您是否尝试连接到远程计算机?
    • 没有远程机器。 Neo4j 在我笔记本电脑的容器中运行,python 脚本在我的笔记本电脑上运行(不是在容器中)
    猜你喜欢
    • 2019-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多