第 17 章 Neo4j 服务器

优质
小牛编辑
134浏览
2023-12-01

17.1. 服务器安装

Neo4j 可以作为运行作为无头的应用程序或系统服务的服务器安装.

1.Download the latest release from http://neo4j.org/download

2.选择适当的版本,您的平台

3.归档文件的内容解压

- refer to the top-level extracted directory as NEO4J_HOME

4.在 bin 目录中使用的脚本

- for Linux/MacOS, run $NEO4J_HOME/bin/neo4j start

- for Windows, double-click on %NEO4J_HOME%\bin\Neo4j.bat

5.请参阅详细信息的文档目录中的打包信息

高可用性的信息,请参阅第 22 章 高可用性模式.

17.1.1. As a Windows service

具有管理权限,可以作为 Windows 服务安装 Neo4j.

1.单击开始 → 所有程序 → 附件

2.右键单击命令提示符 → 以管理员身份运行

3.提供授权和/或管理员密码

4.Navigate to %NEO4J_HOME%

5.Run bin\Neo4j.bat install

To uninstall, run bin\Neo4j.bat removeas Administrator.

To query the status of the service, run bin\Neo4j.bat status

To start the service from the command prompt, run bin\Neo4j.bat start

To stop the service from the command prompt, run bin\Neo4j.bat stop

注意

一些用户报告问题在 Windows 上的使用 ZoneAlarm 防火墙时。如果您有问题从服务器获取大的反应,或如果 Webadmin 不工作,请尝试禁用 ZoneAlarm。请联系 ZoneAlarm 支持,以获取有关如何解决此问题的信息.

17.1.2. Linux Service

Neo4j can participate in the normal system startup and shutdown process. The following procedure should work on most popular Linux distributions:

1.cd $NEO4J_HOME

2.sudo ./bin/neo4j install

if asked, enter your password to gain super-user privileges

3.service neo4j-service status

should indicate that the server is not running

4.service neo4j-service start

will start the server

During installation you will be given the option to select the user Neo4j will run as. You will be asked to supply a username (defaulting to neo4j) and if that user is not present on the system it will be created as a system account and the $NEO4J_HOME/datadirectory will be chown'ed to that user.

You are encouraged to create a dedicated user for running the service and for that reason it is suggested that you unpack the distribution package under /optor your site specific optional packages directory.

After installation you may have to do some platform specific configuration and performance tuning. For that, refer to 第 21.11 节 “Linux特有的注意事项”.

Finally, note that if you chose to create a new user account, on uninstall you will be prompted to remove it from the system.

17.1.4. Multiple Server instances on one machine

Neo4j can be set up to run as several instances on one machine, providing for instance several databases for development. To configure, install two instances of the Neo4j Server in two different directories following the steps outlined below.

First instance

First, create a directory to hold both database instances, and unpack the development instance:

1.cd $INSTANCE_ROOT

2.mkdir -p neo4j

3.cd neo4j

4.tar -xvzf /path/to/neo4j-community.tar.gz

5.mv neo4j-community dev

Next, configure the instance by changing the following values in dev/conf/neo4j-server.properties, see even 第 24.1 节 “安全访问Neo4j服务器”:

1

2

3

4

org.neo4j.server.webserver.port=7474

# Uncomment the following if the instance will be accessed from a host other than localhost.

org.neo4j.server.webserver.address=0.0.0.0

Before running the Windows install or startup, change in dev/conf/neo4j-wrapper.properties

1

2

# Name of the service for the first instance

wrapper.name=neo4j_1

Start the instance:

dev/bin/neo4j start

Check that instance is available by browsing to http://localhost:7474/webadmin/

Second instance (testing, development)

In many cases during application development, it is desirable to have one development database set up, and another against which to run unit tests. For the following example, we are assuming that both databases will run on the same host.

Now create the unit testing second instance:

1.cd $INSTANCE_ROOT/neo4j

2.tar -xvzf /path/to/neo4j-community.tar.gz

3.mv neo4j-community test

Next, configure the instance by changing the following values in test/conf/neo4j-server.propertiesto

- change the server port to 7475

1

2

3

4

5

# Note the different port number from the development instance

org.neo4j.server.webserver.port=7475

# Uncomment the following if the instance will be accessed from a host other than localhost

org.neo4j.server.webserver.address=0.0.0.0

Differentiate the instance from the development instance by modifying test/conf/neo4j-wrapper.properties.

1

