一、下载
下载地址:https://www.elastic.co/downloads/elasticsearch
历史版本:https://www.elastic.co/downloads/past-releases#elasticsearch
如果机器可以访问外网,也可以直接wget下载
shell> wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.4.tar.gz
二、安装(解压)
ES是运行在Java环境下的,所以需要先安装JDK,网上教程一大堆,不再赘述,也可以参考我的另外一篇文章:CentOS安装JDK:
shell> cd /usr/local/
shell> tar -zxvf /tools/elasticsearch-5.6.4.tar.gz
三、基本配置
1、创建用户组和用户(elasticsearch不允许root用户启动)
shell> groupadd es
shell> useradd es -g es
shell> chown -R es:es /usr/local/elasticsearch-5.6.4
# 启动时切换用户
shell> su es
2、es配置
shell> vim /usr/local/elasticsearch-5.6.4/config/elasticsearch.yml
data、logs路径
path.data: /usr/local/elasticsearch-5.6.4/data
path.logs: /usr/local/elasticsearch-5.6.4/logs
network.host: 192.168.1.10(默认配置下,只允许本机访问,如需其他机器访问,需要配置network.host)
network.host: 192.168.1.10
或者直接
network.host: 0.0.0.0
3、其他
shell> vim /etc/sysctl.conf
增加如下内容
vm.max_map_count = 262144
使配置生效
sysctl -p
本文中使用的环境为CentOS 7.6 + elasticsearch 5.6.4
,单节点,完成以上配置,就可以正常启动了,网上看到不少人启动时还遇到了其他各种各样的错误,我这里没有遇到,但是在文章最后会做一个补充
四、启动
shell> su es
shell> cd /usr/local/elasticsearch-5.6.4
shell> ./bin/elasticsearch
后台启动
shell> ./bin/elasticsearch -d
启动时额外指定配置项
shell> ./bin/elasticsearch -d -Ekey1=value1 -Ekey2=value2
五、测试
shell> curl -X GET "localhost:9200/?pretty"
{
"name" : "UgCLrdL",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "BaQiZ_JzQC-QKRqMivzwGA",
"version" : {
"number" : "5.6.4",
"build_hash" : "8bbedf5",
"build_date" : "2017-10-31T18:55:38.105Z",
"build_snapshot" : false,
"lucene_version" : "6.6.1"
},
"tagline" : "You Know, for Search"
}
或者浏览器直接访问:
六、常见问题
1、root用户启动失败
[2020-03-25T01:03:46,642][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:136) ~[elasticsearch-5.6.4.jar:5.6.4]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:123) ~[elasticsearch-5.6.4.jar:5.6.4]
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:70) ~[elasticsearch-5.6.4.jar:5.6.4]
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:134) ~[elasticsearch-5.6.4.jar:5.6.4]
at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-5.6.4.jar:5.6.4]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:91) ~[elasticsearch-5.6.4.jar:5.6.4]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:84) ~[elasticsearch-5.6.4.jar:5.6.4]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:106) ~[elasticsearch-5.6.4.jar:5.6.4]
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:195) ~[elasticsearch-5.6.4.jar:5.6.4]
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:342) ~[elasticsearch-5.6.4.jar:5.6.4]
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:132) ~[elasticsearch-5.6.4.jar:5.6.4]
... 6 more
解决方案:创建专门用于启动es的用户(上面有介绍)
2、权限错误
Exception in thread "main" 2020-03-25 23:57:47,459 main ERROR No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'log4j2.debug' to show Log4j2 internal initialization logging.
2020-03-25 23:57:50,612 main ERROR Could not register mbeans java.security.AccessControlException: access denied ("javax.management.MBeanTrustPermission" "register")
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:585)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.checkMBeanTrustPermission(DefaultMBeanServerInterceptor.java:1848)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:322)
at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:522)
at org.apache.logging.log4j.core.jmx.Server.register(Server.java:389)
at org.apache.logging.log4j.core.jmx.Server.reregisterMBeansAfterReconfigure(Server.java:167)
at org.apache.logging.log4j.core.jmx.Server.reregisterMBeansAfterReconfigure(Server.java:140)
at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:556)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:617)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:634)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:229)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:242)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:174)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:648)
at org.elasticsearch.common.logging.ESLoggerFactory.getLogger(ESLoggerFactory.java:54)
at org.elasticsearch.common.logging.ESLoggerFactory.getLogger(ESLoggerFactory.java:62)
at org.elasticsearch.common.logging.Loggers.getLogger(Loggers.java:101)
at org.elasticsearch.ExceptionsHelper.<clinit>(ExceptionsHelper.java:42)
at org.elasticsearch.ElasticsearchException.toString(ElasticsearchException.java:663)
at java.lang.String.valueOf(String.java:2994)
at java.io.PrintStream.println(PrintStream.java:821)
at java.lang.Throwable$WrappedPrintStream.println(Throwable.java:748)
at java.lang.Throwable.printStackTrace(Throwable.java:655)
at java.lang.Throwable.printStackTrace(Throwable.java:643)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1061)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1052)
at java.lang.Thread.dispatchUncaughtException(Thread.java:1959)
SettingsException[Failed to load settings from /usr/local/elasticsearch-5.6.4/config/elasticsearch.yml]; nested: AccessDeniedException[/usr/local/elasticsearch-5.6.4/config/elasticsearch.yml];
at org.elasticsearch.node.InternalSettingsPreparer.prepareEnvironment(InternalSettingsPreparer.java:102)
at org.elasticsearch.cli.EnvironmentAwareCommand.createEnv(EnvironmentAwareCommand.java:75)
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:70)
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:134)
at org.elasticsearch.cli.Command.main(Command.java:90)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:91)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:84)
Caused by: java.nio.file.AccessDeniedException: /usr/local/elasticsearch-5.6.4/config/elasticsearch.yml
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:84)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214)
at java.nio.file.Files.newByteChannel(Files.java:361)
at java.nio.file.Files.newByteChannel(Files.java:407)
at java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:384)
at java.nio.file.Files.newInputStream(Files.java:152)
at org.elasticsearch.common.settings.Settings$Builder.loadFromPath(Settings.java:1032)
at org.elasticsearch.node.InternalSettingsPreparer.prepareEnvironment(InternalSettingsPreparer.java:100)
... 6 more
解决方案:将es目录拥有者改为es用户(chown -R es:es /usr/local/elasticsearch-5.6.4
)
3、最大虚拟内存太小
[2020-03-25T02:01:00,956][INFO ][o.e.n.Node ] [] initializing ...
[2020-03-25T02:01:01,063][INFO ][o.e.e.NodeEnvironment ] [e9Gon5D] using [1] data paths, mounts [[/ (rootfs)]], net usable_space [43gb], net total_space [49gb], spins? [unknown], types [rootfs]
[2020-03-25T02:01:01,063][INFO ][o.e.e.NodeEnvironment ] [e9Gon5D] heap size [1.9gb], compressed ordinary object pointers [true]
[2020-03-25T02:01:01,064][INFO ][o.e.n.Node ] node name [e9Gon5D] derived from node ID [e9Gon5D8Q5WQ7gad072eBA]; set [node.name] to override
[2020-03-25T02:01:01,065][INFO ][o.e.n.Node ] version[5.6.4], pid[4142], build[8bbedf5/2017-10-31T18:55:38.105Z], OS[Linux/3.10.0-1062.9.1.el7.x86_64/amd64], JVM[Oracle Corporation/Java HotSpot(TM) 64-Bit Server VM/1.8.0_241/25.241-b07]
[2020-03-25T02:01:01,065][INFO ][o.e.n.Node ] JVM arguments [-Xms2g, -Xmx2g, -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, -XX:+UseCMSInitiatingOccupancyOnly, -XX:+AlwaysPreTouch, -Xss1m, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djna.nosys=true, -Djdk.io.permissionsUseCanonicalPath=true, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, -Dio.netty.recycler.maxCapacityPerThread=0, -Dlog4j.shutdownHookEnabled=false, -Dlog4j2.disable.jmx=true, -Dlog4j.skipJansi=true, -XX:+HeapDumpOnOutOfMemoryError, -Des.path.home=/usr/local/elasticsearch-5.6.4]
[2020-03-25T02:01:01,992][INFO ][o.e.p.PluginsService ] [e9Gon5D] loaded module [aggs-matrix-stats]
[2020-03-25T02:01:01,992][INFO ][o.e.p.PluginsService ] [e9Gon5D] loaded module [ingest-common]
[2020-03-25T02:01:01,992][INFO ][o.e.p.PluginsService ] [e9Gon5D] loaded module [lang-expression]
[2020-03-25T02:01:01,992][INFO ][o.e.p.PluginsService ] [e9Gon5D] loaded module [lang-groovy]
[2020-03-25T02:01:01,993][INFO ][o.e.p.PluginsService ] [e9Gon5D] loaded module [lang-mustache]
[2020-03-25T02:01:01,993][INFO ][o.e.p.PluginsService ] [e9Gon5D] loaded module [lang-painless]
[2020-03-25T02:01:01,993][INFO ][o.e.p.PluginsService ] [e9Gon5D] loaded module [parent-join]
[2020-03-25T02:01:01,993][INFO ][o.e.p.PluginsService ] [e9Gon5D] loaded module [percolator]
[2020-03-25T02:01:01,993][INFO ][o.e.p.PluginsService ] [e9Gon5D] loaded module [reindex]
[2020-03-25T02:01:01,993][INFO ][o.e.p.PluginsService ] [e9Gon5D] loaded module [transport-netty3]
[2020-03-25T02:01:01,993][INFO ][o.e.p.PluginsService ] [e9Gon5D] loaded module [transport-netty4]
[2020-03-25T02:01:01,994][INFO ][o.e.p.PluginsService ] [e9Gon5D] no plugins loaded
[2020-03-25T02:01:03,849][INFO ][o.e.d.DiscoveryModule ] [e9Gon5D] using discovery type [zen]
[2020-03-25T02:01:04,356][INFO ][o.e.n.Node ] initialized
[2020-03-25T02:01:04,356][INFO ][o.e.n.Node ] [e9Gon5D] starting ...
[2020-03-25T02:01:04,487][INFO ][o.e.t.TransportService ] [e9Gon5D] publish_address {10.163.2.24:9300}, bound_addresses {10.163.2.24:9300}
[2020-03-25T02:01:04,496][INFO ][o.e.b.BootstrapChecks ] [e9Gon5D] bound or publishing to a non-loopback or non-link-local address, enforcing bootstrap checks
ERROR: [1] bootstrap checks failed
[1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[2020-03-25T02:01:04,533][INFO ][o.e.n.Node ] [e9Gon5D] stopping ...
[2020-03-25T02:01:04,553][INFO ][o.e.n.Node ] [e9Gon5D] stopped
[2020-03-25T02:01:04,553][INFO ][o.e.n.Node ] [e9Gon5D] closing ...
[2020-03-25T02:01:04,570][INFO ][o.e.n.Node ] [e9Gon5D] closed
解决方案:修改/etc/sysctl.conf
,增加vm.max_map_count=262144
,然后执行下sysctl -p
以上三个问题在【基本配置】中都有提到,下面是我安装过程中没有遇到但是网上其他人的踩坑经验,如果遇到相同问题,可以参考下
4、无法创建本地文件问题,用户最大可创建文件数太小
max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
解决方案:
shell> vim /etc/security/limits.conf
添加如下内容:
* soft nofile 65536
* hard nofile 65536
* soft nproc 2048
* hard nproc 4096
备注:* 代表Linux所有用户名称(比如 hadoop)
保存、退出、重新登录才可生效
5、无法创建本地线程问题,用户最大可创建线程数太小
解决方案:
shell> vim /etc/security/limits.d/90-nproc.conf
找到如下内容:
* soft nproc 1024
修改为
* soft nproc 2048
本文主要介绍的是单机版es安装和配置以及常见问题解决方案,后面会再专门总结下集群的安装配置,和单节点大部分相似,只是额外多几个和集群相关的配置
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。