How to manage Memgraph binary instances in Python
Through this guide, you will learn how to start, stop, connect to and monitor Memgraph instances with GQLAlchemy.
This feature only works with Memgraph. Neo4j is not supported.
First, perform all the necessary imports:
from gqlalchemy.instance_runner import MemgraphInstanceBinary
Start the Memgraph instance
In order to start a Memgraph instance that you installed using
dpkg, you need
to run the binary file as user
memgraph. Otherwise, the process won't have the
right access rights to the needed directories and files.
The following code will create a Memgraph instance, start it and return a connection object:
memgraph_instance = MemgraphInstanceBinary(
host="0.0.0.0", port=7698, binary_path="/usr/lib/memgraph/memgraph", user="memgraph"
memgraph = memgraph_instance.start_and_connect(restart=False)
We used the default values for the arguments:
host="0.0.0.0": This is the wildcard address which indicates that the instance should accept connections from all interfaces.
port=7687: This is the default port Memgraph listens to.
binary_path="/usr/lib/memgraph/memgraph": The default location of the Memgraph binary file on Ubuntu.
user="memgraph": The user that will start the Memgraph process.
restart=False: If the instance is already running, it won't be stopped and started again.
After we have created the connection, we can start querying the database:
memgraph.execute_and_fetch("RETURN 'Memgraph is running' AS result"))["result"]
Pass configuration flags
You can pass configuration flags using a dictionary:
memgraph_instance = MemgraphInstanceBinary(config=config)
Stop the Memgraph instance
To stop a Memgraph instance, call the
Check if a Memgraph instance is running
To check if a Memgraph instance is running, call the
Where to next?
Hopefully, this guide has taught you how to manage Memgraph Docker instances. If you have any more questions, join our community and ping us on Discord.