在 Ubuntu 22.04 上安装和配置 Neo4j

在 Ubuntu 22.04 上安装和配置 Neo4j

原文来源 : Akamai - Installing and Configuring Neo4j on Ubuntu 22.04

发布时间 : 2024年5月8日更新 | 原始作者: Cameron Laird

本文档涵盖了在 Ubuntu 22.04 LTS 上安装、配置和使用 Neo4j 图数据库的完整教程。


目录


Neo4j 简介

Neo4j 是目前最受欢迎的图数据库管理系统(DBMS),根据 DB-engine 排名,它在图数据库领域排名第一。Neo4j 采用开源许可证,可免费安装在虚拟机上。它专注于管理和搜索连接数据,例如人物之间的同事关系或文档之间的语义关联。


开始之前

前提条件

  1. 创建实例 : 需要两个 位于同一数据中心的 Ubuntu 22.04 LTS Compute Instance。实际应用中,Neo4j 通常需要至少 4 个 CPU 核心和 8GB 以上内存 。两个实例都需要添加私有 IP 地址

  2. 系统安全设置 : 按照 Setting Up and Securing a Compute Instance 指南更新系统、设置时区、配置主机名、创建受限用户账户并加固 SSH 访问。

注意事项

  • 本指南为非 root 用户编写,需要提权的命令前缀为 sudo
  • 文档中使用的占位符及其替换说明:
占位符 替换为
INSTANCE_1_PUBLIC_IP Neo4j 实例的公共 IP 地址
INSTANCE_1_PRIVATE_IP Neo4j 实例的私有 IP 地址
YOUR_IP_ADDRESS 本地机器的公共 IP 地址
NEO4J_PASSWORD 管理 neo4j 用户的密码
LOCAL_NETWORK_ADDRESS/SUBNET 实例私有 IPv4 地址的本地网络地址和子网

安装 Neo4j

两个 Ubuntu 22.04 LTS 实例上执行以下步骤:

步骤 1: 安装 Neo4j 的缺失依赖

Instance 1 & 2

bash 复制代码
sudo apt install apt-transport-https gnupg2 unzip

步骤 2: 安装 Neo4j 官方包仓库的 GPG 密钥

Instance 1 & 2

bash 复制代码
curl -fsSL https://debian.neo4j.com/neotechnology.gpg.key | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/neo4j.gpg

步骤 3: 添加 Neo4j 官方包仓库

Instance 1 & 2

bash 复制代码
echo 'deb https://debian.neo4j.com stable latest' | sudo tee -a /etc/apt/sources.list.d/neo4j.list

步骤 4: 更新包管理器

Instance 1 & 2

bash 复制代码
sudo apt update

步骤 5: 安装 Neo4j 包

Instance 1 & 2

bash 复制代码
sudo apt install neo4j

出现提示时输入 Y 并按 Enter


激活 Neo4j

仅在第一个实例(作为 Neo4j 服务器)上执行以下步骤:

步骤 1: 启动 Neo4j 服务

Instance 1

bash 复制代码
sudo systemctl start neo4j

步骤 2: 设置 Neo4j 开机自启

Instance 1

bash 复制代码
sudo systemctl enable neo4j

步骤 3: 验证安装

Instance 1

bash 复制代码
sudo systemctl status neo4j

输出中应显示 active (running)

复制代码
● neo4j.service - Neo4j Graph Database
        Loaded: loaded (/lib/systemd/system/neo4j.service; enabled; vendor preset: enabled)
        Active: active (running) since Fri 2023-09-29 19:36:26 EDT; 3s ago

q 键关闭输出并返回终端提示符。


首次登录

步骤 1: 启动 cypher-shell 交互式解释器

Instance 1

bash 复制代码
cypher-shell

步骤 2: 登录并更新密码

  • 新安装的默认用户名和密码均为 neo4j
  • 首次运行时,系统会立即要求为 neo4j 账户设置新密码
  • 请将新密码保存在安全的地方

