Skip to main content
Version: 2.8.0

Querying with mgconsole

The easiest way to execute Cypher queries against Memgraph is by using Memgraph's command-line tool, mgconsole.

1. Install mgconsole

tip

If you installed Memgraph Platform with the Docker image (memgraph/memgraph-platform), mgconsole will start automatically when you run the container. Skip the installation steps and continue with executing Cypher queries.

If you installed any other Docker image or want to explicitly start the mgconsole from the Memgraph Platform image, you need to manually run mgconsole following the steps described below.

If you want to install or run mgconsole to query a running Memgraph database instance, use the following steps:

If you installed MemgraphDB using Docker or closed the terminal with the running mgconsole from Memgraph Platform image, run the mgconsole client from your Docker image using the following commands:

1. First, you need to find the CONTAINER_ID by running:

docker ps

2. Once you know the CONTAINER_ID, you can start mgconsole by running the following command:

docker exec -it CONTAINER_ID mgconsole

2. Execute a Cypher query

After the client has started, it should present a command prompt similar to:

mgconsole X.X
Connected to 'memgraph://127.0.0.1:7687'
Type :help for shell usage
Quit the shell by typing Ctrl-D(eof) or :quit
memgraph>

At this point, it is possible to execute Cypher queries against a running Memgraph database instance.

tip

You can use the TAB key to autocomplete commands in mgconsole.

Each query needs to end with the ; (semicolon) character. For example:

CREATE (u:User {name: "Alice"})-[:Likes]->(m:Software {name: "Memgraph"});

The above query will create 2 nodes in the database, one labeled "User" with name "Alice" and the other labeled "Software" with name "Memgraph". It will also create a relationship that "Alice" likes "Memgraph".

To find created nodes and relationships, execute the following query:

MATCH (u:User)-[r]->(x) RETURN u, r, x;

Query execution time

To get a breakdown of the execution time, set the -verbose_execution_info flag to true.

Upon query execution you will get this information:

Query COST estimate: 3066
Query PARSING time: 0.000175982 sec
Query PLAN EXECUTION time: 0.0154524 sec
Query PLANNING time: 8.054e-05 sec

The values show:

  • COST estimate - Internal planner estimation on the cost of the query. When comparing two query executions, an order of magnitude larger COST estimates might indicate the query's longer execution time.
  • PARSING time - Time spent checking if the query is valid and normalizing it for cache.
  • PLAN EXECUTION time - Time executing the plan.
  • PLANNING time - Time it takes the query planner to create the optimal plan to execute the query.

Configure mgconsole

Below are configurational flags you can use with mgconsole:

Main

FlagDescriptionTypeDefault
-csv_delimiterCharacter used to separate fields.string","
-csv_doublequoteControls how instances of the quotechar(") appearing inside a field should themselves be quoted. When true, the character is doubled. When false, the escapechar is used as a prefix to the quotechar. If csv_doublequote is false, csv_escapechar must be set.booltrue
-csv_escapecharCharacter used to escape the quote character (") if csv_doublequote is false.string""
-fit_to_screenFit output width to screen width.boolfalse
-historyUse the specified directory to save history.string"~/.memgraph"
-hostServer address. It can be a DNS resolvable hostname.string"127.0.0.1"
-no_historyDo not save history.boolfalse
-output_formatQuery output format. Can be csv or tabular. If the output format is not tabular fit_to_screen flag is ignored.string"tabular"
-passwordDatabase password.string""
-portServer port.int327687
-term_colorsUse terminal colors syntax highlighting.boolfalse
-use_sslUse SSL when connecting to the server.boolfalse
-usernameDatabase username.string""
-verbose_execution_infoOutput the additional information about query such as query cost, parsing, planning and execution times.boolfalse

Flags

FlagDescriptionTypeDefault
-flagfileLoad flags from a file.string""
-fromenvSet flags from the environment [example: 'export FLAGS_flag1=value'].string""
-tryfromenvSet flags from the environment if present.string""
-undefokComma-separated list of flag names. These flags can be specified on the command line even if the program does not define a flag with that name. IMPORTANT: Flags from the list that have arguments MUST use the flag=value format.string""
-tab_completion_columnsThe number of columns used in output for tab completion.int3280
-tab_completion_wordIf non-empty, HandleCommandLineCompletions() will hijack the process and attempt to do bash-style command line flag completion on this value.string""

Help

FlagDescriptionTypeDefault
-helpShow help on all flags [tip: all flags can have two dashes].boolfalse
-helpfullShow help on all flags -- same as -help.boolfalse
-helpmatchShow help on modules, names of which contain the specified substring.string""
-helponShow help on the modules named by this flag value.string""
-helppackageShow help on all modules in the main package.boolfalse
-helpshortShow help on the main module for this program only.boolfalse
-helpxmlProduce an .xml version of help.boolfalse
-versionShow version and build info then exit.boolfalse
caution

When working with Memgraph Platform, you should pass configuration flags inside of environment variables.

For example, you should start Memgraph Platform with docker run -e MGCONSOLE="-output_format="csv"" memgraph/memgraph-platform.

Non-interactive mode

To get the query result in bash, use the following command:

mgconsole < <(echo "MATCH (n:Person) RETURN n;")

or

echo "MATCH (n:Person) RETURN n;" | mgconsole

To save the query results in a file, use the following command:

mgconsole < <(echo "MATCH (n:Person) RETURN n;") > results.txt

Where to next?

If you want to learn more about graph databases and Cypher queries, visit Memgraph Playground and go through the guided lessons. All the datasets and most of the queries used in the guided lessons can also be explored in the Tutorials section, and knowledge about Cypher is gathered in the Cypher manual.

If you are all good to go on your own - import your data!