Centos7下避坑安装elasticsearch

介绍

采用rpm的方式安装elasticsearch,为什么叫避坑安装。因为解压安装会有很多报错,主要是与系统参数的冲突,将会在附录中贴出部分安装包安装的报错与解决办法。而采用rpm安装方式直接载入到service中,利用service启动,方便快捷。

环境

安装过程

  1. 下载rpm包到服务器目录
  2. rpm安装并启动
# rpm -ivh  elasticsearch-2.3.0.rpm
# sudo systemctl start elasticsearch.service
  1. 调试
# curl -XGET 'http://localhost:9200/'

出现一下信息代表成功:

{
"name" : "WfSYaKG",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "dGCAG-HkSv-hiNFfeqoV1A",
"version" : {
  "number" : "2.3.0",
  "build_hash" : "f9d9b74",
  "build_date" : "2017-02-24T17:26:45.835Z",
  "build_snapshot" : false,
  "lucene_version" : "6.4.1"
},
"tagline" : "You Know, for Search"
}

你需要了解的目录结构

# whereis elasticsearch
elasticsearch: /etc/elasticsearch /usr/share/elasticsearch

可以看出elasticsearch的安装目录和配置文件所在目录,因为rpm安装和安装包解压的目录结构是不一样的,为了方便运维,你需要记住:

  • 启动文件目录:/usr/share/elasticsearch/bin
  • 配置文件目录:/etc/elasticsearch
  • 日志目录:/var/log/elasticsearch

安装包解压安装报错与解决方案

  1. 启动内存不足
$./bin/elasticsearch
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000085330000, 2060255232, 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 2060255232 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /data/elasticsearch-5.2.2/hs_err_pid26945.log

解决方案:调小启动内存

# vi ${elasticsearch_HOME}/config/jvm.options

#-Xms2g
#-Xmx2g
-Xms512m
-Xmx512m
  1. 无法以root权限启动
[2017-03-23T16:22:17,193][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-5.2.2.jar:5.2.2]
        at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:112) ~[elasticsearch-5.2.2.jar:5.2.2]
        at org.elasticsearch.cli.SettingCommand.execute(SettingCommand.java:54) ~[elasticsearch-5.2.2.jar:5.2.2]
        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:122) ~[elasticsearch-5.2.2.jar:5.2.2]
        at org.elasticsearch.cli.Command.main(Command.java:88) ~[elasticsearch-5.2.2.jar:5.2.2]
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:89) ~[elasticsearch-5.2.2.jar:5.2.2]
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:82) ~[elasticsearch-5.2.2.jar:5.2.2]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
        at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:105) ~[elasticsearch-5.2.2.jar:5.2.2]
        at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:203) ~[elasticsearch-5.2.2.jar:5.2.2]
        at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:333) ~[elasticsearch-5.2.2.jar:5.2.2]
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:121) ~[elasticsearch-5.2.2.jar:5.2.2]
        ... 6 more

解决方案:创建一个非root用户并赋予目录该用户权限,再启动

# groupadd es
# useradd es -g es -p es
# chown es:es ${elasticsearch_HOME}/
# sudo su es
# ./bin/elasticsearch
    原文作者:syncwt
    原文地址: https://www.jianshu.com/p/1d1886bfb5b8
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