[Python] Unable to connect to neo4j using python driver but works from neo4j browser

Discussão em 'Python' iniciado por Stack, Outubro 1, 2024.

  1. Stack

    Stack Membro Participativo

    I'm Trying to setup neo4j instance using ec2 as a docker container. The ec2 is in private subnet and it is accessible using a application load balance. the load balancer has two listeners one for the browser and another for the bolt.

    I can able to connect to neo4j browser UI using a custom domain mapped for alb and I can able to connect to bolt server as well from the browser UI. The custom domain has ssl cert from ACM.

    But when I try to connect to neo4j bolt using the endpoint url I couldn't able to connect and throwing the below error.

    raceback (most recent call last):
    File "/opt/homebrew/lib/python3.11/site-packages/neo4j/_async_compat/network/_bolt_socket.py", line 694, in connect
    return BoltSocket._handshake(s, resolved_address, deadline)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/homebrew/lib/python3.11/site-packages/neo4j/_async_compat/network/_bolt_socket.py", line 641, in _handshake
    raise ServiceUnavailable(
    neo4j.exceptions.ServiceUnavailable: Cannot to connect to Bolt service on ResolvedIPv4Address(('18.194.47.191', 7687)) (looks like HTTP)

    The above exception was the direct cause of the following exception:

    Traceback (most recent call last):
    File "/Users/Downloads/neo.py", line 12, in <module>
    result = session.run("MATCH (n) RETURN n LIMIT 5")
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/homebrew/lib/python3.11/site-packages/neo4j/_sync/work/session.py", line 302, in run
    self._connect(self._config.default_access_mode)
    File "/opt/homebrew/lib/python3.11/site-packages/neo4j/_sync/work/session.py", line 130, in _connect
    super()._connect(
    File "/opt/homebrew/lib/python3.11/site-packages/neo4j/_sync/work/workspace.py", line 182, in _connect
    self._connection = self._pool.acquire(**acquire_kwargs_)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/homebrew/lib/python3.11/site-packages/neo4j/_sync/io/_pool.py", line 526, in acquire
    return self._acquire(
    ^^^^^^^^^^^^^^
    File "/opt/homebrew/lib/python3.11/site-packages/neo4j/_sync/io/_pool.py", line 313, in _acquire
    return connection_creator()
    ^^^^^^^^^^^^^^^^^^^^
    File "/opt/homebrew/lib/python3.11/site-packages/neo4j/_sync/io/_pool.py", line 163, in connection_creator
    connection = self.opener(
    ^^^^^^^^^^^^
    File "/opt/homebrew/lib/python3.11/site-packages/neo4j/_sync/io/_pool.py", line 500, in opener
    return Bolt.open(
    ^^^^^^^^^^
    File "/opt/homebrew/lib/python3.11/site-packages/neo4j/_sync/io/_bolt.py", line 403, in open
    BoltSocket.connect(
    File "/opt/homebrew/lib/python3.11/site-packages/neo4j/_async_compat/network/_bolt_socket.py", line 718, in connect
    raise ServiceUnavailable(
    neo4j.exceptions.ServiceUnavailable: Couldn't connect to neo.mydomain.com:7687 (resolved to ()):
    Cannot to connect to Bolt service on ResolvedIPv4Address(('18.456.47.191', 7687)) (looks like HTTP)
    Cannot to connect to Bolt service on ResolvedIPv4Address(('52.456.141.61', 7687)) (looks like HTTP)


    And I used the below code to connect to the server

    from neo4j import GraphDatabase

    uri = "bolt+s://neo.mydomain.com:7687"
    username = "neo4j"
    password = "password"

    # Initialize the driver with SSL encryption
    driver = GraphDatabase.driver(uri, auth=(username, password))

    # Optional: Create a session to interact with the database
    with driver.session() as session:
    result = session.run("MATCH (n) RETURN n LIMIT 5")
    for record in result:
    print(record)

    # Remember to close the driver when done
    driver.close()


    I tried with both bolt+s:// and bolt:// but still no luck. Below is my docker-compose.yml

    services:
    neo4j:
    image: neo4j:5.18
    ports:
    - "7474:7474"
    - "7687:7687"
    restart: always
    environment:
    NEO4J_AUTH: "neo4j/password"
    NEO4J_PLUGINS: '["apoc"]'
    NEO4J_dbms_security_procedures_unrestricted: "apoc.*,gds.*"
    NEO4J_dbms_security_procedures_allowlist: "apoc.*,gds.*"
    NEO4J_apoc_export_file_enabled: "true"
    NEO4J_apoc_import_file_enabled: "true"
    NEO4J_server_bolt_listen__address: 0.0.0.0:7687
    NEO4J_server_default__listen__address: 0.0.0.0
    NEO4J_server_bolt_advertised__address: :7687
    volumes:
    - /neo4j_data/data:/data
    - /neo4j_data/logs:/logs

    Continue reading...

Compartilhe esta Página