登录成功后,提示符如下:

复制代码
neo4j@neo4j>

步骤 3: 退出 cypher-shell

Instance 1

bash 复制代码
:exit

更多用户管理信息请参考 Neo4j 官方文档


使用 Cypher-Shell

步骤 1: 重新登录

Instance 1

bash 复制代码
cypher-shell

步骤 2: 查看可用命令

Instance 1

bash 复制代码
:help

可用命令列表:

复制代码
Available commands:
     :begin       Open a transaction
     :commit      Commit the currently open transaction
     :connect     Connects to a database
     :disconnect  Disconnects from database
     :exit        Exit the logger
     :help        Show this help message
     :history     Statement history
     :impersonate Impersonate user
     :param       Set the value of a query parameter
     :rollback    Rollback the currently open transaction
     :source      Executes Cypher statements from a file
     :sysinfo     Neo4j system information
     :use         Set the active database

步骤 3: 创建示例节点

创建一个 "Welcome" 组织节点:

Instance 1

cypher 复制代码
CREATE (:Welcome);

输出:

复制代码
0 rows
ready to start consuming query after 64 ms, results consumed after another 0 ms
Added 1 nodes, Added 1 labels

步骤 4: 添加成员记录

Instance 1

cypher 复制代码
CREATE (:Welcome {name: 'Mary Jones'});

输出:

复制代码
0 rows
ready to start consuming query after 32 ms, results consumed after another 0 ms
Added 1 nodes, Set 1 properties, Added 1 labels

步骤 5: 检索数据验证存储

Instance 1

cypher 复制代码
MATCH (node) RETURN node;

输出:

复制代码
+---------------------------------+
| node                            |
+---------------------------------+
| (:Welcome)                      |
| (:Welcome {name: "Mary Jones"}) |
+---------------------------------+

2 rows
ready to start consuming query after 25 ms, results consumed after another 5 ms

步骤 6: 退出

Instance 1

bash 复制代码
:exit

更多 Cypher 语言功能请参考 Getting Started with Cypher


Neo4j 配置要点

基本配置

步骤 1: 编辑配置文件

Instance 1

bash 复制代码
sudo nano /etc/neo4j/neo4j.conf

编辑完成后按 CTRL + X,然后按 Y,再按 Enter 保存并退出 nano

步骤 2: 重启 Neo4j 使配置生效

Instance 1

bash 复制代码
sudo systemctl restart neo4j

Neo4j 的默认配置已经很完善,通常无需大幅修改。高级配置请参考 Neo4j 官方文档


接受本地网络连接

步骤 1: 修改配置文件

Instance 1

bash 复制代码
sudo nano /etc/neo4j/neo4j.conf

找到以下行并移除注释符 #

修改前:

复制代码
#server.default_listen_address=0.0.0.0

修改后:

复制代码
server.default_listen_address=0.0.0.0

配置文件上下文:

复制代码
#*****************************************************************
# Network connector configuration
#*****************************************************************

# With default configuration Neo4j only accepts local connections.
# To accept non-local connections, uncomment this line:
server.default_listen_address=0.0.0.0

此设置使 Neo4j 监听所有 IPv4 网络接口。

CTRL + X,然后按 Y,再按 Enter 保存退出。

步骤 2: 重启 Neo4j

Instance 1

bash 复制代码
sudo systemctl restart neo4j

步骤 3: 配置防火墙允许本地网络访问

Instance 1

bash 复制代码
sudo ufw allow from LOCAL_NETWORK_ADDRESS/SUBNET to any port 7687 proto tcp

⚠️ 重要提示 : 如果通过 SSH 连接到实例,请确保在启用 UFW 之前打开端口 22,否则连接可能中断,只能通过 LISH Console 重新登录。

打开 SSH 端口:

bash 复制代码
sudo ufw allow 22/tcp

步骤 4: 启用系统防火墙

