阿里云服务器 快速安装ElasticSearch

使用wget 下载elasticsearch安装包

1
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.13.tar.gz

解压

1
tar -zxvf elasticsearch-5.6.13.tar.gz

外网访问配置

config目录下面elasticsearch.yml
修改为 network.host: 0.0.0.0

常见配置问题资料

此版本配合 spring-boot 对应版本 2.0.7.RELEASE 使用

1
2
3
4
5
6
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

spring-boot 使用依赖

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

IK中文分词GitHub下载网站

版本查看

使用wget 下载IK中文分词器

1
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.6.13/elasticsearch-analysis-ik-5.6.13.zip

插件下载到ES根目录下的plugins文件夹中 : ./elasticsearch-5.6.13/plugins/

解压

1
unzip elasticsearch-analysis-ik-5.6.13.zip

删除压缩包

1
rm elasticsearch-analysis-ik-5.6.13.zip

当我们在使用elasticsearch的时候,如果是以root权限来执行elasticsearch

1
./elasticsearch

则会有以下报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[2018-12-04T10:24:21,751][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:125) ~[elasticsearch-6.2.2.jar:6.2.2]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:112) ~[elasticsearch-6.2.2.jar:6.2.2]
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-6.2.2.jar:6.2.2]
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[elasticsearch-cli-6.2.2.jar:6.2.2]
at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-6.2.2.jar:6.2.2]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-6.2.2.jar:6.2.2]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:85) ~[elasticsearch-6.2.2.jar:6.2.2]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:105) ~[elasticsearch-6.2.2.jar:6.2.2]
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:172) ~[elasticsearch-6.2.2.jar:6.2.2]
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:323) ~[elasticsearch-6.2.2.jar:6.2.2]
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:121) ~[elasticsearch-6.2.2.jar:6.2.2]

这是出于系统安全考虑设置的条件。由于ElasticSearch可以接收用户输入的脚本并且执行,为了系统安全考虑,
建议创建一个单独的用户用来运行ElasticSearch

创建elsearch用户组及elsearch用户

1
2
groupadd elsearch
useradd elsearch -g elsearch -p elasticsearch

更改elasticsearch文件夹及内部文件的所属用户及组为elsearch:elsearch

1
chown -R elsearch:elsearch ${elasticsearch}

${elasticsearch}为你elasticsearch的目录路径

切换到elsearch用户再启动

1
2
3
su elsearch #切换账户
cd elasticsearch/bin #进入你的elasticsearch目录下的bin目录
./elasticsearch

