neo4j是一個高性能的面向圖的數據庫,聽說是java世界「最受歡迎的圖數據庫」,雖然,圖數據庫自己也沒有幾種能夠選......。html
neo4j的高可用很容易搭建,本文使用的是neo4j-2.0.2版本,在其官方網站的manual中有很是詳細的例子,參考其manual的ch23,http://docs.neo4j.org/chunked/2.0.2/ha-setup-tutorial.html。其中提到了2個狀況下的例子,一個是真正的生產狀況下,採用多臺機器部署的狀況;另外一個是開發環境中,在一臺機器上啓動多個服務模擬生產的高可用。本文采用的是後一種,這次實驗依然是在ubuntu 64bit上進行。java
先去官網上下載neo4j的企業版,地址在:http://dist.neo4j.org/neo4j-enterprise-2.0.2-unix.tar.gz。因爲neo4j是用java寫的,因此,沒有JRE是不行的,安裝Java的過程在這裏略過,我用的是目前最新發布的java 8。node
XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2$ java -version java version "1.8.0" Java(TM) SE Runtime Environment (build 1.8.0-b132) Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)
下載以後解壓,須要用root用戶安裝neo4j的service,web
XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2$ sudo ./bin/neo4j-installer install WARNING: this installer is deprecated and may not be the optimal way to install Neo4j on your system. Please see the Neo4j Manual for up to date information on installing Neo4j. Press any key to continue Graph-like power should be handled carefully. What user should run Neo4j? [neo4j] XXXXX Adding system startup for /etc/init.d/neo4j-service ... /etc/rc0.d/K20neo4j-service -> ../init.d/neo4j-service /etc/rc1.d/K20neo4j-service -> ../init.d/neo4j-service /etc/rc6.d/K20neo4j-service -> ../init.d/neo4j-service /etc/rc2.d/S20neo4j-service -> ../init.d/neo4j-service /etc/rc3.d/S20neo4j-service -> ../init.d/neo4j-service /etc/rc4.d/S20neo4j-service -> ../init.d/neo4j-service /etc/rc5.d/S20neo4j-service -> ../init.d/neo4j-service
安裝的時候有一個提示很是重要,讓你選哪一個用戶是neo4j的主用戶,默認是neo4j,通常狀況下你係統裏面不會有這樣一個用戶的,因此必定要改成你經常使用的那個用戶。以後就能夠啓動neo4j數據庫
XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2$ service neo4j-service status Neo4j Server is not running XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2$ service neo4j-service start WARNING: Max 1024 open files allowed, minimum of 40 000 recommended. See the Neo4j manual. Using additional JVM arguments: -server -XX:+DisableExplicitGC -Dorg.neo4j.server.properties=conf/neo4j-server.properties -Djava.util.logging.config.file=conf/logging.properties -Dlog4j.configuration=file:conf/log4j.properties -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled Starting Neo4j Server...WARNING: not changing user process [14512]... waiting for server to be ready................ OK. http://localhost:7474/ is ready. XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2$ service neo4j-service status Neo4j Server is running at pid 14512 XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2$
啓動時會打印一些比較簡單的log,打印了進程號。ubuntu
若是要作高可用的話,須要多個neo4j的數據庫,把解壓的這個目錄複製2份,這樣就有3個內容同樣的目錄,用於高可用的配置。neo4j的高可用主要須要修改conf/neo4j.properties和conf/neo4j-server.properties這2個文件。節點1須要修改的配置以下,app
conf/neo4j.properties: # Unique server id for this Neo4j instance # can not be negative id and must be unique ha.server_id = 1 # IP and port for this instance to bind to for communicating data with the # other neo4j instances in the cluster. ha.server = 127.0.0.1:6363 online_backup_server = 127.0.0.1:6366 # IP and port for this instance to bind to for communicating cluster information # with the other neo4j instances in the cluster. ha.cluster_server = 127.0.0.1:5001 # List of other known instances in this cluster ha.initial_hosts = 127.0.0.1:5001,127.0.0.1:5002,127.0.0.1:5003 conf/neo4j-server.properties # database location org.neo4j.server.database.location=data/graph.db # http port (for all data, administrative, and UI access) org.neo4j.server.webserver.port=7474 # webserver IP bind org.neo4j.server.webserver.address=0.0.0.0 # https port (for all data, administrative, and UI access) org.neo4j.server.webserver.https.port=7484 # HA - High Availability # SINGLE - Single mode, default. org.neo4j.server.database.mode=HA
neo4j中有一個關於IP的配置,0.0.0.0,表示neo4j須要監聽該機器上的全部ip,通常對neo4j提供的web管理功能,都會採用這種方式,即配置org.neo4j.server.webserver.address這個參數。性能
節點2和節點3上的配置比較相似,只列出節點2的配置,以下,網站
conf/neo4j.properties: # Unique server id for this Neo4j instance # can not be negative id and must be unique ha.server_id = 2 # IP and port for this instance to bind to for communicating data with the # other neo4j instances in the cluster. ha.server = 127.0.0.1:6364 online_backup_server = 127.0.0.1:6367 # IP and port for this instance to bind to for communicating cluster information # with the other neo4j instances in the cluster. ha.cluster_server = 127.0.0.1:5002 # List of other known instances in this cluster ha.initial_hosts = 127.0.0.1:5001,127.0.0.1:5002,127.0.0.1:5003 conf/neo4j-server.properties # database location org.neo4j.server.database.location=data/graph.db # http port (for all data, administrative, and UI access) org.neo4j.server.webserver.port=7475 # webserver IP bind org.neo4j.server.webserver.address=0.0.0.0 # https port (for all data, administrative, and UI access) org.neo4j.server.webserver.https.port=7485 # HA - High Availability # SINGLE - Single mode, default. org.neo4j.server.database.mode=HA
配置完成以後,就能夠啓動包含3個neo4j節點的HA服務。啓動步驟以下ui
XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2$ ./bin/neo4j start WARNING: Max 1024 open files allowed, minimum of 40 000 recommended. See the Neo4j manual. Using additional JVM arguments: -server -XX:+DisableExplicitGC -Dorg.neo4j.server.properties=conf/neo4j-server.properties -Djava.util.logging.config.file=conf/logging.properties -Dlog4j.configuration=file:conf/log4j.properties -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled Starting Neo4j Server...WARNING: not changing user HA instance started in process [20002]. Will be operational once connected to peers. See /home/XXXXX/neo4j-enterprise-2.0.2/data/log/console.log for current status. XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2$ cd ../neo4j-enterprise-2.0.2-node2/ XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2-node2$ ./bin/neo4j start WARNING: Max 1024 open files allowed, minimum of 40 000 recommended. See the Neo4j manual. Using additional JVM arguments: -server -XX:+DisableExplicitGC -Dorg.neo4j.server.properties=conf/neo4j-server.properties -Djava.util.logging.config.file=conf/logging.properties -Dlog4j.configuration=file:conf/log4j.properties -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled Starting Neo4j Server...WARNING: not changing user HA instance started in process [22238]. Will be operational once connected to peers. See /home/XXXXX/neo4j-enterprise-2.0.2-node2/data/log/console.log for current status. XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2-node2$ cd ../neo4j-enterprise-2.0.2-node3/ XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2-node3$ ./bin/neo4j start WARNING: Max 1024 open files allowed, minimum of 40 000 recommended. See the Neo4j manual. Using additional JVM arguments: -server -XX:+DisableExplicitGC -Dorg.neo4j.server.properties=conf/neo4j-server.properties -Djava.util.logging.config.file=conf/logging.properties -Dlog4j.configuration=file:conf/log4j.properties -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled Starting Neo4j Server...WARNING: not changing user HA instance started in process [22605]. Will be operational once connected to peers. See /home/XXXXX/neo4j-enterprise-2.0.2-node3/data/log/console.log for current status. XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2-node3$ jps 20002 Bootstrapper 22238 Bootstrapper 22605 Bootstrapper 23805 Jps XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2-node3$
最後用jps查看進程,發現有3個Bootstrapper進程,這個就是neo4j節點的進程。啓動完成以後,能夠用web方式登錄http://localhost:7474/webadmin/進行管理,若是直接登錄http://localhost:7474/返回的是http://localhost:7474/browser/,這個頁面中能作的事情就不多,只能夠運行一些簡單的查詢。
webadmin頁面有5個標籤,以下圖
在最後一個標籤「Server Info」中有一項「High Availability」,能夠查看HA運行的信息,如下是節點1的信息,
在第3個標籤「Console」中能夠敲入Cypher(neo4j的查詢語言),用於管理。
咱們能夠在節點1中使用Cypher插入數據,
neo4j-sh (?)$ CREATE (ee { name: "Emil", from: "Sweden" }) RETURN ee.name; ==> +---------+ ==> | ee.name | ==> +---------+ ==> | "Emil" | ==> +---------+ ==> 1 row ==> Nodes created: 1 ==> Properties set: 2 ==> 847 ms neo4j-sh (?)$ START ee=node(*) WHERE ee.name! = "Emil" RETURN ee; ==> SyntaxException: This syntax is no longer supported (missing properties are now returned as null). (line 1, column 27) ==> "START ee=node(*) WHERE ee.name! = "Emil" RETURN ee" ==> ^ neo4j-sh (?)$ START a = node(0) RETURN a; ==> +------------------------------------+ ==> | a | ==> +------------------------------------+ ==> | Node[0]{name:"Emil",from:"Sweden"} | ==> +------------------------------------+ ==> 1 row ==> 38 ms
而後再去節點2或者節點3上查詢,
neo4j-sh (?)$ START a = node(0) RETURN a; ==> +------------------------------------+ ==> | a | ==> +------------------------------------+ ==> | Node[0]{name:"Emil",from:"Sweden"} | ==> +------------------------------------+ ==> 1 row ==> 339 ms neo4j-sh (?)$
發現,數據已經由節點1同步到節點3,說明,HA已經在正常的工做了。