Need Help with Neo4j and Python Integration: Connection Error and Managing Multiple Databases

Hey Neo4j community,

I'm currently working on a Python project that involves integrating Neo4j, and I'm encountering a couple of issues that I could really use some help with.

1\Connection Error When Running Python Script : I'm using Neo4j Community Edition 4.4.31 along with Python driver 5.18.0. Whenever I execute my Python script, I encounter the error message: "Unable to retrieve routing information. Connection failed: Unable to retrieve routing information." Interestingly, when the Neo4j server is opened separately and then I run the script again, it successfully executes without errors. This raises the question: should I keep the Neo4j server open while running the Python script? If not, what could be causing this error and how can I resolve it?


2\Managing Multiple Databases: I've read in the Neo4j documentation that the Community Edition allows only one standard database. However, I need to work with two databases in my Python project. I understand that I can modify the dbms.default_database configuration setting to switch between databases.
For example, if I set the database variable to the name of the second database, it displays the error message: 'Connection failed: {code: Neo.ClientError.Database.DatabaseNotFound} {message: Unable to get a routing table for database 'neo4j' because this database does not exist.' This occurs because I haven't modified dbms.default_database to the name of the second database, thus preventing its use.
But how can I effectively manage both databases within my Python project?
this is my simple code for testing connection

from neo4j import GraphDatabase
 

uri = "neo4j://localhost"
username = "neo4j"
password = "justmdp"    



try:
    driver = GraphDatabase.driver(uri, auth=(username, password))

    dataDB=driver.session(database='metadata')
    with dataDB as session:
        result = session.run("MATCH (n) RETURN n LIMIT 5")
        
        for record in result:
            print(record)

except Exception as e:
    print(f"Connection failed: {str(e)}")


@oumamaexol

in response to your 2 issues

  1. you are running neo4j console which is a foreground start of Neo4j. If you terminate this processs then Neo4j thus stops. Whereas neo4j start is a background start of Neo4j. Running neo4j start will return you back to the linux prompt but Neo4j still remains running. Running neo4j stop will thus stop Neo4j.
    The error you are encountering is a bit misleading in that this message is provided if there is any connection failure.

2). Neo4j Community will only ever see the system database and 1 other user database. Not sure how you are going to get around this given your current design/implementation

Integrating Neo4j with Python can indeed pose some challenges, but these can be resolved with a better understanding of how Neo4j and its Python driver operate. Let's address your two primary concerns:

1. Connection Error When Running Python Script

The error message you're encountering, "Unable to retrieve routing information. Connection failed: Unable to retrieve routing information," typically indicates an issue with the server's availability or configuration. Here are a few steps to help resolve this:

  1. Ensure the Neo4j Server is Running: The Neo4j server must be running and accessible when you execute your Python script. If the server is not running, the driver won't be able to connect. You should start the Neo4j server before running your script.

  2. Check Neo4j URI and Configuration: Ensure that the URI is correctly specified. For a local server, it should be neo4j://localhost:7687 unless you've changed the default port.

    uri = "neo4j://localhost:7687"
    
  3. Verify Authentication Details: Ensure that the username and password are correct and that the Neo4j instance is configured to accept these credentials.

  4. Driver Version Compatibility: Verify that the Neo4j Python driver version is compatible with your Neo4j server version. The versions you've mentioned should generally work together, but it's always good to double-check compatibility in the official [Neo4j Python driver documentation]

2. Managing Multiple Databases in Neo4j Community Edition

Neo4j Community Edition only supports a single database. To work with multiple databases, you would typically need the Enterprise Edition. However, there are workarounds you can consider:

  1. Switching Databases in Configuration: As you've noted, you can change the dbms.default_database setting to switch between databases. This approach is not practical for concurrent access but can work for sequential operations where you restart the server between operations.

  2. Using Different Neo4j Instances: Run multiple Neo4j server instances on different ports, each handling a separate database. This way, you can connect to different databases by changing the URI in your Python script.

Here's an example of running two Neo4j instances on different ports:

  • Instance 1: neo4j://localhost:7687 (default database)
  • Instance 2: neo4j://localhost:7688 (second database)
from neo4j import GraphDatabase

# Connection details for the first database
uri1 = "neo4j://localhost:7687"
username = "neo4j"
password = "justmdp"

# Connection details for the second database
uri2 = "neo4j://localhost:7688"

try:
    # Connect to the first database
    driver1 = GraphDatabase.driver(uri1, auth=(username, password))
    with driver1.session(database='neo4j') as session1:
        result1 = session1.run("MATCH (n) RETURN n LIMIT 5")
        for record in result1:
            print("Database 1:", record)

    # Connect to the second database
    driver2 = GraphDatabase.driver(uri2, auth=(username, password))
    with driver2.session(database='metadata') as session2:
        result2 = session2.run("MATCH (n) RETURN n LIMIT 5")
        for record in result2:
            print("Database 2:", record)

except Exception as e:
    print(f"Connection failed: {str(e)}")

In this setup, you'll have two Neo4j instances running on different ports, allowing you to manage and query both databases independently within the same Python script.

Summary

  • Ensure your Neo4j server is running when executing your Python script.
  • Verify URI, authentication details, and version compatibility.
  • For managing multiple databases, consider running multiple Neo4j instances on different ports.

If you need to work with multiple databases concurrently and more efficiently, you might want to consider upgrading to the Neo4j Enterprise Edition.