Instance 1

bash 复制代码
sudo ufw enable

出现提示时按 y 继续:

复制代码
Firewall is active and enabled on system startup

步骤 5: 验证防火墙配置

Instance 1

bash 复制代码
sudo ufw status

输出示例:

复制代码
Status: active

To                         Action      From
--                         ------      ----
7687/tcp                   ALLOW       192.168.128.0/17
22/tcp                     ALLOW       Anywhere
22/tcp (v6)                ALLOW       Anywhere (v6)

步骤 6: 从第二个实例通过本地网络连接

Instance 2

bash 复制代码
cypher-shell -a neo4j://INSTANCE_1_PRIVATE_IP:7687 -u neo4j -p NEO4J_PASSWORD

步骤 7: 验证数据访问

cypher 复制代码
MATCH (node) RETURN node;

应返回与之前相同的数据:

复制代码
+---------------------------------+
| node                            |
+---------------------------------+
| (:Welcome)                      |
| (:Welcome {name: "Mary Jones"}) |
+---------------------------------+

2 rows
ready to start consuming query after 25 ms, results consumed after another 5 ms

步骤 8: 退出

bash 复制代码
:exit

接受远程连接

以下步骤使本地桌面可以通过 Neo4j Browser 远程访问。

步骤 1: 更新防火墙允许远程机器访问

Instance 1

bash 复制代码
sudo ufw allow from YOUR_IP_ADDRESS to any port 7687,7474 proto tcp

步骤 2: 重新加载防火墙

Instance 1

bash 复制代码
sudo ufw reload

步骤 3: 验证防火墙配置

Instance 1

bash 复制代码
sudo ufw status

输出示例:

复制代码
Status: active

To                         Action      From
--                         ------      ----
7687/tcp                   ALLOW       192.168.128.0/17
22/tcp                     ALLOW       Anywhere
7474,7687/tcp              ALLOW       01.234.567.89
22/tcp (v6)                ALLOW       Anywhere (v6)

使用 Neo4j Browser

步骤 1: 访问 Web 界面

在本地桌面的浏览器中访问:

复制代码
http://INSTANCE_1_PUBLIC_IP:7474

步骤 2: 登录

  • 用户名 : neo4j
  • 密码: 之前设置的新密码

步骤 3: 使用快速链接

登录后可看到以下帮助主题:

  • Getting started with Neo4j Browser --- Neo4j Browser 快速入门
  • Try Neo4j with live data --- 使用实时数据体验 Neo4j
  • Cypher basics --- Cypher 基础

选择 Try Neo4j with live data 下的 Open guide 并按指示操作。

教程会直接操作你的 Neo4j 安装实例,创建、更新和查询数据。

步骤 4: 验证浏览器创建的数据

切换回云实例,打开 cypher-shell,运行以下命令:

Instance 1 or 2

cypher 复制代码
MATCH (tom:Person {name: "Tom Hanks"}) RETURN tom;

应返回:

复制代码
+-------------------------------------------+
| tom                                       |
+-------------------------------------------+
| (:Person {name: "Tom Hanks", born: 1956}) |
+-------------------------------------------+

1 row
ready to start consuming query after 2 ms, results consumed after another 0 ms

Neo4j Browser 内置了丰富的可视化功能,可以通过 API 操作数据并生成快速图表。


总结

完成本教程后,你应该能够:

  1. 在 Ubuntu 22.04 LTS 上安装 Neo4j 图数据库
  2. 掌握 Neo4j 的基本配置方法
  3. 开放本地网络或远程机器的访问
  4. 使用 cypher-shell 命令行工具或 Neo4j Browser 图形界面管理和查询数据

关键端口说明

端口 用途
7687 Bolt 协议(cypher-shell 和程序连接)
7474 Neo4j Browser Web 界面
22 SSH 访问

本教程由 Akamai 原始英文文档翻译整理而成,仅供参考学习。