4
votes

I'm trying to run neo4j in a docker container and connect to it from a python script not running in a container, but I'm getting AuthError.

I'm following the instructions from here

Start neo4j

docker run \
    --publish=7474:7474 --publish=7687:7687 \
    --volume=$HOME/neo4j/data:/data \
    --volume=$HOME/neo4j/logs:/logs \
    neo4j:3.5

I've also done this with these added insturctions "By default Neo4j requires authentication and requires you to login with neo4j/neo4j at the first connection and set a new password. You can set the password for the Docker container directly by specifying --env NEO4J_AUTH=neo4j/ in your run directive."

Start 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

After doing either of these, I am able to connect via the web interface at http://localhost:7474/

Now I want to connect with a python script as described here which has me run this code (note I've changed the password to match the NEO4J_AUTH setting from the docker command).

from neo4j import 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()

When I run this code I get this error.

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}
1

1 Answers

0
votes

Your error does indicate a bit different code:

Traceback (most recent call last):
File "/Users/j/projects/neotest/neo.py", line 25, in <module>
    greeter = HelloWorldExample("bolt://localhost:7687", "neo4j", "neo4j")

Here it shows that you initiated the HelloWorldClass with password "neo4j" instead of "neo"