neo4j-shell 使用 笔记

Neo4j-shell是Neo4j数据库的命令行操作界面,(类似于MySQL的Command Line) 使用方便,操作简单。

步骤:

  1. 首先修改neo4j.conf配置文件,必须要修改配置后可以使用neo4j-shell。
  2. 启动Neo4j数据库。
  3. 使用 neo4j-shell

1.  修改neo4j.conf配置文件

1
2
3
4
5
6
7
8
9
10
11
# 修改233行,去掉前面的#,允许使用neo4j-shell,类似于mysql 命令行之类的
# Enable a remote shell server which Neo4j Shell clients can log in to.
dbms.shell.enabled=true

# 修改235行,去掉#,设置连接neo4j-shell的端口,一般都是localhost或者127.0.0.1,这样安全,其他地址的话,一般使用https就行
# The network interface IP the shell will listen on (use 0.0.0.0 for all interfaces).
dbms.shell.host=127.0.0.1

# 修改237行,去掉#,设置neo4j-shell端口,端口可以自定义,只要不和其他端口冲突就行
# The port the shell will listen on, default is 1337.
dbms.shell.port=1337

2 启动Neo4j数据库

neo4j start(linux) 或 neo4j console(windows) 启动Neo4j数据库

1
2
3
4
5
6
C:\ProfessionSofware\Neo4j\neo4j-community-3.1.2\bin
λ neo4j console
2017-05-03 08:04:41.297+0000 INFO Starting...
2017-05-03 08:04:43.361+0000 INFO Bolt enabled on 0.0.0.0:7687.
2017-05-03 08:04:50.026+0000 INFO Started.
2017-05-03 08:04:51.655+0000 INFO Remote interface available at http://0.0.0.0:7474/

3 使用neo4j-shell

输入 neo4j-shell 命令连接默认的Neo4j,进入neo4j-shell
默认是连接当前操作系统上已经启动的Neo4j数据库,
如果目前的操作系统上启动了两个Neo4j数据库,neo4j-shell会连其中一个(具体我也不知道会选择哪一个,血的教训,因为这个问题,有一次差点删库)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
C:\ProfessionSofware\Neo4j\neo4j-community-3.1.2\bin
λ neo4j-shell
Welcome to the Neo4j Shell! Enter 'help' for a list of commands. Please note that neo4j-shell is deprecated and to be replaced by cypher-shell.
NOTE: Remote Neo4j graph database service 'shell' at port 1337

neo4j-sh (?)$ help
Available commands: alias begin call cd commit create cypher dbinfo drop dump env explain export foreach gsh help index jsh load ls man match merge mknode mkrel mv optional paths planner profile pwd return rm rmnode rmrel rollback runtime schema set start trav unwind using with
Use man <command> for info about each command.
Please note that neo4j-shell is deprecated and to be replaced by cypher-shell.
neo4j-sh (?)$ match (n) return count(n);
+----------+
| count(n) |
+----------+
| 5 |
+----------+
1 row
93 ms
neo4j-sh (?)$

# 退出是exit
neo4j-sh (?)$ exit

C:\ProfessionSofware\Neo4j\neo4j-community-3.3.1\bin

连接指定ip host的Neo4j

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
λ neo4j-shell -host 172.16.6.88 -port 1337 -name shell
Welcome to the Neo4j Shell! Enter 'help' for a list of commands. Please note that neo4j-shell is deprecated and to be replaced by cypher-shell.
NOTE: Remote Neo4j graph database service 'shell' at port 1337

neo4j-sh (?)$ match (n) return count(n);
+----------+
| count(n) |
+----------+
| 0 |
+----------+
1 row
68 ms
neo4j-sh (?)$ match (n) return count(n);
+----------+
| count(n) |
+----------+
| 1 |
+----------+
1 row
14 ms
neo4j-sh (?)$

neo4j-shell帮助信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
C:\ProfessionSofware\Neo4j\neo4j-community-3.1.2\bin>neo4j-shell help
警告: This command does not appear to be running with administrative rights. Some commands may fail e.g. Start/Stop
ERROR (-v for expanded information):
Connection refused

-host Domain name or IP of host to connect to (default: localhost)
-port Port of host to connect to (default: 1337)
-name RMI name, i.e. rmi://<host>:<port>/<name> (default: shell)
-pid Process ID to connect to
-c Command line to execute. After executing it the shell exits
-file File containing commands to execute, or '-' to read from stdin. After executing it the shell exits
-readonly Connect in readonly mode (only for connecting with -path)
-path Points to a neo4j db path so that a local server can be started there
-config Points to a config file when starting a local server

Example arguments for remote:
-port 1337
-host 192.168.1.234 -port 1337 -name shell
-host localhost -readonly
...or no arguments for default values
Example arguments for local:
-path /path/to/db
-path /path/to/db -config /path/to/neo4j.config
-path /path/to/db -readonly

可以通过 neo4j-shell -host 172.16.11.123 -port 1337 -name shell 连接指定的数据库

批量执行cypher语句

要创建上百个索引,类似于关系型数据库中批量create,想找一个简单的办法

./bin/neo4j-shell -c < /data/stale/data01/neo4j/create_index.cypher

./bin/neo4j-shell -path /data/stale/data01/neo4j/neo4j-community-3.1.0/data/dbms/ -conf /data/stale/data01/neo4j/neo4j-community-3.1.0/conf/neoo4j.conf -file /data/stale/data01/neo4j/create_index.cypther