Installing Dgraph on Ubuntu 22.04

Updated , by Rajakavitha Kodhandapani
Select distribution:
Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Create a Linode account to try this guide with a $ credit.
This credit will be applied to any valid services used during your first  days.

In this Dgraph tutorial, learn how to install the database on Ubuntu 22.04, add a schema and access the GraphQL endpoint.

Graph databases are databases explicitly designed for the analysis of relationships. You can use graph databases if you are interested in the analysis of relationships between data — not to build the data store for your typical backend application.

Dgraph is an open-source, scalable, distributed, highly available and fast graph database, designed from the ground up to be run in production.

Dgraph consists of different nodes such as Zero, Alpha, and Ratel each node serves a different purpose.

  • Dgraph Zero control the Dgraph database cluster. It assigns Alpha nodes to groups, re-balances data between groups, handles transaction timestamp and UID assignment.
  • Dgraph Alpha hosts predicates and indexes. Predicates are either the properties associated with a node or the relationship between two nodes. Indexes are the tokens that can be associated with the predicates to enable filtering using appropriate functions.
  • Ratel is the UI to run queries, mutations, and altering schema.

Before You Begin

  • Familiarize yourself with our Getting Started guide and complete the steps for setting your Linode’s hostname and timezone.

  • Complete the sections of our Securing Your Server to create a standard user account, harden SSH access and remove unnecessary network services.

  • Update your system:

      sudo apt-get update && sudo apt-get upgrade
    
  • Install Docker Engine

Note
This guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo. If you’re not familiar with the sudo command, you can check our Users and Groups guide.

Install Dgraph

You can install Dgraph using the Docker Compose.

  1. If you not already installed Docker Compose, install it using:

    sudo apt install docker-compose
    
  2. Download the Dgraph docker-compose.yml file:

    wget https://github.com/dgraph-io/dgraph/raw/main/contrib/config/docker/docker-compose.yml
    

    By default only the localhost IP 127.0.0.1 is allowed. When you run Dgraph on Docker, the containers are assigned IPs and those IPs need to be added to the allowed list.

  3. Add a list of IPs allowed for Dgraph so that you can create the schema. Use an editor of your choice and add the <ip_address> of the local host in docker-compose.yml file:

    File: /docker-compose.yml
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    
    # This Docker Compose file can be used to quickly bootup Dgraph Zero
    # and Alpha in different Docker containers.
    
    # It mounts /tmp/data on the host machine to /dgraph within the
    # container. You will need to change /tmp/data to a more appropriate location.
    # Run `docker-compose up` to start Dgraph.
    
    version: "3.2"
    services:
      zero:
        image: dgraph/dgraph:latest
        volumes:
          - /tmp/data:/dgraph
        ports:
          - 5080:5080
          - 6080:6080
        restart: on-failure
        command: dgraph zero --my=zero:5080
      alpha:
        image: dgraph/dgraph:latest
        volumes:
          - /tmp/data:/dgraph
        ports:
          - 8080:8080
          - 9080:9080
        restart: on-failure
        command: dgraph alpha --my=alpha:7080 --zero=zero:5080 --security whitelist=<ip_address>
      ratel:
        image: dgraph/ratel:latest
        ports:
          - 8000:8000

  4. Run the docker-compose command to start the Dgraph services in the docker container:

    sudo docker-compose up
    

    After Dgraph is installed on Docker, you can view the images and the containers running in Docker for Dgraph.

  5. View the containers running for Dgraph using:

    sudo docker ps -a
    

    An output similar to the following appears:

       CONTAINER ID   IMAGE                  COMMAND                  CREATED
       4b67157933b6   dgraph/dgraph:latest   "dgraph zero --my=ze…"   2 days ago
       3faf9bba3a5b   dgraph/ratel:latest    "/usr/local/bin/dgra…"   2 days ago
       a6b5823b668d   dgraph/dgraph:latest   "dgraph alpha --my=a…"   2 days ago
       
  6. To access the Ratel UI for queries, mutations, and altering schema, open your web browser and navigate to http://<LINODE_IP_ADDRESS>:8000.

  7. Click Launch Latest to access the latest stable release of Ratel UI.

  8. In the Dgraph Server Connection dialog that set the Dgraph server URL as http://<LINODE_IP_ADDRESS>:8080

  9. Click Connect . The connection health appears green.

  10. Click Continue to query or run mutations.

Get Started with Dgraph

  1. Use an editor of your choice and add the following content in file named schema.graphql, to create a schema on a local computer:

    File: /schema.graphql
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
    type Product {
        productID: ID!
        name: String @search(by: [term])
        reviews: [Review] @hasInverse(field: about)
    }
    
    type Customer {
        username: String! @id @search(by: [hash, regexp])
        reviews: [Review] @hasInverse(field: by)
    }
    
    type Review {
        id: ID!
        about: Product!
        by: Customer!
        comment: String @search(by: [fulltext])
        rating: Int @search
    }
  2. Add this schema to Dgraph using the following command:

    curl -X POST <LINODE_IP_ADDRESS>:8080/admin/schema --data-binary '@schema.graphql'
    

    When the schema is added successfully a message similar to the following appears:

    {"data":{"code":"Success","message":"Done"}}%
    

In the Ratel UI go to Schema and the schema that you added appears in Schema.

GraphQL Mutations

You can access that GraphQL endpoint with any of the great GraphQL developer tools such as GraphQL Playground, Insomnia, GraphiQL and Altair. The GraphQL endpoint is http://<LINODE_IP_ADDRESS>:8080/graphql. For more information about getting started with GraphQL mutations and queries, see the Dgraph quick start guide.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This page was originally published on


Your Feedback Is Important

Let us know if this guide was helpful to you.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.
The Disqus commenting system for Linode Docs requires the acceptance of Functional Cookies, which allow us to analyze site usage so we can measure and improve performance. To view and create comments for this article, please update your Cookie Preferences on this website and refresh this web page. Please note: You must have JavaScript enabled in your browser.