wrapper.name=neo4j-test

On Windows, you even need to change the name of the service in bin\neo4j.batto be able to run it together with the first instance.

1

2

setserviceName=Neo4j-Server-test

setserviceDisplayName=Neo4j-Server-test

Start the instance:

test/bin/neo4j start

Check that instance is available by browsing to http://localhost:7475/webadmin/

17.2. 服务器配置

快速浏览信息

- The server’s primary configuration file is found under conf/neo4j-server.properties

- The conf/log4j.propertiesfile contains the default server logging configuration

- Low-level performance tuning parameters are found in conf/neo4j.properties

- Configuraion of the deamonizing wrapper are found in conf/neo4j-wrapper.properties

- HTTP logging configuration is found in conf/neo4j-http-logging.xml

17.2.1. 重要的服务器配置参数

主配置文件的服务器可以发现在 conf/neo4j-server.properties。此文件包含几个重要的设置,并且虽然默认设置是明智管理员可能会选择进行更改 (特别是对端口设置).

Set the location on disk of the database directory like this:

1

org.neo4j.server.database.location=data/graph.db

注意

On Windows systems, absolute locations including drive letters need to read "c:/data/db".

Specify the HTTP server port supporting data, administrative, and UI access:

1

org.neo4j.server.webserver.port=7474

Specify the client accept pattern for the webserver (default is 127.0.0.1, localhost only):

1

2

#allow any client to connect

org.neo4j.server.webserver.address=0.0.0.0

For securing the Neo4j Server, see also security-server

Set the location of the round-robin database directory which gathers metrics on the running server instance:

1

org.neo4j.server.webadmin.rrdb.location=data/graph.db/../rrd

Set the URI path for the REST data API through which the database is accessed. This should be a relative path.

1

org.neo4j.server.webadmin.data.uri=/db/data/

Setting the management URI for the administration API that the Webadmin tool uses. This should be a relative path.

1

org.neo4j.server.webadmin.management.uri=/db/manage

Force the server to use IPv4 network addresses, in conf/neo4j-wrapper.confunder the section Java Additional Parametersadd a new paramter:

1

wrapper.java.additional.3=-Djava.net.preferIPv4Stack=true

Low-level performance tuning parameters can be explicitly set by referring to the following property:

1

org.neo4j.server.db.tuning.properties=neo4j.properties

If this property isn’t set, the server will look for a file called neo4j.propertiesin the same directory as the neo4j-server.propertiesfile.

If this property isn’t set, and there is no neo4j.propertiesfile in the default configuration directory, then the server will log a warning. Subsequently at runtime the database engine will attempt tune itself based on the prevailing conditions.

17.2.2. Neo4j数据库性能配置

The fine-tuning of the low-level Neo4j graph database engine is specified in a separate properties file, conf/neo4j.properties.

图数据库引擎具有一系列性能优化选项,列举服务器性能。请注意应考虑其他因素比 Neo4j 优化,优化服务器,虽然包括一般服务器负载、 内存和文件争用和 JVM 的刑罚甚至垃圾收集这种考虑性能时超出了此配置文件的范围.

17.2.3. 服务器日志配置

Application events within Neo4j server are processed with java.util.loggingand configured in the file conf/logging.properties.

By default it is setup to print INFOlevel messages both on screen and in a rolling file in data/log. Most deployments will choose to use their own configuration here to meet local standards. During development, much useful information can be found in the logs so some form of logging to disk is well worth keeping. On the other hand, if you want to completely silence the console output, set:

1

java.util.logging.ConsoleHandler.level=OFF

By default log files are rotated at approximately 10Mb and named consecutively neo4j.<id>.<rotation sequence #>.log To change the naming scheme, rotation frequency and backlog size modify

1

2

3

java.util.logging.FileHandler.pattern

java.util.logging.FileHandler.limit

java.util.logging.FileHandler.count

respectively to your needs. Details are available at the Javadoc for java.util.logging.FileHandler.

Apart from log statements originating from the Neo4j server, other libraries report their messages through various frameworks.

Zookeeper is hardwired to use the log4j logging framework. The bundled conf/log4j.propertiesapplies for this use only and uses a rolling appender and outputs logs by default to the data/logdirectory.

17.2.4. HTTP日志配置

As well as logging events happening within the Neo4j server, it is possible to log the HTTP requests and responses that the server consumes and produces. Configuring HTTP logging requires operators to enable and configure the logger and where it will log; and then to optionally configure the log format.

