ElasticSearch安装和配置

一、准备工作

安装Centos7、建议内存2G以上、能够访问网络、
安装java1.8环境

二、单机节点安装

1.下载安装包

  下载解压的文件夹可自选啊!自己开心就好!

ElasticSearch下载站点

[es@localhost ~]# mkdir -p /opt/es cd /opt/es 
[es@localhost ~]# wget  https://elasticsearch.thans.cn/downloads/elasticsearch/elasticsearch-6.3.1.tar.gz

2.解压并启动

  解压压缩包,进入解压后的bin目录,启动elasticsearch

[es@localhost ~]# tar -zxvf elasticsearch-6.3.1.tar.gz 
[es@localhost ~]# cd  elasticsearch-6.3.1
[es@localhost ~]# ./elasticsearch

  第一次可能无法启动,因为elasticsearch的配置要求可能与机器的不符,需进行一些修改。

3.配置优化

启动运行,按照错误提示配置es的最大文件数、线程数等等。
img

1、es使用jvm内存大小

[es@localhost ~]# cd /opt/es/elasticsearch-6.3.1/config/ 
[es@localhost ~]# vi jvm.options

  如果仅仅用于测试、学习不必用那么大,256MB足以。

img

2、es的host地址

  配成本机地址,允许访问,一般配置成0.0.0.0即代表允许所有ip访问。

[es@localhost ~]# cd /opt/es/elasticsearch-6.3.1/config/
[es@localhost ~]# vi elasticsearch.yml
#允许跨域(一般开发的时候可以这样配置)
http.cors.enabled:true
http.cors.allow-origin:"*"

img

3、linux用户配置

  由于ELasticSearch是由非root用户启动,用户的所使用的进程数、以及最大打开文件数等都是需要重新配置,以满足ELasticSearch的启动需求

[es@localhost ~]# vi /etc/security/limits.conf 
#配置文件生效 如果该命令无法执行,建议在配置完毕后重启
[es@localhost ~]# source/etc/security/limits.conf

#添加以下内容
* hard nofile 655360
* soft nofile 131072
* hard nproc 4096
* soft nproc 2048

img

  • nofile - 打开文件的最大数目
  • noproc - 进程的最大数目
  • soft 指的是当前系统生效的设置值
  • hard 表明系统中所能设定的最大值

4、jvm开启最大的线程数

[es@localhost ~]# vi /etc/sysctl.conf
[es@localhost ~]# sysctl -p

#添加以下内容
vm.max_map_count=655360
fs.file-max=655360

img

按照上述配置之后我的cenotos6.9还是无法启动,错误如下
img
解决方法:

  • 修改线程数
[es@localhost ~]# vi/etc/security/limits.d/90-nproc.conf

img

  • 修改虚拟内存
[es@localhost ~]# cd /opt/es/elasticsearch-6.3.1/config/ 
[es@localhost ~]# vi elasticsearch.yml

#添加以下内容
bootstrap.memory_lock: false
bootstrap.system_call_filter: false

img

  如果启动还是失败,建议重启一次,没有什么问题是重启解决不了的,如果有就再重启一次!哈哈哈!

如果以上配置完毕后还有其他问题,建议查看ElasticSearch Exception处理,看是否能够帮助到你。

三、集群安装

四、配置开机自启

1、编辑自启动文件

[es@localhost ~]# vi /etc/init.d/elasticsearch

2、增加文件执行权限

[es@localhost ~]# chmod +x /etc/init.d/elasticsearch

3、添加开机自启动

[es@localhost ~]# chkconfig --add /etc/init.d/elasticsearch

4、文件内容

  其中JAVA_HOME、用户名(developer)和ElasticSearch需根据自己及其进行修改。

#!/bin/bash
#chkconfig: 2345 80 05
#description: elasticsearch

export JAVA_HOME=/opt/environment/jdk1.8.0_241
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
su - developer<<!
cd /opt/environment/elasticsearch-6.3.1
./bin/elasticsearch -d
exit
!

五、添加ik分词器

ik分词器各个版本下载地址:
https://github.com/medcl/elasticsearch-analysis-ik/releases

参考:https://www.jianshu.com/p/f283d876b1cb

参考:https://juejin.im/entry/59f9220df265da431d3bfdc3