Connection Refused - How to Fix?

from collections import defaultdict

from click import clear
from neo4j import GraphDatabase

driver = GraphDatabase.driver(
"bolt://localhost:7687",
auth=("neo4j", "")
)

with driver.session() as s:
result = s.run("MATCH (n) RETURN n")
data = defaultdict(int)
for r in result:
for l in r["n"].labels:
data[l] += 1
print(data)

driver.close()

Traceback:

(.venv) larryeisenberg@Mac Neo4j Learn % python3 start.py
Traceback (most recent call last):
File "/Users/larryeisenberg/PycharmProjects/Neo4jLearn/.venv/lib/python3.11/site-packages/neo4j/_async_compat/network/_bolt_socket.py", line 528, in _connect
s.connect(resolved_address)
ConnectionRefusedError: [Errno 61] Connection refused

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

Traceback (most recent call last):
File "/Users/larryeisenberg/PycharmProjects/Neo4jLearn/.venv/lib/python3.11/site-packages/neo4j/_async_compat/network/_bolt_socket.py", line 690, in connect
s = BoltSocket._connect(resolved_address, tcp_timeout,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/larryeisenberg/PycharmProjects/Neo4jLearn/.venv/lib/python3.11/site-packages/neo4j/_async_compat/network/_bolt_socket.py", line 546, in _connect
raise ServiceUnavailable(
neo4j.exceptions.ServiceUnavailable: Failed to establish connection to ResolvedIPv4Address(('127.0.0.1', 7687)) (reason [Errno 61] Connection refused)

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

Traceback (most recent call last):
File "/Users/larryeisenberg/PycharmProjects/Neo4j Learn/start.py", line 12, in
result = s.run("MATCH (n) RETURN n")
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/larryeisenberg/PycharmProjects/Neo4jLearn/.venv/lib/python3.11/site-packages/neo4j/_sync/work/session.py", line 302, in run
self._connect(self._config.default_access_mode)
File "/Users/larryeisenberg/PycharmProjects/Neo4jLearn/.venv/lib/python3.11/site-packages/neo4j/_sync/work/session.py", line 130, in _connect
super()._connect(
File "/Users/larryeisenberg/PycharmProjects/Neo4jLearn/.venv/lib/python3.11/site-packages/neo4j/_sync/work/workspace.py", line 178, in _connect
self._connection = self.pool.acquire(**acquire_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/larryeisenberg/PycharmProjects/Neo4jLearn/.venv/lib/python3.11/site-packages/neo4j/_sync/io/_pool.py", line 526, in acquire
return self._acquire(
^^^^^^^^^^^^^^
File "/Users/larryeisenberg/PycharmProjects/Neo4jLearn/.venv/lib/python3.11/site-packages/neo4j/_sync/io/_pool.py", line 313, in _acquire
return connection_creator()
^^^^^^^^^^^^^^^^^^^^
File "/Users/larryeisenberg/PycharmProjects/Neo4jLearn/.venv/lib/python3.11/site-packages/neo4j/_sync/io/_pool.py", line 163, in connection_creator
connection = self.opener(
^^^^^^^^^^^^
File "/Users/larryeisenberg/PycharmProjects/Neo4jLearn/.venv/lib/python3.11/site-packages/neo4j/_sync/io/_pool.py", line 500, in opener
return Bolt.open(
^^^^^^^^^^
File "/Users/larryeisenberg/PycharmProjects/Neo4jLearn/.venv/lib/python3.11/site-packages/neo4j/_sync/io/_bolt.py", line 401, in open
BoltSocket.connect(
File "/Users/larryeisenberg/PycharmProjects/Neo4jLearn/.venv/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 localhost:7687 (resolved to ()):
Failed to establish connection to ResolvedIPv4Address(('127.0.0.1', 7687)) (reason [Errno 61] Connection refused)
Failed to establish connection to ResolvedIPv6Address(('::1', 7687, 0, 0)) (reason [Errno 61] Connection refused)
(.venv) larryeisenberg@Mac Neo4j Learn %

Is the service up? Is this a local db in desktop or a server instance.

My goal is to have a stand alone app using a local database similar to using python with SQLight with SQLAlchemy with Flask

Thank you for responding to me Gary. You can check my answer on the website. I hope that is OK. Thank you,

Larry

Did you start the server?

If by start do you mean connect to the database? I got connection refused.

So, you are not using neo4j desktop, but you installed neo4j - correct?

Btw - for local development neo4j desktop is very good, as you can create any number of projects with many databases of different version and purpose. You can start and stop them, and you can use the UI to test them.

Hi Gary - Yes I installed neo4j. Regarding using the desktop, If found it easy to use in the elementary classes in Neo4j Academy, but I don't know how to use the desktop to create a stand alone app, with a database stored locally on my computer. I am trying to something similar to using SQLight with SQLAlchemy with Flask and Python. I am writing my code in Python, so I would need call Cyber from Python. Am I making any sense here? Thanks Gary for your help. - Larry

Ok, as for desktop, you can use it for development like you want. Just make sure you start the database you want to use within neo4j desktop. It will be reachable at localhost:7687 using a driver. I do this all the time with the Java driver and neo4j SDN when I am developing.

If you installed neo4j as a standalone server, you need to start it or install it as a service.

Here are the instructions for both Mac and windows:

Both describe how to start each.

Does this help, or is the problem something else?

Thank you Gary. Sounds like good news to me. I will do as you suggest Monday. I am quite excited to see if this works.

1 Like

Hope you don’t mind this question Gary. The latest JDK is version 23. According neo4j docs Neo4j’s support includes JDK version 21. Stick with 21?

You should use the supported version...

Thank you Gary. Anyway to install in virtual environment?

Sure. You can install it like any other application int the virtual environment. You may want to consider a docker container instead. A virtual environment needs an operating system and all the other overhead. A docker container will allow you to run the neo4j server directly. You will have to learn a little though.

I use docker containers as well for hosting each of my micro services and a neo4j server for testing. I also use test containers for integration testing with a neo4j database.

https://java.testcontainers.org/modules/databases/neo4j/

Thanks Gary. I’ll check docker out.

How to get from this to your pic?

Maybe this log will help

What is your status now? How can we help?

Thank for asking Gary. I think I need a bit more specific directions. However my questions may seem dumb. Hopefully not too dumb for you to be OK with answering them. For starters which of these releases of JDK 17 should I use:

2021/06/10 | | Rampdown Phase One (fork from main line) |

General availability would be the one released.