1. Neo4j图数据库简介
Neo4j是世界领先的图数据库管理系统,使用节点和关系来存储和查询数据。Neo4j以其高性能的图查询能力、ACID事务支持和丰富的生态系统著称,特别适合社交网络、推荐系统、知识图谱等场景。更多学习教程www.fgedu.net.cn
Neo4j核心特点
原生图存储:使用节点、关系、属性存储数据
Cypher查询语言:类SQL的图查询语言,简单易学
ACID事务:完全支持ACID事务,保证数据一致性
高性能:优化的图遍历算法,毫秒级查询响应
丰富生态:支持多种编程语言驱动和可视化工具
2. Neo4j版本说明
Neo4j提供社区版和企业版,用户可根据需求选择。学习交流加群风哥微信: itpux-com
最新版本信息
Neo4j 2025.05.0:最新稳定版本
Neo4j 5.26.8:LTS长期支持版本
Neo4j 4.4.44:历史长期支持版本
版本类型
Community版:免费开源版本,单节点部署,功能完整
Enterprise版:商业版本,支持集群、高可用、安全特性
AuraDB:Neo4j云托管服务
3. Neo4j下载地址
Neo4j可以从官方网站获取安装包。
官方下载地址
官方网站:https://neo4j.com/download-center/
部署中心:https://neo4j.com/deployment-center/
下载安装包
# cd /fgeudb/software
# wget https://dist.neo4j.org/neo4j-community-5.26.8-unix.tar.gz
# 输出示例如下:
–2026-04-04 10:00:00– https://dist.neo4j.org/neo4j-community-5.26.8-unix.tar.gz
Resolving dist.neo4j.org… 151.101.2.132
Connecting to dist.neo4j.org|151.101.2.132|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 157286400 (150M) [application/gzip]
Saving to: ‘neo4j-community-5.26.8-unix.tar.gz’
neo4j-community-5.26.8-unix.tar.gz 100%[===================>] 150.00M 20.5MB/s in 7.3s
2026-04-04 10:00:10 (20.5 MB/s) – ‘neo4j-community-5.26.8-unix.tar.gz’ saved
# 验证下载文件
# ls -lh neo4j-community-5.26.8-unix.tar.gz
-rw-r–r–. 1 root root 150M Apr 4 10:00 neo4j-community-5.26.8-unix.tar.gz
4. 环境准备
在安装Neo4j之前,需要准备必要的系统环境和Java运行时。
系统要求
# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.9 (Maipo)
# 检查系统架构
# uname -m
x86_64
# 检查内存和磁盘空间
# free -h
total used free shared buff/cache available
Mem: 31G 2.1G 28G 8.5M 1.0G 28G
Swap: 15G 0B 15G
# df -h /fgeudb
Filesystem Size Used Avail Use% Mounted on
/dev/sdb1 500G 20G 480G 4% /fgeudb
# 安装JDK 17或更高版本(Neo4j 5.x要求)
# yum install -y java-17-openjdk java-17-openjdk-devel
# 验证Java版本
# java -version
# 输出示例如下:
openjdk version “17.0.8” 2023-07-18 LTS
OpenJDK Runtime Environment 21.9 (build 17.0.8+9-LTS)
OpenJDK 64-Bit Server VM 21.9 (build 17.0.8+9-LTS, mixed mode, sharing)
# 配置JAVA_HOME
# vi /etc/profile.d/java.sh
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
export PATH=$JAVA_HOME/bin:$PATH
# 使环境变量生效
# source /etc/profile.d/java.sh
# 创建Neo4j用户和组
# groupadd -g 5007 neo4j
# useradd -g neo4j -u 5007 -d /home/neo4j -s /bin/bash neo4j
# 创建安装目录
# mkdir -p /fgeudb/neo4j/{data,logs,import}
# chown -R neo4j:neo4j /fgeudb/neo4j
5. TAR包安装实战
使用TAR包安装是最常见的方式,以下是详细步骤。学习交流加群风哥QQ113257174
步骤1:解压安装包
# cd /fgeudb/software
# tar -xzf neo4j-community-5.26.8-unix.tar.gz -C /fgeudb/
# 输出示例如下:
# 重命名目录
# mv /fgeudb/neo4j-community-5.26.8 /fgeudb/neo4j
# 查看目录结构
# ls -la /fgeudb/neo4j/
total 64
drwxr-xr-x. 11 neo4j neo4j 4096 Apr 4 10:05 .
drwxr-xr-x. 3 root root 4096 Apr 4 10:00 ..
drwxr-xr-x. 2 neo4j neo4j 4096 Apr 4 10:05 bin
drwxr-xr-x. 2 neo4j neo4j 4096 Apr 4 10:05 conf
drwxr-xr-x. 2 neo4j neo4j 4096 Apr 4 10:05 data
drwxr-xr-x. 2 neo4j neo4j 4096 Apr 4 10:05 import
drwxr-xr-x. 2 neo4j neo4j 4096 Apr 4 10:05 lib
drwxr-xr-x. 2 neo4j neo4j 4096 Apr 4 10:05 licenses
drwxr-xr-x. 2 neo4j neo4j 4096 Apr 4 10:05 logs
-rw-r–r–. 1 neo4j neo4j 5120 Apr 4 10:05 LICENSE.txt
-rw-r–r–. 1 neo4j neo4j 1024 Apr 4 10:05 NOTICE.txt
-rw-r–r–. 1 neo4j neo4j 2048 Apr 4 10:05 README.txt
# 设置权限
# chown -R neo4j:neo4j /fgeudb/neo4j
步骤2:配置Neo4j
# vi /fgeudb/neo4j/conf/neo4j.conf
# 主要配置参数
# 服务器配置
server.default_listen_address=0.0.0.0
server.default_advertised_address=192.168.1.51
# HTTP连接器
server.http.enabled=true
server.http.listen_address=:7474
server.http.advertised_address=:7474
# HTTPS连接器
server.https.enabled=true
server.https.listen_address=:7473
server.https.advertised_address=:7473
# Bolt连接器
server.bolt.enabled=true
server.bolt.listen_address=:7687
server.bolt.advertised_address=:7687
# 数据目录
server.directories.data=/fgeudb/neo4j/data
server.directories.logs=/fgeudb/neo4j/logs
server.directories.import=/fgeudb/neo4j/import
# 内存配置
server.memory.heap.initial_size=4G
server.memory.heap.max_size=8G
server.memory.pagecache.size=2G
# 数据库配置
dbms.default_database=neo4j
# 保存配置文件
步骤3:启动Neo4j
# su – neo4j
# 启动Neo4j服务
$ cd /fgeudb/neo4j
$ ./bin/neo4j start
# 输出示例如下:
Directories in use:
home: /fgeudb/neo4j
config: /fgeudb/neo4j/conf
logs: /fgeudb/neo4j/logs
plugins: /fgeudb/neo4j/plugins
import: /fgeudb/neo4j/import
data: /fgeudb/neo4j/data
certificates: /fgeudb/neo4j/certificates
run: /fgeudb/neo4j/run
Starting Neo4j.
Started neo4j (pid 12345). It is available at http://localhost:7474
# 检查进程
$ ps -ef | grep neo4j
neo4j 12345 1 5 10:10 ? 00:00:10 /usr/lib/jvm/java-17-openjdk/bin/java -cp /fgeudb/neo4j/lib/* neo4j
# 检查端口
$ netstat -tlnp | grep java
tcp 0 0 0.0.0.0:7473 0.0.0.0:* LISTEN 12345/java
tcp 0 0 0.0.0.0:7474 0.0.0.0:* LISTEN 12345/java
tcp 0 0 0.0.0.0:7687 0.0.0.0:* LISTEN 12345/java
步骤4:配置systemd服务
# vi /etc/systemd/system/neo4j.service
[Unit]
Description=Neo4j Graph Database
After=network.target
[Service]
Type=forking
User=neo4j
Group=neo4j
Environment=”JAVA_HOME=/usr/lib/jvm/java-17-openjdk”
Environment=”NEO4J_HOME=/fgeudb/neo4j”
ExecStart=/fgeudb/neo4j/bin/neo4j start
ExecStop=/fgeudb/neo4j/bin/neo4j stop
PIDFile=/fgeudb/neo4j/run/neo4j.pid
Restart=on-failure
RestartSec=10
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
# 重新加载systemd
# systemctl daemon-reload
# 启动Neo4j服务
# systemctl start neo4j
# 设置开机自启
# systemctl enable neo4j
# 输出示例如下:
Created symlink from /etc/systemd/system/multi-user.target.wants/neo4j.service to /etc/systemd/system/neo4j.service.
# 检查服务状态
# systemctl status neo4j
● neo4j.service – Neo4j Graph Database
Loaded: loaded (/etc/systemd/system/neo4j.service; enabled)
Active: active (running) since Fri 2026-04-04 10:15:00 CST; 10s ago
Process: 12340 ExecStart=/fgeudb/neo4j/bin/neo4j start (code=exited, status=0/SUCCESS)
Main PID: 12345 (java)
CGroup: /system.slice/neo4j.service
└─12345 /usr/lib/jvm/java-17-openjdk/bin/java -cp …
6. Docker容器安装实战
使用Docker容器安装Neo4j是最快捷的方式,适合快速测试和开发环境。更多学习教程公众号风哥教程itpux_com
步骤1:拉取Neo4j镜像
# docker pull neo4j:5.26.8-community
# 输出示例如下:
5.26.8-community: Pulling from library/neo4j
f1f26f570256: Pull complete
8f8e43ef9c3a: Pull complete
e9d8dca5b8a5: Pull complete
a1b8d4c4a5e6: Pull complete
Digest: sha256:abc123def456…
Status: Downloaded newer image for neo4j:5.26.8-community
docker.io/library/neo4j:5.26.8-community
# 查看镜像
# docker images | grep neo4j
neo4j 5.26.8-community abc123def456 2 weeks ago 600MB
步骤2:运行Neo4j容器
# mkdir -p /fgeudb/neo4j/{data,logs,import,plugins}
# 运行Neo4j容器
# docker run -d \
–name neo4j \
-p 7474:7474 \
-p 7473:7473 \
-p 7687:7687 \
-v /fgeudb/neo4j/data:/data \
-v /fgeudb/neo4j/logs:/logs \
-v /fgeudb/neo4j/import:/var/lib/neo4j/import \
-v /fgeudb/neo4j/plugins:/plugins \
-e NEO4J_AUTH=neo4j/YourSecurePassword123! \
-e NEO4J_server_memory_heap_max__size=8G \
-e NEO4J_server_memory_pagecache_size=2G \
neo4j:5.26.8-community
# 输出示例如下:
abc123def456789…
# 检查容器状态
# docker ps | grep neo4j
abc123def456 neo4j:5.26.8-community “tini -g — /sbin/do…” 10 seconds ago Up 9 seconds 0.0.0.0:7473-7474->7473-7474/tcp, 0.0.0.0:7687->7687/tcp neo4j
# 查看容器日志
# docker logs neo4j
# 输出示例如下:
2026-04-04 10:20:00.000+0000 INFO Starting…
2026-04-04 10:20:01.000+0000 INFO Bolt enabled on 0.0.0.0:7687.
2026-04-04 10:20:02.000+0000 INFO HTTP enabled on 0.0.0.0:7474.
2026-04-04 10:20:03.000+0000 INFO HTTPS enabled on 0.0.0.0:7473.
2026-04-04 10:20:05.000+0000 INFO Started.
7. Neo4j配置优化
以下是生产环境的Neo4j配置优化建议。
内存配置优化
# vi /fgeudb/neo4j/conf/neo4j.conf
# JVM内存配置
server.memory.heap.initial_size=8G
server.memory.heap.max_size=16G
server.memory.pagecache.size=4G
server.memory.off_heap.max_size=2G
# 事务日志配置
db.tx_log.rotation.retention_policy=2 days 1G size
db.tx_log.rotation.size=256M
# 检查点配置
db.checkpoint.iops.limit=1000
db.checkpoint.interval.time=10m
# 查询配置
dbms.cypher.default_planner=cost
dbms.cypher.runtime=pipelined
cypher.lenient_create_relationship=true
# 索引配置
db.index.default_background_provider=native-btree-1.0
# 并发配置
dbms.threads.worker_count=CPU核心数
# 保存配置文件
安全配置
# vi /fgeudb/neo4j/conf/neo4j.conf
# 认证配置
dbms.security.auth_enabled=true
dbms.security.auth_provider=native
# 密码策略
dbms.security.password_policy.minimum_length=12
dbms.security.password_policy.require_uppercase=true
dbms.security.password_policy.require_lowercase=true
dbms.security.password_policy.require_numbers=true
dbms.security.password_policy.require_special_characters=true
# SSL配置
dbms.ssl.policy.bolt.enabled=true
dbms.ssl.policy.bolt.base_directory=certificates/bolt
dbms.ssl.policy.bolt.private_key=private.key
dbms.ssl.policy.bolt.public_certificate=public.crt
dbms.ssl.policy.https.enabled=true
dbms.ssl.policy.https.base_directory=certificates/https
dbms.ssl.policy.https.private_key=private.key
dbms.ssl.policy.https.public_certificate=public.crt
# 保存配置文件
8. 安装验证与测试
安装完成后,需要进行验证测试确保Neo4j正常运行。
访问Neo4j Browser
# URL: http://192.168.1.51:7474/
# 首次登录需要修改密码
# 默认用户名: neo4j
# 默认密码: neo4j
# 新密码: YourSecurePassword123!
# Neo4j Browser功能:
# – Cypher查询编辑器
# – 图数据可视化
# – 数据库管理
# – 插件管理
# – 文档和教程
使用Cypher Shell连接
$ /fgeudb/neo4j/bin/cypher-shell -a bolt://192.168.1.51:7687 -u neo4j -p YourSecurePassword123!
# 输出示例如下:
Connected to Neo4j at bolt://192.168.1.51:7687 as user neo4j.
Type :help for a list of available commands or :exit to exit the shell.
Note that Cypher queries must end with a semicolon.
neo4j@neo4j>
# 查看版本
neo4j@neo4j> CALL dbms.components() YIELD name, versions, edition;
+————————————————————————+
| name | versions | edition |
+————————————————————————+
| “Neo4j” | [“5.26.8”, “5.26”] | “community” |
+————————————————————————+
# 查看数据库
neo4j@neo4j> SHOW DATABASES;
+———————————————————-+
| name | currentStatus | requestedStatus | default |
+———————————————————-+
| “neo4j” | “online” | “online” | true |
| “system”| “online” | “online” | false |
+———————————————————-+
创建图数据
neo4j@neo4j> CREATE (p:Person {name: ‘Zhang San’, age: 30, city: ‘Beijing’});
0 rows available after 50 ms, consumed after another 0 ms
Added 1 nodes, Set 3 properties, Added 1 labels
neo4j@neo4j> CREATE (p:Person {name: ‘Li Si’, age: 28, city: ‘Shanghai’});
0 rows available after 30 ms, consumed after another 0 ms
Added 1 nodes, Set 3 properties, Added 1 labels
neo4j@neo4j> CREATE (c:Company {name: ‘Tech Corp’, industry: ‘IT’});
0 rows available after 25 ms, consumed after another 0 ms
Added 1 nodes, Set 2 properties, Added 1 labels
# 创建关系
neo4j@neo4j> MATCH (p:Person {name: ‘Zhang San’}), (c:Company {name: ‘Tech Corp’})
CREATE (p)-[:WORKS_FOR {since: 2020}]->(c);
0 rows available after 100 ms, consumed after another 0 ms
Added 1 relationships, Set 1 properties
neo4j@neo4j> MATCH (p1:Person {name: ‘Zhang San’}), (p2:Person {name: ‘Li Si’})
CREATE (p1)-[:KNOWS {since: 2018}]->(p2);
0 rows available after 80 ms, consumed after another 0 ms
Added 1 relationships, Set 1 properties
# 查询节点
neo4j@neo4j> MATCH (p:Person) RETURN p;
+—————————————————————+
| p |
+—————————————————————+
| (:Person {name: “Zhang San”, age: 30, city: “Beijing”}) |
| (:Person {name: “Li Si”, age: 28, city: “Shanghai”}) |
+—————————————————————+
2 rows available after 50 ms, consumed after another 10 ms
# 查询关系
neo4j@neo4j> MATCH (p:Person {name: ‘Zhang San’})-[r]->(target)
RETURN type(r) AS relationship, target.name AS target_name;
+—————————————-+
| relationship | target_name |
+—————————————-+
| “WORKS_FOR” | “Tech Corp” |
| “KNOWS” | “Li Si” |
+—————————————-+
2 rows available after 30 ms, consumed after another 5 ms
# 查询路径
neo4j@neo4j> MATCH path = (p:Person {name: ‘Zhang San’})-[*1..2]-(target)
RETURN path;
+————————————————————————–+
| path |
+————————————————————————–+
| [Node[0]{name:”Zhang San”,age:30,city:”Beijing”},:WORKS_FOR,Node[2]{…}]|
| [Node[0]{name:”Zhang San”,age:30,city:”Beijing”},:KNOWS,Node[1]{…}] |
+————————————————————————–+
2 rows available after 40 ms, consumed after another 10 ms
# 退出
neo4j@neo4j> :exit
REST API测试
$ curl -X POST “http://192.168.1.51:7474/db/neo4j/tx/commit” \
-H “Content-Type: application/json” \
-u neo4j:YourSecurePassword123! \
-d ‘{
“statements”: [
{
“statement”: “MATCH (p:Person) RETURN p.name, p.age”
}
]
}’
# 输出示例如下:
{
“results”: [
{
“columns”: [“p.name”, “p.age”],
“data”: [
{“row”: [“Zhang San”, 30], “meta”: [null, null]},
{“row”: [“Li Si”, 28], “meta”: [null, null]}
]
}
],
“errors”: []
}
至此,Neo4j图数据库的下载和安装已完成。后续可以根据实际业务需求创建图模型、设计索引和优化查询。
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
