Linux安装Elasticsearch

时间:2023-02-11 12:01:38

大家好,我是小悟


1、概念

ElasticSearch,官方的解释Elasticsearch 是一个分布式的开源搜索和分析引擎,适用于所有类型的数据,包括文本、数字、地理空间、结构化和非结构化数据。Elasticsearch 在 Apache Lucene 的基础上开发而成,由 Elasticsearch N.V.(即现在的 Elastic)于 2010 年首次发布。Elasticsearch 以其简单的 REST 风格 API、分布式特性、速度和可扩展性而闻名,是 Elastic Stack 的核心组件;Elastic Stack 是适用于数据采集、充实、存储、分析和可视化的一组开源工具。人们通常将 Elastic Stack 称为 ELK Stack(代指 Elasticsearch、Logstash 和 Kibana),目前 Elastic Stack 包括一系列丰富的轻量型数据采集代理,这些代理统称为 Beats,可用来向 Elasticsearch 发送数据。现在大部分服务器是基于Linux底层,今天介绍下Linux上Elasticsearch的安装、遇到的问题以及解决方法。

Linux安装Elasticsearch

2、安装

Elasticsearch下载地址:

​https://www.elastic.co/cn/downloads/past-releases#elasticsearch​

解压elasticsearch-6.4.3.tar.gz到/usr/local/elastic-plugin目录:

进入解压后的elasticsearch目录:/usr/local/elastic-plugin/elasticsearch-6.4.3

(1)新建data目录:

Linux安装Elasticsearch

(2)修改config/elasticsearch.yml:

在Memory下面添加

bootstrap.system_call_filter: false

Linux安装Elasticsearch

在最后添加下列内容

#数据和日志的存储目录
path.data: /usr/local/elastic-plugin/elasticsearch-6.4.3/data
path.logs: /usr/local/elastic-plugin/elasticsearch-6.4.3/logs
#设置绑定的ip,设置为0.0.0.0以后就可以让任何计算机节点访问到了
network.host: 0.0.0.0
http.port: 9200 #端口
#qcl自己加的
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true

修改完毕后,:wq 保存退出vim

准备启动es

进入/bin目录执行命令:

后台启动:./elasticsearch -d

Linux安装Elasticsearch


3、问题以及解决方法

问题:

Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000d4cc0000, 724828160, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 724828160 bytes for committing reserved memory.
# An error report file with more information is saved as:
# logs/hs_err_pid8507.log
[root@VM_97_229_centos elasticsearch-6.4.3]#
[root@VM_0_2_centos bin]#

解决:

看来是我这1G的内存太小了啊,elasticsearch使用java的jvm默认是使用1G的内存的,这里我们修改一下内存,直接把内存改到200m

cd 到es目录修改 ./config/jvm.options:

修改该内容:

-Xmx200m

:wq 保存并退出vim,再次启动es

再次启动出现如下错误:

[2019-06-21T16:20:03,039][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler][node-1] 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:163) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:150) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[elasticsearch-cli-6.4.3.jar:6.4.3]
at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-6.4.3.jar:6.4.3.jar:6.4.3:7.1.1]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:115) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-6.4.3.jar:6.4.3]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:102) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:169) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:325) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:159) ~[elasticsearch-6.4.3.jar:6.4.3]
... 6 more
[root@VM_0_2_centos elasticsearch-7.1.1]#

这是不能使用root用户操作,添加一个其他的用户再试试:

用户名和密码都是admin

[root@VM_0_2_centos elasticsearch-6.4.3]# passwd admin
Changing password for user admin.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

改一下admin目录所属用户:

vim 编辑 /etc/security/limits.conf,在末尾加上:

admin hard nofile 65536
admin soft nproc 4096
admin hard nproc 4096

vim 编辑 vim /etc/security/limits.d/20-nproc.conf,将* 改为用户名(admin):

# accidental fork bombs.
# See rhbz #432903 for reasoning.
admin soft nproc 4096
root soft nproc unlimited

vim 编辑 /etc/sysctl.conf,在末尾加上:

执行:

​`​​[root@VM_0_2_centos ~]# sysctl -p

kernel.printk = 5

vm.max_map_count = 655360

[root@VM_0_2_centos ~]#

登录刚才新建的admin用户,并启动elasticsearch,OK
[es@VM_0_2_centos elasticsearch-6.4.3]$ ./bin/elasticsearch
![image.png](https://ucc.alicdn.com/pic/developer-ecology/eef197ebb86349139b5b62b6f272d893.png)
后台启动:[es@VM_0_2_centos elasticsearch-6.4.3]$
查看进程:
[es@VM_0_2_centos elasticsearch-6.4.3]$ ./bin/elasticsearch -d
[es@VM_0_2_centos elasticsearch-6.4.3]$ ps -ef|grep elasticsearch
es 18652 1 19 17:48 pts/2 00:00:00 /usr/local/java/jdk1.8.0_211/bin/java -Xms200m -Xmx200m -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFractinotallow=75 -XX:+UseCMSInitiatingOccupancyOnly -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=10 -XX:+AlwaysPreTouch -Xss1m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -XX:-OmitStackTraceInFastThrow -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimizatinotallow=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Djava.io.tmpdir=/tmp/elasticsearch-182563007296674551 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=data -XX:ErrorFile=logs/hs_err_pid%p.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintTenuringDistribution -XX:+PrintGCApplicationStoppedTime -Xloggc:logs/gc.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=32 -XX:GCLogFileSize=64m -Dio.netty.allocator.type=unpooled -Des.path.home=/usr/local/elasticsearch-6.4.3 -Des.path.cnotallow=/usr/local/elasticsearch-6.4.3/config -Des.distribution.flavor=default -Des.distribution.type=tar -Des.bundled_jdk=true -cp /usr/local/elasticsearch-6.4.3/lib/* org.elasticsearch.bootstrap.Elasticsearch -d
es 18728 8399 0 17:48 pts/2 00:00:00 grep --color=auto elasticsearch
[es@VM_0_2_centos elasticsearch-6.4.3]$


您的一键三连,是我更新的最大动力,谢谢

山水有相逢,来日皆可期,谢谢阅读,我们再会

我手中的金箍棒,上能通天,下能探海