CentOS安裝Elasticsearch

1、下載

下載地址:https://www.elastic.co/downloads/elasticsearchjava

歷史版本:https://www.elastic.co/downloads/past-releases#elasticsearchnode

若是機器能夠訪問外網,也能夠直接wget下載git

shell> wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.4.tar.gz

2、安裝(解壓)

ES是運行在Java環境下的,因此須要先安裝JDK,網上教程一大堆,再也不贅述,也能夠參考個人另一篇文章:CentOS安裝JDKgithub

shell> cd /usr/local/
shell> tar -zxvf /tools/elasticsearch-5.6.4.tar.gz

image-20200325010305868.png

3、基本配置

一、建立用戶組和用戶(elasticsearch不容許root用戶啓動)
shell> groupadd es
shell> useradd es -g es
shell> chown -R es:es /usr/local/elasticsearch-5.6.4
# 啓動時切換用戶
shell> su es
二、es配置
shell> vim /usr/local/elasticsearch-5.6.4/config/elasticsearch.yml

data、logs路徑shell

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)express

network.host: 192.168.1.10

或者直接apache

network.host: 0.0.0.0
三、其餘
shell> vim /etc/sysctl.conf

增長以下內容bootstrap

vm.max_map_count = 262144

使配置生效vim

sysctl -p

本文中使用的環境爲CentOS 7.6 + elasticsearch 5.6.4,單節點,完成以上配置,就能夠正常啓動了,網上看到很多人啓動時還遇到了其餘各類各樣的錯誤,我這裏沒有遇到,可是在文章最後會作一個補充segmentfault

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

5、測試

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"
}

或者瀏覽器直接訪問:
image-20200326004827033.png

6、常見問題

一、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的用戶(上面有介紹)

二、權限錯誤
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

三、最大虛擬內存過小
[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

以上三個問題在【基本配置】中都有提到,下面是我安裝過程當中沒有遇到可是網上其餘人的踩坑經驗,若是遇到相同問題,能夠參考下

四、沒法建立本地文件問題,用戶最大可建立文件數過小
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)
保存、退出、從新登陸纔可生效

五、沒法建立本地線程問題,用戶最大可建立線程數過小

解決方案:

shell> vim /etc/security/limits.d/90-nproc.conf

找到以下內容:

* soft nproc 1024

修改成

* soft nproc 2048

本文主要介紹的是單機版es安裝和配置以及常見問題解決方案,後面會再專門總結下集羣的安裝配置,和單節點大部分類似,只是額外多幾個和集羣相關的配置

參考文檔:https://github.com/DimonHo/DH_Note/issues/3

相關文章
相關標籤/搜索