Not able to connect from python

Hi, I am running neo4j in kubernetes. I expose it via nginx ingress
one url for port 7474 - neo4j-url:443
one url for port 7687 - neo4bolt-url:443

everything was working fine I am able to open neo4j-url/browser and connect to neo4j+s://neo4bolt-url:443

But I was trying to connect from my local machine via python script

from neo4j import GraphDatabase


driver = GraphDatabase.driver("neo4j+s://neo4bolt-url:443", auth=("neo4j","password"))
session = driver.session()

query = """ MATCH (ee:Person) WHERE ee.from  = "Sweden" RETURN ee.name as name """

results = session.run(query)
print(results["name"])

I am getting this error

Unable to retrieve routing information
Traceback (most recent call last):
  File "db.py", line 14, in <module>
    results = session.run(query)
  File "C:\anaconda\lib\site-packages\neo4j\work\simple.py", line 221, in run
    self._connect(self._config.default_access_mode, database=self._config.database)
  File "C:\anaconda\lib\site-packages\neo4j\work\simple.py", line 122, in _connect
    bookmarks=self._bookmarks
  File "C:\anaconda\lib\site-packages\neo4j\io\__init__.py", line 1118, in acquire
    bookmarks=bookmarks
  File "C:\anaconda\lib\site-packages\neo4j\io\__init__.py", line 1087, in _select_address
    access_mode=access_mode, database=database, bookmarks=bookmarks
  File "C:\anaconda\lib\site-packages\neo4j\io\__init__.py", line 1069, in ensure_routing_table_is_fresh
    self.update_routing_table(database=database, bookmarks=bookmarks)
  File "C:\anaconda\lib\site-packages\neo4j\io\__init__.py", line 1041, in update_routing_table
    raise ServiceUnavailable("Unable to retrieve routing information")
neo4j.exceptions.ServiceUnavailable: Unable to retrieve routing information

Not sure what is the problem, any tip ?
Thank you

Where is the python app deployed? Is it dockerized and running in the same namespace as the neo4j deployment? If not -- this probably won't work as you've constructed it.

There are some documentation dealing with this situation you can find here: External Exposure of Neo4j Clusters - Neo4j-Helm User Guide

Note, this area of Neo4j is undergoing some change, and Neo4j 4.3 will obviate the guidance at the link above (the picture will become simpler as of Neo4j 4.3). If you're using 4.0 <= Neo4j <= 4.2 though, you should have a look at the page above.

If your python app is dockerized and running in k8s, you should use the discovery address published by the helm install process, and not the simple local DNS name ("neo4bolt-url") as you are doing now. Neo4j should not be fronted by nginx ingress when you're using <= 4.2. This will not work because of the Neo4j smart-client routing protocol.

For full details on how smart client routing works and why you are seeing the error you are seeing, you can consult this page: Querying Neo4j Clusters. How Neo4j clusters and smart query… | by David Allen | Neo4j Developer Blog | Medium

Leveraging Neo4J 5.7.0, I am running into a similar issue.

I have Neo4J Running in a docker container behind a Traefik reverse proxy in which Traefik is routing https://neo4j.localtest.me:443 to neo4j-container:7687.

When I try to access the address securely in python: neo4j+s://neo4j.localtest.me:7687

I am getting the following error:

Unable to retrieve routing information
Traceback (most recent call last):
  File "/Users/kgamble/neo4j-docker/example.py", line 14, in <module>
    driver.verify_connectivity()
  File "/Users/kgamble/.pyenv/versions/3.10.9/lib/python3.10/site-packages/neo4j/_sync/driver.py", line 963, in verify_connectivity
    self._get_server_info()
  File "/Users/kgamble/.pyenv/versions/3.10.9/lib/python3.10/site-packages/neo4j/_sync/driver.py", line 1177, in _get_server_info
    return session._get_server_info()
  File "/Users/kgamble/.pyenv/versions/3.10.9/lib/python3.10/site-packages/neo4j/_sync/work/session.py", line 177, in _get_server_info
    self._connect(READ_ACCESS, liveness_check_timeout=0)
  File "/Users/kgamble/.pyenv/versions/3.10.9/lib/python3.10/site-packages/neo4j/_sync/work/session.py", line 135, in _connect
    super()._connect(
  File "/Users/kgamble/.pyenv/versions/3.10.9/lib/python3.10/site-packages/neo4j/_sync/work/workspace.py", line 161, in _connect
    self._pool.update_routing_table(
  File "/Users/kgamble/.pyenv/versions/3.10.9/lib/python3.10/site-packages/neo4j/_sync/io/_pool.py", line 800, in update_routing_table
    raise ServiceUnavailable("Unable to retrieve routing information")
neo4j.exceptions.ServiceUnavailable: Unable to retrieve routing information

Here is my docker configuration for that container:

 neo4j:
    image: neo4j:latest
    environment:
      - NEO4J_server_memory_pagecache_size=1024M
    volumes:
      - ./neo4j/data:/data
      - ./neo4j/logs:/logs
     healthcheck:
      disable: true
    labels:
      traefik.enable: "true"
      traefik.hostname: neo4j
      traefik.http.services.neo4j.loadbalancer.server.port: "7474"

      traefik.tcp.routers.neo4j-tcp.entrypoints: "neo4j"
      traefik.tcp.routers.neo4j-tcp.tls: "true"
      traefik.tcp.routers.neo4j-tcp.rule: "HostSNI(`neo4j.localtest.me`)"
      traefik.tcp.routers.neo4j-tcp.service: "neo4j-tcp"
      traefik.tcp.services.neo4j-tcp.loadbalancer.server.port: "7687"

      
    networks:
      - default
      - proxy

This is my python script:

from neo4j import GraphDatabase

# Create the uri for the connection
uri = "neo4j+s://neo4j.localtest.me:7687"

# Create the driver object
print("Connecting to {}".format(uri))

driver = GraphDatabase.driver(uri, auth=("neo4j", "neo4j!123"))
print("Driver created, verifying connectivity...")

# Get info about the driver

driver.verify_connectivity()

My guess here is that when it tries to establish the websocket, it can't get back out of the container once it gets in.

I appreciate any ideas here!

I did end up getting this to work by just changing my protocol to neo4j+ssc instead of just neo4j +s

Not sure I understand why, but it worked.

1 Like

worked for me as well, i was creating app on my windows 10 laptop