Neo4j ubuntu installation environment configuration python instance

Introduction to Neo4j

It is recommended to refer to my other article for a more detailed and comprehensive introduction and use of Neo4J Cypher neo4j-driver py2neo.
Neo4j is a Java-based graph database. To run Neo4j, you need to start the JVM process, so you must install the Java SE JDK.

The Neo4j application has the following main directory structure:

bin directory: used to store Neo4j executable programs;
conf directory: used to control Neo4j startup configuration files;
data directory: used to store core database files;
plugins directory: used to store Neo4j plugins.

Next, I mainly introduce the neo4j environment configuration under ubuntu.

Environment configuration

jdk1.8 installation

Check if the current server has a java environment

java -version

Insert picture description here
If the picture above is displayed, version 1.8 does not need to be installed.

If not, refer to the link

Neo4j installation

Download the installation package on the server

curl -O http://dist.neo4j.org/neo4j-community-3.4.5-unix.tar.gz

Unzip the installation package

tar -axvf neo4j-community-3.4.5-unix.tar.gz

Enter the unzipped directory

cd neo4j-community-3.4.5-unix

Modify the configuration file

vim conf/neo4j.conf

Insert picture description here

The following quote is from https://blog.csdn.net/u013946356/article/details/81736232
. The dbms.shell.host line number is modified. All the line numbers below are correct. You can directly escexit the editing mode and :linenumberdirectly locate the line to be modified.

# 修改第22行load csv时l路径,在前面加个#,可从任意路径读取文件
#dbms.directories.import=import

# 修改35行和36行,设置JVM初始堆内存和JVM最大堆内存
# 生产环境给的JVM最大堆内存越大越好,但是要小于机器的物理内存
dbms.memory.heap.initial_size=5g
dbms.memory.heap.max_size=10g

# 修改46行,可以认为这个是缓存,如果机器配置高,这个越大越好
dbms.memory.pagecache.size=10g

# 修改54行,去掉改行的#,可以远程通过ip访问neo4j数据库
dbms.connectors.default_listen_address=0.0.0.0

# 默认 bolt端口是7687,http端口是7474,https关口是7473,不修改下面3项也可以
# 修改71行,去掉#,设置http端口为7687,端口可以自定义,只要不和其他端口冲突就行
dbms.connector.bolt.listen_address=:7687

# 修改75行,去掉#,设置http端口为7474,端口可以自定义,只要不和其他端口冲突就行
dbms.connector.http.listen_address=:7474

# 修改79行,去掉#,设置http端口为7473,端口可以自定义,只要不和其他端口冲突就行
dbms.connector.https.listen_address=:7473

# 修改227行,去掉#,允许从远程url来load csv
dbms.security.allow_csv_import_from_file_urls=true

# 修改246行,允许使用neo4j-shell,类似于mysql 命令行之类的
dbms.shell.enabled=true

# 修改248行,去掉#,设置连接neo4j-shell的端口,一般都是localhost或者127.0.0.1,这样安全,其他地址的话,一般使用https就行
dbms.shell.host=127.0.0.1

# 修改250行,去掉#,设置neo4j-shell端口,端口可以自定义,只要不和其他端口冲突就行
dbms.shell.port=1337

# 修改254行,设置neo4j可读可写
dbms.read_only=false

Start, console, stop service

bin/neo4j start
bin/neo4j console
bin/neo4j stop

password

User name neo4j
Default passwordneo4j

Modify the default password from the command line

Enter the cypher-shell command line

bin/cypher-shell

Change the default password

CALL dbms changePassword('newneo4j');

Launch cypher-shell command line

:exit;

neo4j and neo4j-driver installation

pip install neo4j neo4j-driver==1.7.6

neo4j-driverThe version that must be specified here ,
refer to the link: from error neo4j.exceptions.ServiceUnavailable: The Neo4J server does not support
communication with this driver. This driver have support for Bolt
Protocols dict_keys([Version(3, 0), Version(4, 0) )])

Otherwise, an error will be reported as follows:

raise ServiceUnavailable(str(error)) from error
neo4j.exceptions.ServiceUnavailable: 
The Neo4J server does not support communication with this driver. 
This driver have support for Bolt Protocols dict_keys([Version(3, 0), Version(4, 0), Version(4, 1)])

python example


# step 1:导入 Neo4j 驱动包
from neo4j import GraphDatabase
# step 2:连接 Neo4j 图数据库
driver = GraphDatabase.driver("bolt://127.0.0.1:7687", auth=("neo4j", "newneo4j"))
# driver = GraphDatabase.driver("http://127.0.0.1:7474", auth=("neo4j", "neo4j"))

# 添加 关系 函数
def add_friend(tx, name, friend_name):
    tx.run("MERGE (a:Person {name: $name}) "
        "MERGE (a)-[:KNOWS]->(friend:Person {name: $friend_name})",
        name=name, friend_name=friend_name)
# 定义 关系函数
def print_friends(tx, name):
    for record in tx.run("MATCH (a:Person)-[:KNOWS]->(friend) WHERE a.name = $name "
                        "RETURN friend.name ORDER BY friend.name", name=name):
        print(record["friend.name"])

# step 3:运行
with driver.session() as session:
    session.write_transaction(add_friend, "Arthur", "Guinevere")
    session.write_transaction(add_friend, "Arthur", "Lancelot")
    session.write_transaction(add_friend, "Arthur", "Merlin")
    session.read_transaction(print_friends, "Arthur")
    

Output

Guinevere
Lancelot
Merlin

Reference link

  1. jdk1.8 ubuntu installation
  2. Introduction to neo4j
  3. neo4j ubuntu environment configuration

Guess you like

Origin blog.csdn.net/qq_32507417/article/details/112403721