想要运行简单测试一下neo4j,就直接使用docker创建了一个容器,并用cypher-shell本地连接neo4j,创建图进行测试。
1 开启docker
sudo systemctl start docker
2 拉取镜像源
sudo docker pull neo4j # 默认latest版本
3 查看本地镜像,检验是否拉取成功
sudo docker images
4 创建容器并运行
在/home/neo4j目录下分别创建 data、logs、conf、import 四个目录
- data,数据存放的文件夹
- logs,运行的日志文件夹
- conf,数据库配置文件夹
- import,为了大批量导入csv来构建数据库,只能导入.csv格式的文件,要放到这个文件夹下
sudo docker run -d --name container_name -p 27474:7474 -p 27687:7687 -v /home/neo4j/data:/data -v /home/neo4j/logs:/logs -v /home/neo4j/conf:/var/lib/neo4j/conf -v /home/neo4j/import:/var/lib/neo4j/import --env NEO4J_AUTH=neo4j/password neo4j
-d --name container_name //-d表示容器后台运行 --name指定容器名字
-p 27474:7474 -p 27687:7687 //映射容器的端口号到宿主机的端口号;27474 为宿主机端口
-v /home/neo4j/data:/data //把容器内的数据目录挂载到宿主机的对应目录下
-v /home/neo4j/logs:/logs //挂载日志目录
-v /home/neo4j/conf:/var/lib/neo4j/conf //挂载配置目录
-v /home/neo4j/import:/var/lib/neo4j/import //挂载数据导入目录
--env NEO4J_AUTH=neo4j/password //设定数据库的名字的访问密码
neo4j //指定使用的镜像
5 检查容器是否构建成功
sudo docker ps -a
6 进入到容器内部
sudo docker exec -it 容器id bash
# 例如:sudo docker exec -it ea6ecd2e43ac bash
7 运行客户端cypher-shell
./bin/cypher-shell
8 cypher图创建,图查询
neo4j@neo4j> CREATE (node1:Router {ip:'127.0.0.1'}),(node2:Router {ip:'127.0.0.2'}),(node3:Router {ip:'127.0.0.3'}),(node4:Router {ip:'127.0.0.4'}),(node5:Router {ip:'127.0.0.5'}),(node6:Router {ip:'127.0.0.6'}),(node7:Router {ip:'127.0.0.7'}),(node8:Router {ip:'127.0.0.8'}),(node9:Router {ip:'127.0.0.9'}),(node1)-[:Connect]->(node4),(node2)-[:Connect]->(node5),(node3)-[:Connect]->(node6),(node4)-[:Connect]->(node7),(node4)-[:Connect]->(node8),(node5)-[:Connect]->(node8),(node6)-[:Connect]->(node9);
0 rows
ready to start consuming query after 412 ms, results consumed after another 0 ms
Added 9 nodes, Created 7 relationships, Set 9 properties, Added 9 labels
neo4j@neo4j>
neo4j@neo4j>
neo4j@neo4j> MATCH (r:Router)-[:Connect]->(t:Router)-[:Connect]->(p:Router) WHERE r.ip >= '127.0.0.1' AND r.ip <= '127.0.0.3' RETURN t.ip, p.ip;
+---------------------------+
| t.ip | p.ip |
+---------------------------+
| "127.0.0.4" | "127.0.0.8" |
| "127.0.0.4" | "127.0.0.7" |
| "127.0.0.5" | "127.0.0.8" |
| "127.0.0.6" | "127.0.0.9" |
+---------------------------+4 rows
ready to start consuming query after 422 ms, results consumed after another 12 ms
9 退出cypher-shell和容器
:exit # 退出cypher-shell
exit # 退出容器
10 容器停止运行
sudo docker stop 容器id