环境

因为elasticsearch是用java编写的,所以需要先安装JDK

ES 5,安装需要 JDK 8 以上
ES 6.5,安装需要 JDK 11 以上
ES 7.2.1,内置了 JDK 12

安装jdk11

yum -y install java-11-openjdk.x86_64

使用 wget 命令下载

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

安装elasticsearch

1、解压elasticsearch到 /usr/local 目录下

tar -zxvf elasticsearch-5.6.6.tar.gz -C /usr/local/

2、elasticsearch默认是不支持用root用户来启动的,需要添加专门的用户

cd /usr/local
useradd elastic
chown -R elastic:elastic elasticsearch-5.6.6/
su elastic
./elasticsearch-5.6.6/bin/elasticsearch -d

2、进入到解压的elasticsearch包目录下、启动参数 -d 指的是后台运行

cd /usr/local/elasticsearch-5.6.6/
./bin/elasticsearch -d

3、使用  curl http://localhost:9200/  查看是否运行,如果返回如下信息则标示运行正常

 4、elasticsearch默认restful-api的端口是9200 不支持Ip地址,只能在本机用http://localhost:9200来访问。如果需要改变,需要修改配置文件。

默认情况下 Elasticsearch 的 RESTful 服务只有本机才能访问,也就是说无法从主机访问虚拟机中的服务。

可以修改 /etc/elasticsearch/config/elasticsearch.yml 文件,将注释的 network.host 和 http.port 放开,并配置正确的IP(或者0.0.0.0)

cd /usr/local/elasticsearch-5.6.6
vim config/elasticsearch.yml

5、先将Elasticsearch 关闭,然后启动;

关闭方法:输入命令: ps -ef | grep elasticsearch ,找到进程,然后kill掉就行了;

启动方法:输入命令:su elastic , 然后输入 ./bin/elasticserach -d 

6、在谷歌浏览器中打开:http://{server_IP}:9200/

问题

1、问题一:启动elasticsearch报错如下:Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000085330000, 2060255232, 0) failed; error='Cannot allocate memory' (errno=12)

 解决方法:

      由于elasticsearch5.0默认分配jvm空间大小为2g,修改jvm空间分配: vim config/jvm.options

-Xms2g
-Xmx2g

    修改为

-Xms512m
-Xmx512m

 2、问题二:启动elasticsearch报错如下:org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root

解决方法:

原因是elasticsearch默认是不支持用root用户来启动的,需要添加专门的用户。

cd /usr/local
useradd elastic
chown -R elastic:elastic elasticsearch-5.6.6/
su elastic
./elasticsearch-5.6.6/bin/elasticsearch -d

3、问题三:启动报错如下

ERROR: [3] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
[2]: max number of threads [1775] for user [elastic] is too low, increase to at least [2048]
[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

解决方法:

(1)[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]

    [2]: max number of threads [1775] for user [elastic] is too low, increase to at least [2048]

先切换到root账户下面,使用 vi /etc/security/limits.conf ,增加如下内容

elastic soft nofile 65536
elastic hard nofile 65536
elastic soft nproc 2048
elastic hard nproc 2048

(2)[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

使用 vim /etc/sysctl.conf ,增加如下的内容

vm.max_map_count=262144

输入:sysctl -p ,如下所示

(3)重新启动elasticsearch,

su elastic
cd /usr/local/elasticsearch-5.6.6
./bin/elasticsearch -d
01-04 10:34