启动后会打印正常启动日志信息如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[2018-12-04T10:48:10,218][INFO ][o.e.n.Node               ] [] initializing ...
[2018-12-04T10:48:10,387][INFO ][o.e.e.NodeEnvironment ] [5tOgySS] using [1] data paths, mounts [[/ (/dev/vda1)]], net usable_space [34.2gb], net total_space [39.2gb], types [ext4]
[2018-12-04T10:48:10,387][INFO ][o.e.e.NodeEnvironment ] [5tOgySS] heap size [990.7mb], compressed ordinary object pointers [true]
[2018-12-04T10:48:10,388][INFO ][o.e.n.Node ] node name [5tOgySS] derived from node ID [5tOgySSsTVyh-aqzPrZrZQ]; set [node.name] to override
[2018-12-04T10:48:10,389][INFO ][o.e.n.Node ] version[6.2.2], pid[2211], build[10b1edd/2018-02-16T19:01:30.685723Z], OS[Linux/4.4.0-117-generic/amd64], JVM[Oracle Corporation/OpenJDK 64-Bit Server VM/1.8.0_191/25.191-b12]
[2018-12-04T10:48:10,389][INFO ][o.e.n.Node ] JVM arguments [-Xms1g, -Xmx1g, -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, -XX:+UseCMSInitiatingOccupancyOnly, -XX:+AlwaysPreTouch, -Xss1m, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djna.nosys=true, -XX:-OmitStackTraceInFastThrow, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, -Dio.netty.recycler.maxCapacityPerThread=0, -Dlog4j.shutdownHookEnabled=false, -Dlog4j2.disable.jmx=true, -Djava.io.tmpdir=/tmp/elasticsearch.UJf6TjBC, -XX:+HeapDumpOnOutOfMemoryError, -XX:+PrintGCDetails, -XX:+PrintGCDateStamps, -XX:+PrintTenuringDistribution, -XX:+PrintGCApplicationStoppedTime, -Xloggc:logs/gc.log, -XX:+UseGCLogFileRotation, -XX:NumberOfGCLogFiles=32, -XX:GCLogFileSize=64m, -Des.path.home=/usr/local/lib/elasticsearch-6.2.2, -Des.path.conf=/usr/local/lib/elasticsearch-6.2.2/config]
[2018-12-04T10:48:11,432][INFO ][o.e.p.PluginsService ] [5tOgySS] loaded module [aggs-matrix-stats]
[2018-12-04T10:48:11,432][INFO ][o.e.p.PluginsService ] [5tOgySS] loaded module [analysis-common]
[2018-12-04T10:48:11,433][INFO ][o.e.p.PluginsService ] [5tOgySS] loaded module [ingest-common]
[2018-12-04T10:48:11,433][INFO ][o.e.p.PluginsService ] [5tOgySS] loaded module [lang-expression]
[2018-12-04T10:48:11,433][INFO ][o.e.p.PluginsService ] [5tOgySS] loaded module [lang-mustache]
[2018-12-04T10:48:11,433][INFO ][o.e.p.PluginsService ] [5tOgySS] loaded module [lang-painless]
[2018-12-04T10:48:11,433][INFO ][o.e.p.PluginsService ] [5tOgySS] loaded module [mapper-extras]
[2018-12-04T10:48:11,433][INFO ][o.e.p.PluginsService ] [5tOgySS] loaded module [parent-join]
[2018-12-04T10:48:11,434][INFO ][o.e.p.PluginsService ] [5tOgySS] loaded module [percolator]
[2018-12-04T10:48:11,434][INFO ][o.e.p.PluginsService ] [5tOgySS] loaded module [rank-eval]
[2018-12-04T10:48:11,434][INFO ][o.e.p.PluginsService ] [5tOgySS] loaded module [reindex]
[2018-12-04T10:48:11,434][INFO ][o.e.p.PluginsService ] [5tOgySS] loaded module [repository-url]
[2018-12-04T10:48:11,434][INFO ][o.e.p.PluginsService ] [5tOgySS] loaded module [transport-netty4]
[2018-12-04T10:48:11,434][INFO ][o.e.p.PluginsService ] [5tOgySS] loaded module [tribe]
[2018-12-04T10:48:11,435][INFO ][o.e.p.PluginsService ] [5tOgySS] no plugins loaded
[2018-12-04T10:48:14,742][INFO ][o.e.d.DiscoveryModule ] [5tOgySS] using discovery type [zen]
[2018-12-04T10:48:15,172][INFO ][o.e.n.Node ] initialized
[2018-12-04T10:48:15,172][INFO ][o.e.n.Node ] [5tOgySS] starting ...
[2018-12-04T10:48:15,356][INFO ][o.e.t.TransportService ] [5tOgySS] publish_address {127.0.0.1:9300}, bound_addresses {127.0.0.1:9300}
[2018-12-04T10:48:15,365][WARN ][o.e.b.BootstrapChecks ] [5tOgySS] max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]
[2018-12-04T10:48:15,365][WARN ][o.e.b.BootstrapChecks ] [5tOgySS] max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[2018-12-04T10:48:18,413][INFO ][o.e.c.s.MasterService ] [5tOgySS] zen-disco-elected-as-master ([0] nodes joined), reason: new_master {5tOgySS}{5tOgySSsTVyh-aqzPrZrZQ}{2do4g-5VQhqH_uTYtm9Paw}{127.0.0.1}{127.0.0.1:9300}
[2018-12-04T10:48:18,417][INFO ][o.e.c.s.ClusterApplierService] [5tOgySS] new_master {5tOgySS}{5tOgySSsTVyh-aqzPrZrZQ}{2do4g-5VQhqH_uTYtm9Paw}{127.0.0.1}{127.0.0.1:9300}, reason: apply cluster state (from master [master {5tOgySS}{5tOgySSsTVyh-aqzPrZrZQ}{2do4g-5VQhqH_uTYtm9Paw}{127.0.0.1}{127.0.0.1:9300} committed version [1] source [zen-disco-elected-as-master ([0] nodes joined)]])
[2018-12-04T10:48:18,526][INFO ][o.e.h.n.Netty4HttpServerTransport] [5tOgySS] publish_address {127.0.0.1:9200}, bound_addresses {127.0.0.1:9200}
[2018-12-04T10:48:18,526][INFO ][o.e.n.Node ] [5tOgySS] started
[2018-12-04T10:48:18,534][INFO ][o.e.g.GatewayService ] [5tOgySS] recovered [0] indices into cluster_state

ElasticSearch后台启动命令

1
./elasticsearch -d

常见异常

max file descriptors [65535] for elasticsearchprocess is too low, increase to at least [65536]

修改 /etc/security/limits.conf 添加如下内容(建议新增到最下面)

1
2
* - nofile 65536
* - memlock unlimited

max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

修改 /etc/sysctl.conf 添加如下内容(建议新增到最下面)

1
vm.max_map_count = 262144

修改完内核参数立即生效

1
sudo sysctl -p