警告

By default the HTTP logger uses Common Log Formatmeaning that most Web server tooling can automtically consume such logs. In general users should only enable HTTP logging, select an output directory, and if necessary alter the rollover and retention policies.

To enable HTTP logging, edit the conf/neo4j-server.propertiesfile resemble the following:

1

2

org.neo4j.server.http.log.enabled=true

org.neo4j.server.http.log.config=conf/neo4j-http-logging.xml

org.neo4j.server.http.log.enabled=truetells the server that HTTP logging is enabled. HTTP logging can be totally disabled by setting this property to false. org.neo4j.server.http.log.config=conf/neo4j-http-logging.xmlspecifies the logging format and rollover policy file that governs how HTTP log output is presented and archived. The defaults provided with Neo4j server uses an hourly log rotation and Common Log Format.

If logging is set up to use log files then the server will check that the log file directory exists and is writable. If this check fails, then the server will not startup and wil report the failure another available channel like standard out.

17.2.5. 其他配置选项

启用来自垃圾收集器日志记录

To get garbage collection logging output you have to pass the corresponding option to the server JVM executable by setting in conf/neo4j-wrapper.confthe value

1

wrapper.java.additional.3=-Xloggc:data/log/neo4j-gc.log

This line is already present and needs uncommenting. Note also that logging is not directed to console ; You will find the logging statements in data/log/ne4j-gc.logor whatever directory you set at the option.

在Webadmin中禁用控制台类型

You may, for security reasons, want to disable the Gremlin console and/or the Neo4j Shell in Webadmin. Both of them allow arbitrary code execution, and so they could constitute a security risk if you do not trust all users of your Neo4j Server.

In the conf/neo4j-server.propertiesfile:

1

2

3

4

5

6

7

8

# To disable both Neo4j Shell and Gremlin:

org.neo4j.server.manage.console_engines=

# To enable only the Neo4j Shell:

org.neo4j.server.manage.console_engines=shell

# To enable both

org.neo4j.server.manage.console_engines=gremlin,shell

17.3. 设置远程调试

In order to configure the Neo4j server for remote debugging sessions, the Java debugging parameters need to be passed to the Java process through the configuration. They live in the conf/neo4j-wrapper.propertiesfile.

In order to specify the parameters, add a line for the additional Java arguments like this:

1

2

3

4

# Java Additional Parameters

wrapper.java.additional.1=-Dorg.neo4j.server.properties=conf/neo4j-server.properties

wrapper.java.additional.2=-Dlog4j.configuration=file:conf/log4j.properties

wrapper.java.additional.3=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -Xdebug-Xnoagent-Djava.compiler=NONE-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005

This configuration will start a Neo4j server ready for remote debugging attachement at localhost and port 5005. Use these parameters to attach to the process from Eclipse, IntelliJ or your remote debugger of choice after starting the server.

Neo4j简体中文手册 > 参考 > Neo4j服务器 > 使用Neo4j服务器(带Web管理控制台)连接一个Neo4j嵌入数据库上一页 下一页

--------------------------------------------------------------------------------

17.4. 使用Neo4j服务器(带Web管理控制台)连接一个Neo4j嵌入数据库17.4.1. Getting the libraries

17.4.2. Starting the Server from Java

17.4.3. Providing custom configuration

Even if you are using the Neo4j Java API directly, for instance via EmbeddedGraphDatabase or HighlyAvailableGraphDatabase, you can still use the features the server provides.

17.4.1. Getting the librariesFrom the Neo4j Server installationTo run the server all the libraries you need are in the system/lib/ directory of the download package. For further instructions, see 第 4.1 节 “将Neo4j引入到你的项目工程中”. The only difference to the embedded setup is that system/lib/ should be added as well, not only the lib/ directory.

Via MavenFor users of dependency management, an example for Apache Maven follows. Note that the web resources are in a different artifact.

Maven pom.xml snippet.

1234567891011121314151617181920212223242526 <dependencies> <dependency> <groupId>org.neo4j.app</groupId> <artifactId>neo4j-server</artifactId> <version>1.8</version> </dependency> <dependency> <groupId>org.neo4j.app</groupId> <artifactId>neo4j-server</artifactId> <classifier>static-web</classifier> <version>1.8</version> </dependency> </dependencies> <repositories> <repository> <id>neo4j-snapshot-repository</id> <name>Neo4j Maven 2 snapshot repository</name> <url>http://m2.neo4j.org/content/repositories/snapshots/</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories>

