Neo4j 是一个原生图数据库,这意味着它在存储层实现了真正的图模型。它不是在其他技术之上使用"图抽象",而是以您在白板上绘制想法的相同方式在Neo4j中存储数据。
自2007年以来,Neo4j已经发展成为一个丰富的工具、应用程序和库的生态系统。该生态系统允许您以多种方式将图技术与您的工作环境集成,
除了核心图之外,Neo4j还提供ACID事务、集群支持和运行时故障转移。
Neo4j 使用Java和Scala编写。可以在GitHub上查看源代码:https://github.com/neo4j/neo4j
中文教程: https://neo4j.com.cn/
下载安装
下载页面:Neo4j Deployment Center - Graph Database & Analytics
可以选择:Graph Database Self-Managed(图形数据库自我管理 )选社区版下载,也可以下载Ubuntu 包文件,或者下载桌面版,比如Windows安装版文件进行安装。
下载Windows安装版安装
在下载页面选中windows桌面版,第一次需要注册信息,点击后会自动下载,并给Neo4j Desktop Activation Key
将key保存好。当然国内的ip可能无法下载。
或者点击Neo4j的:Download your free copy of O'Reilly's Graph Databases 社区版
显示:
Your download should begin automatically. Click here if it doesn't.
Default login is username 'neo4j' and password 'neo4j' (full installation instructions below)
想办法下载neo4j-community-2025.03.0-windows后,解压后是一个目录,到目录的bin子目录,然后执行
E:\360Downloads\neo4j-community-2025.03.0-windows\bin>neo4j.bat console
在FreeBSD下安装neo4j
pkg search neo4j
neo4j-4.4.35 High performance graph store and database
安装
sudo pkg install neo4j
启动服务
# 启动服务(命令行)
neo4j console
启动后,会显示网页链接地址:
2025-04-22 14:03:23.617+0000 INFO Remote interface available at http://localhost:7474/
为了非本地主机也能控制,可以修改配置文件,比如FreeBSD的配置文件在:/usr/local/etc/neo4j.conf:
找到这句:#dbms.default_listen_address=0.0.0.0
把注释去掉即可。
这样启动后就可以远程访问了,比如:
http://192.168.0.109:7474/browser/
连上网页控制台
启动后,访问
http://192.168.0.109:7474/browser/
连上控制页面:

