2017-03-01

Neo4j is a different type of database, is a graph database. It is quite cool and it is worth to spend the time to learn how it works.

The main benefit of a graph database is that the information is interconnected as a graph, and allows to execute complex queries very quickly.

One of the sample databases in Neo4j is (big part of) the content of IMDb.com (the movie database). Here is the description of some possible queries:

Find actors who worked with Gene Hackman, but not when he was also working with Robin Williams in the same movie.

Who are the five busiest actors?

Return the count of movies in which an actor and director have jointly worked

In this post

we install Neo4j in an LXD container on Ubuntu (or any other GNU/Linux distribution that has installation packages)

set it up so we can access Neo4j from our Ubuntu desktop browser

start the cool online tutorial for Neo4j, which you can complete on your own

remove the container (if you really wish!) in order to clean up the space

Creating an LXD container

See Trying out LXD containers on our Ubuntu in order to make the initial (one-time) configuration of LXD on your Ubuntu  desktop.

Then, let’s start with creating a container for neo4j:

Here we launched a container named neo4j, that runs Ubuntu 16.04 (Xenial, hence ubuntu:x).

Let’s see the container details:

It takes a few seconds for a new container to launch. Here, the container is in the RUNNING state, and also has a private IP address. It’s good to go!

Connecting to the LXD container

Let’s get a shell in the new neo4j LXD container.

The command we used to get a shell is this, sudo –login –user ubuntu

We instructed to execute (exec) in the neo4j container the command that appears after the — separator.

The images for the LXD containers have both a root account and also a user account, in the Ubuntu images is called ubuntu. Both accounts are locked (no default password is available). lxc exec runs the commands in the containers as root, therefore, the sudo –login –user ubuntu command would obviously run without asking passwords. This sudo command creates a login shell for the specified user, user ubuntu.

Once we are connected to the container as user ubuntu, we can then run commands as root simply by using sudo in front of them. Since user ubuntu is in /etc/sudoers, no password is asked. That is the reason why sudo apt update was ran earlier without asking a password.

The Ubuntu LXD containers auto-update themselves by running unattended-upgrade, which means that we do not need to run sudo apt upgrade. We do run sudo apt update just to get an updated list of available packages and avoid any errors when installing, just because the package list was changed recently.

After we updated the package list, we exit the container with exit.

Installing neo4j

This is the download page for Neo4j, https://neo4j.com/download/ and we click to get the community edition.



We download Neo4j (the Linux(tar)) on our desktop. When we tried this, version 3.1.1 was available.



We downloaded the file and it can be found in ~/Downloads/ (or the localized name). Let’s copy it to the container,

The tarball is about 80MB and we use lxc file push to copy it inside the neo4j container, in the directory /home/ubuntu/. Note that neo4j/home/ubuntu/ ends with a / character to specify that it is a directory. If you omit this, you get an error.

Let’s deal with the tarball inside the container,

The files are now in the container, let’s run this thing!

Running Neo4j

The commands to manage Neo4j are in the bin/ subdirectory,

According to Running Neo4j, we need to run “neo4j start“. Let’s do it.

We need Java, and the documentation actually said so. We just need the headless JDK, since we are accessing the UI from our browser.

Now, we are ready to start Neo4j!

So, Neo4j is running just fine, but it has been bound on the localhost which makes it inaccessible to our desktop browser. Let’s verify again,

What we actually need, is for Neo4j to bind to all network interfaces so that it becomes accessible to our desktop browser. Being in a container, the all network interfaces means to bind the only other interface, the private network that LXD created for us:

Where do we look in the configuration files of Neo4j to get it to bind to all network interfaces?

We look at the Neo4j documentation on Configuring the connectors, and we see that we need to edit the configuration file neo4j-community-3.1.1/conf/neo4j.conf

We can see that there is an overall configuration parameter for the connectors, and we can set the default_listen_address to 0.0.0.0. This 0.0.0.0 in networking terms means that we want the process to bind to all networking interfaces. Let’s remind us here that in our case of LXD containers that reside in private networking, this is OK.

to

Let’s restart Neo4j and check whether it looks OK:

The log messages still say that Neo4j is accessible at http://localhost:7474/, which is factually correct. However, lsof shows us that it is boung to all network interfaces (the * means all).

Loading up Neo4j in the browser

We know already that in our case, the private IP address of the neo4j LXD container is 10.60.117.21. Let’s visit http://10.60.117.21:7474/ on our desktop Web browser!



It works! It asks us to log in using the default username neo4j with the default password neo4j. Then, it will ask us to change the password to something else and we are presented with the initial page of Neo4j,

The $ ▊ prompt is there for you to type instructions. According to the online tutorial at https://neo4j.com/graphacademy/online-training/introduction-graph-databases/

you can start the tutorial if you type :play movie graph and press the Run button. Therefore, load on one browser tab the tutorial and on other tab run the commands in the neo4j server from the LXD container!

Once you are done

Once you have completed the tutorial, you can keep the container in order to try out more tutorials and learn more about neo4j.

However, if you want to remove this LXD container, it can be done by running:

That’s it. The container is gone and LXD is ready for you to follow more LXD tutorials and create more containers!

Show more