Home > database >  Accessing dockerized neo4j using neo4j vs py2neo
Accessing dockerized neo4j using neo4j vs py2neo

Time:02-01

I have setup neo4j to run in docker and exposed the http and bolt ports (7474, 7687).

This is the setup I used :

docker run \
    --name testneo4j \
    -p7474:7474 -p7687:7687 \
    -d \
    -v `pwd`/neo4j/data:/data \
    -v `pwd`/neo4j/logs:/logs \
    -v `pwd`/import:/var/lib/neo4j/import \
    -v `pwd`/neo4j/plugins:/plugins \
    --env NEO4J_AUTH=neo4j/XXXXXXX \

I am now trying to connect to the graph database using Python

Using the py2neo library works fine:

In [1]: from py2neo import Graph

In [2]: graph=Graph('bolt://localhost:7687',user="neo4j", password="XXXXXXX")
   ...: graph.run('MATCH(x) RETURN COUNT(x)')

 COUNT(x)
----------
        0

But when I use the neo4j module:

from neo4j import GraphDatabase, TRUST_ALL_CERTIFICATES
trust=TRUST_ALL_CERTIFICATES

neo4j_user="neo4j"
neo4j_passwd="XXXXXXX"
uri="bolt://localhost:7687"
driver = GraphDatabase.driver(uri, 
                       auth=(neo4j_user, neo4j_passwd),
                       encrypted=False, trust=trust)

I get this error:


File ~/local/anaconda3/lib/python3.8/site-packages/neo4j/__init__.py:120, in GraphDatabase.driver(cls, uri, **config)
    114 @classmethod
    115 def driver(cls, uri, **config):
    116     """ Create a :class:`.Driver` object. Calling this method provides
    117     identical functionality to constructing a :class:`.Driver` or
    118     :class:`.Driver` subclass instance directly.
    119     """
--> 120     return Driver(uri, **config)

File ~/local/anaconda3/lib/python3.8/site-packages/neo4j/__init__.py:161, in Driver.__new__(cls, uri, **config)
    159 for subclass in Driver.__subclasses__():
    160     if parsed_scheme in subclass.uri_schemes:
--> 161         return subclass(uri, **config)
    162 raise ValueError("URI scheme %r not supported" % parsed.scheme)

File ~/local/anaconda3/lib/python3.8/site-packages/neo4j/__init__.py:235, in DirectDriver.__new__(cls, uri, **config)
    232     return connect(address, **dict(config, **kwargs))
    234 pool = ConnectionPool(connector, instance.address, **config)
--> 235 pool.release(pool.acquire())
    236 instance._pool = pool
    237 instance._max_retry_time = config.get("max_retry_time", default_config["max_retry_time"])

File ~/local/anaconda3/lib/python3.8/site-packages/neobolt/direct.py:715, in ConnectionPool.acquire(self, access_mode)
    714 def acquire(self, access_mode=None):
--> 715     return self.acquire_direct(self.address)

File ~/local/anaconda3/lib/python3.8/site-packages/neobolt/direct.py:608, in AbstractConnectionPool.acquire_direct(self, address)
    606 if can_create_new_connection:
    607     try:
--> 608         connection = self.connector(address, error_handler=self.connection_error_handler)
    609     except ServiceUnavailable:
    610         self.remove(address)

File ~/local/anaconda3/lib/python3.8/site-packages/neo4j/__init__.py:232, in DirectDriver.__new__.<locals>.connector(address, **kwargs)
    231 def connector(address, **kwargs):
--> 232     return connect(address, **dict(config, **kwargs))

File ~/local/anaconda3/lib/python3.8/site-packages/neobolt/direct.py:972, in connect(address, **config)
    970     raise ServiceUnavailable("Failed to resolve addresses for %s" % address)
    971 else:
--> 972     raise last_error

File ~/local/anaconda3/lib/python3.8/site-packages/neobolt/direct.py:964, in connect(address, **config)
    962     s = _connect(resolved_address, **config)
    963     s, der_encoded_server_certificate = _secure(s, host, security_plan.ssl_context, **config)
--> 964     connection = _handshake(s, address, der_encoded_server_certificate, **config)
    965 except Exception as error:
    966     last_error = error

File ~/local/anaconda3/lib/python3.8/site-packages/neobolt/direct.py:920, in _handshake(s, resolved_address, der_encoded_server_certificate, **config)
    918 if agreed_version == 0:
    919     log_debug("[#X]  C: <CLOSE>", local_port)
--> 920     s.shutdown(SHUT_RDWR)
    921     s.close()
    922 elif agreed_version in (1, 2):

OSError: [Errno 57] Socket is not connected


Does anyone know why the former works but the latter doesn't?

CodePudding user response:

You're not using the same password.

CodePudding user response:

It turns out that the problem was I was using an older version of the neo4j library (1.7.6). I did a pip install neo4j --upgrade to version 5.5 and I am no longer getting any errors.

  • Related