Via Scala SBT / IvyIn order to pull in the dependencys with SBT and configure the underlying Ivy dependency manager, you can use a setup like the following in your build.sbt:

1234567891011121314151617 organization := "your.org" name := "your.name" version := "your.version" /** Deps for Embedding the Neo4j Admin server. */libraryDependencies ++= Seq( "org.neo4j.app" % "neo4j-server" % "1.8" classifier "static-web" classifier "", "com.sun.jersey" % "jersey-core" % "1.9") /** Repos for Neo4j Admin server dep */resolvers ++= Seq( "maven-central" at "http://repo1.maven.org/maven2", "neo4j-public-repository" at "http://m2.neo4j.org/content/groups/public")

17.4.2. Starting the Server from JavaThe Neo4j server exposes a class called WrappingNeoServerBootstrapper, which is capable of starting a Neo4j server in the same process as your application. It uses an AbstractGraphDatabase instance that you provide.

This gives your application, among other things, the REST API, statistics gathering and the web interface that comes with the server.

Usage example.

1234567 InternalAbstractGraphDatabase graphdb = getGraphDb(); WrappingNeoServerBootstrapper srv; srv = new WrappingNeoServerBootstrapper( graphdb ); srv.start(); // The server is now running // until we stop it: srv.stop();

Once you have the server up and running, see 第 26 章 基于Web的Neo4j图数据库管理工具 and rest-api for how to use it!

17.4.3. Providing custom configurationYou can modify the server settings programmatically and, within reason, the same settings are available to you here as those outlined in server-configuration.

The settings that are not available (or rather, that are ignored) are those that concern the underlying database, such as database location and database configuration path.

Custom configuration example.

1234567891011121314 // let the database accept remote neo4j-shell connections GraphDatabaseAPI graphdb = (GraphDatabaseAPI) new GraphDatabaseFactory() .newEmbeddedDatabaseBuilder( "target/configDb" ) .setConfig( ShellSettings.remote_shell_enabled, GraphDatabaseSetting.TRUE ) .newGraphDatabase(); ServerConfigurator config; config = new ServerConfigurator( graphdb ); // let the server endpoint be on a custom port config.configuration().setProperty( Configurator.WEBSERVER_PORT_PROPERTY_KEY, 7575 ); WrappingNeoServerBootstrapper srv; srv = new WrappingNeoServerBootstrapper( graphdb, config ); srv.start();

17.5. 服务器性能优化

17.5.1. Specifying Neo4j tuning properties

17.5.2. Specifying JVM tuning properties

At the heart of the Neo4j server is a regular Neo4j storage engine instance. That engine can be tuned in the same way as the other embedded configurations, using the same file format. The only difference is that the server must be told where to find the fine-tuning configuration.

Quick info

- The neo4j.properties file is a standard configuration file that databases load in order to tune their memory use and caching strategies.

- See 第 21.4 节 “Neo4j的缓存设置”for more information.

17.5.1. Specifying Neo4j tuning properties

The conf/neo4j-server.propertiesfile in the server distribution, is the main configuration file for the server. In this file we can specify a second properties file that contains the database tuning settings (that is, the neo4j.propertiesfile). This is done by setting a single property to point to a valid neo4j.propertiesfile:

1

org.neo4j.server.db.tuning.properties={neo4j.properties file}

On restarting the server the tuning enhancements specified in the neo4j.propertiesfile will be loaded and configured into the underlying database engine.

17.5.2. Specifying JVM tuning properties

Tuning the standalone server is achieved by editing the neo4j-wrapper.conffile in the confdirectory of NEO4J_HOME.

Edit the following properties:

表 17.1. neo4j-wrapper.conf JVM tuning properties

Property Name

Meaning

wrapper.java.initmemory

initial heap size (in MB)

wrapper.java.maxmemory

maximum heap size (in MB)

wrapper.java.additional.N

additional literal JVM parameter, where N is a number for each

For more information on the tuning properties, see 第 21.6 节 “JVM设置”.

17.6. 在云计算环境中的服务器安装

在各种云服务上的Neo4j,要么是用户自己建立的,要么是Neo Technology云。下面是关于这些的介绍。

17.7. Heroku

了解一个基本的安装设置,请参考:the Heroku 快速入门教程

为了增加Neo4j到你的Heroku应用中,如下操作:

1

heroku addons:add neo4j