默认用户和密码是neo4j,连上后会提示设置新密码,设好新密码后,就进入了管理页面。
在这个页面,可以neoj4$提示符后,输入运行的语句:
// 创建电影图谱
CREATE (m:Movie {title:"黑客帝国", year:1999})
CREATE (a:Person {name:"基努·里维斯"})
CREATE (a)-[:ACTED_IN {role:"Neo"}]->(m)
然后输入如下三句进行查询:
// 查询出演过某电影的演员
MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)
WHERE m.title = "黑客帝国"
RETURN p.name, r.role
查询结果:
| p.name | r.role |
|--------|----------|-------|
| 1 | "基努·里维斯" | "Neo" |
Windows下将 Neo4j 安装为 Windows 服务
-
在命令行中运行以下命令将 Neo4j 安装为 Windows 服务:
neo4j.bat install-service
-
安装成功后,可以使用以下命令启动、停止、重启和查询 Neo4j 服务的状态:
neo4j.bat start neo4j.bat stop neo4j.bat restart neo4j.bat status
-
这样设置后,Neo4j 会在 Windows 启动时自动运行。
配置
Neo4j 的配置文件位于 conf
文件夹中的 neo4j.conf
文件。Windows就是解压缩目录所在的conf目录,FreeBSD就是/usr/local/etc 目录。
去掉neo4j.conf文件这句的注释就能绑定到0.0.0.0监听:
dbms.default_listen_address=0.0.0.0
修改监听端口号
修改server.http.listen_address的值来修改端口号
#server.http.listen_address=:7474
技术学习:
Neo4j 图数据库将数据存储为节点 、关系 和属性,而不是存储在表或文档中。这意味着您可以像在白板上绘制想法一样组织数据。
Neo4j 目前被各种行业的初创公司 、教育机构 和大型企业广泛使用,包括金融服务、政府、能源、技术、零售和制造业。图在帮助他们开发创新新技术、业务管理、洞察力和收入再生以及整体效率提升方面取得了成功。
什么是什么是 Cypher
Cypher 是 Neo4j 的声明式和 GQL 兼容的查询语言。Cypher 通过 openCypher 项目以开源形式提供,类似于 SQL,但针对图进行了优化。
Cypher 直观且接近自然语言,通过基于 ASCII 艺术类型语法的自身设计,提供了一种可视化匹配模式和关系的方式:
(:nodes)-[:ARE_CONNECTED_TO]->(:otherNodes)
圆括号用于表示 (:Nodes)
,而 -[:ARROWS]->
用于表示 (:Nodes)
之间的关系。使用此查询语法,您可以对图执行创建、读取、更新或删除 (CRUD) 操作。
模式
Neo4j 的属性图由节点和关系组成,它们都可以具有属性。节点表示实体,例如概念、事件、地点和事物。关系连接成对的节点。
但是,节点和关系可以被认为是低级构建块。属性图的真正优势在于它能够编码连接的节点和关系的模式。单个节点或关系通常编码的信息很少,但是节点和关系的模式可以编码任意复杂的想法。
Cypher®(Neo4j 的查询语言)强烈基于模式。具体来说,模式用于匹配所需的图结构。一旦找到或创建了匹配的结构,Neo4j 就可以使用它进行进一步处理。
一个简单的模式(只有一个关系)连接一对节点(或者,偶尔,一个节点连接到自身)。例如,一个人 LIVES_IN
一个城市,或者一个城市是 PART_OF
一个国家。
使用多个关系的复杂模式可以表达任意复杂的概念,并支持各种有趣的用例。例如,我们可能想要匹配一个人 LIVES_IN
一个国家的实例。以下 Cypher 代码将两个简单的模式组合成一个稍微复杂的模式,该模式执行此匹配:
(:Person) -[:LIVES_IN]-> (:City) -[:PART_OF]-> (:Country)
节点语法
Cypher 使用一对括号来表示一个节点:()
。这让人联想到一个圆形或一个带有圆形端盖的矩形。
关系语法
Cypher 使用一对破折号 (--
) 来表示无向关系。有向关系在一端有一个箭头 (<--
,-->
)。带括号的表达式 ([...]
) 可用于添加详细信息。
模式语法
结合节点和关系的语法,我们可以表达模式。以下可能是此域中的一个简单模式(或事实):
(keanu:Person:Actor {name: 'Keanu Reeves'})-[role:ACTED_IN {roles: ['Neo']}]->(matrix:Movie {title: 'The Matrix'})
与节点标签等效,:ACTED_IN
模式声明关系的类型。变量(例如,role
)可以在语句中的其他地方使用以引用该关系。
模式变量
为了提高模块化并减少重复,Cypher 允许将模式分配给变量。这允许检查匹配的路径,在其他表达式中使用等。
acted_in = (:Person)-[:ACTED_IN]->(:Movie)
acted_in
变量将包含每个找到或创建的路径的两个节点和连接关系。有许多函数可以访问路径的详细信息,例如:nodes(path)
、relationships(path)
和 length(path)
。
子句
Cypher 语句通常具有多个子句,每个子句执行一个特定任务,例如:
- 在图中创建和匹配模式
- 过滤、投影、排序或分页结果
- 组成部分语句
通过组合 Cypher 子句,您可以组成复杂的语句,表达您想要知道或创建的内容。
关于桌面版的官方的文档
Launch
Step 1: Activation
Copy and Paste the activation at the top of this page in the "Activation Key" box in the Neo4j Desktop app.
Alternatively, you can also generate a key from within the app by filling out the form on the right side of the app screen.
Step 2: Create a database
After activation, click on the "New Graph" button. Select "Create a local graph" from the options presented.
Next, enter the "Database name" and "Password" in field and click on the "Create" button.
Step 3: Start the database
After the database is created, click on the "Start" button.
Open the Neo4j Browser
Step 1: Start Neo4j Browser
After the database starts, click on the "Manage" button.
On the next screen, locate "Open Browser" on top of the screen and click on it. This will open Neo4j Browser in a new window.
Learn more about how to use Neo4j Browser here.
Step 2: Explore sample dataset
Neo4j Desktop comes with two sample datasets. You can run them using the following commands
:play movie graph
and :play northwind graph
Learn more about how to use Neo4j Browser here.
调试
下载报错:The Amazon CloudFront distribution is configured to block access from your country
被禁了?
好消息是FreeBSD里有。
在FreeBSD下安装neo4j
pkg search neo4j
neo4j-4.4.35 High performance graph store and database
安装
sudo pkg install neo4j
安装完毕显示:
Message from openjdk11-11.0.26+4.1_1:
--
This OpenJDK implementation may require fdescfs(5) mounted on /dev/fd
and procfs(5) mounted on /proc for some applications.
If you have not done it yet, please do the following:
mount -t fdescfs fdesc /dev/fd
mount -t procfs proc /proc
To make it permanent, you need the following lines in /etc/fstab:
fdesc /dev/fd fdescfs rw 0 0
proc /proc procfs rw 0 0
=====
Message from neo4j-4.4.35:
--
===> NOTICE:
The neo4j port currently does not have a maintainer. As a result, it is
more likely to have unresolved issues, not be up-to-date, or even be removed in
the future. To volunteer to maintain this port, please create an issue at:
https://bugs.freebsd.org/bugzilla
More information about port maintainership is available at:
https://docs.freebsd.org/en/articles/contributing/#ports-contributing
neo4j启动报错:Failed to write PID file: Access denied at /var/run/neo4j.pid
使用管理员账户启动
neo4j console
启动后显示:
Directories in use:
home: /usr/local/neo4j
config: /usr/local/etc
logs: /var/log/neo4j
plugins: /usr/local/neo4j/plugins
import: /usr/local/neo4j/import
data: /var/db/neo4j
certificates: /usr/local/neo4j/certificates
licenses: /usr/local/neo4j/licenses
run: /var/run
Starting Neo4j.
......
2025-04-22 14:03:23.617+0000 INFO Remote interface available at http://localhost:7474/
2025-04-22 14:03:23.635+0000 INFO id: E631C7E152FA51F51F15CBC9797D6C2D30A5D3C627B6433DF27CF7911201DA13
2025-04-22 14:03:23.635+0000 INFO name: system
node4j启动后只能本地打开
打开配置文件/usr/local/etc/neo4j.conf:
找到这句:#dbms.default_listen_address=0.0.0.0
把注释去掉即可。
这样启动后就可以远程访问了,比如:
http://192.168.0.109:7474/browser/
下载了neo4j-community-2025.03.0-windows ,解压后是一个目录,应该怎么启动?
到目录的bin子目录,然后执行
E:\360Downloads\neo4j-community-2025.03.0-windows\bin>neo4j.bat console
windows下执行neo4j.bat console报错
neo4j.bat console
: org.neo4j.server.startup.Neo4jCommand ʱ LinkageError
java.lang.UnsupportedClassVersionError: org/neo4j/server/startup/Neo4jCommand has been compiled by a more recent version of the Java Runtime (class file version 65.0), this version of the Java Runtime only recognizes class file versions up to 55.0
需要升级java,算了,就先用FreeBSD版本实践了。