Linux安装elasticsearch6.7.2
安装步骤
安装包下载地址:https://www.elastic.co/cn/downloads/elasticsearch
1、下载安装包
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.7.2.rpm
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.7.2.rpm.sha512
2、比较下载的RPM的SHA
后者应该输出elasticsearch-{version}.RPM:OK。
shasum -a 512 -c elasticsearch-6.7.2.rpm.sha512
3、使用rpm方式安装
rpm -ivh elasticsearch-6.7.2.rpm
#RPM 包采用系统默认的安装路径
#rpm安装不建议自定义路径
#路径 说明
#/usr/bin 普通可执行程序文件目录
#/usr/sbin 服务器执行程序文件和管理程序文件目录
#/etc 应用程序配置文件(安装目录)
#/var/log 日志文件目录
#/usr/share/doc 应用程序文档文件目录
#/usr/share/man 应用程序手册页文件目录
#/usr/lib 程序使用的动态函数库目录
4、加入开机自启动
sudo chkconfig --add elasticsearch
5、启动
sudo service elasticsearch start
6、创建日志和数据存放文件夹
mkdir -p /home/es/data
mkdir -p /home/es/log/elasticsearch
7、为elasticsearch用户赋予文件夹权限
(执行rpm安装时,会自动创建elasticsearch用户和组)
sudo chown -R elasticsearch:elasticsearch /home/es/data
sudo chmod -R 777 /home/es/data
sudo chown -R elasticsearch:elasticsearch /home/es/log/elasticsearch
sudo chmod -R 777 /home/es/log/elasticsearch
sudo chown -R elasticsearch:elasticsearch /usr/share/elasticsearch/
sudo chmod -R 777 /usr/share/elasticsearch/
sudo chown -R elasticsearch:elasticsearch /var/lib/elasticsearch
sudo chmod -R 777 /var/lib/elasticsearch
8、编辑ElasticSearch的服务文件
启动之前为elasticsearch配置jdk
vim /etc/sysconfig/elasticsearch 中修改JAVA_HOME路径的路径
指定JAVA_HOME位置
8 # Elasticsearch Java path
9 #JAVA_HOME=
10 JAVA_HOME=/opt/jdk1.8.0_121
9、修改es配置文件
cd /etc/elasticsearch/
#备份
mv elasticsearch.yml elasticsearch.yml.bak20230530
vim /etc/elasticsearch/elasticsearch.yml
10、修改jvm内存大小
vi ./config/jvm.options
# 内存占用
# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space
配置文件详解
#集群名称
cluster.name: huaming
#节点名称
node.name: hm-node
# 数据存储位置(单个目录设置)
path.data: /home/es/data
#日志文件的路径
path.logs: /home/es/log/elasticsearch
bootstrap.system_call_filter: false
# ---------------------------------- Network -----------------------------------
# #
# # 设置绑定的ip地址,可以是ipv4或ipv6的,默认为0.0.0.0
network.bind_host: 0.0.0.0 #只有本机可以访问http接口
#
# # 设置其它节点和该节点交互的ip地址,如果不设置它会自动设置,值必须是个真实的ip地址 (可以不配)
# # network.publish_host:
#
# # 同时设置bind_host和publish_host上面两个参数 (当前使用)
network.host: 0.0.0.0 #绑定监听IP
#
# # 设置节点间交互的tcp端口,默认是9300
transport.tcp.port: 9300
#
# # 设置是否压缩tcp传输时的数据,默认为false,不压缩
transport.tcp.compress: true
#
# # 设置对外服务的http端口,默认为9200
http.port: 9200
#
# # 设置请求内容的最大容量,默认100mb
http.max_content_length: 100mb
#
# # 使用http协议对外提供服务,默认为true,开启
http.cors.enabled: true
es常用命令
查看状态:service elasticsearch status
重启:service elasticsearch restart
启动:service elasticsearch start
停止:service elasticsearch stop
查看是否启动成功:curl 127.0.0.1:9200
查看集群健康状态:curl -XGET 'http://192.168.1.139:9200/_cat/health?v'
查看集群健康状态:curl -XGET 'http://192.168.1.139:9200/_cat/health?v'
curl -XGET 'http://192.168.1.139:9200/_cat/shards'
curl http://192.168.1.139:9200/_cluster/health\?pretty
curl http://192.168.1.139:9200/_nodes/process?pretty
curl http://192.168.1.139:9200/_cluster/allocation/explain?pretty
卸载elasticsearch
卸载:
# 查询rpm安装的elasticsearch
rpm -qa | grep elasticsearch;
# rpm卸载elasticsearch,elasticsearch-6.7.2-1.noarch名称 需要根据查询上一步查询结果来
rpm -e --nodeps elasticsearch-6.7.2-1.noarch;
# 删除残留文件
rm -rf /etc/elasticsearch;
# 删除elasticsearch自定义文件
rm -rf /usr/share/elasticsearch/
[root@51mm-webserver ~]# rpm -qa |grep elasticsearch
elasticsearch-6.7.2-1.noarch
[root@51mm-webserver ~]# rpm -qa |grep elasticsearch
elasticsearch-6.7.2-1.noarch
[root@51mm-webserver ~]# rpm -e --nodeps elasticsearch
Stopping elasticsearch service...停止 elasticsearch:[确定]
OK
warning: /etc/sysconfig/elasticsearch saved as /etc/sysconfig/elasticsearch.rpmsave
warning: /etc/elasticsearch/jvm.options saved as /etc/elasticsearch/jvm.options.rpmsave
warning: /etc/elasticsearch/elasticsearch.yml saved as /etc/elasticsearch/elasticsearch.yml.rpmsave
Deleting log directory... OK
Deleting plugins directory... OK
[root@51mm-webserver ~]# rpm -qa |grep elasticsearch
安装ik分词器
方式1
使用 elasticsearch-plugin 安装
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.7.2/elasticsearch-analysis-ik-6.7.2.zip
方式2
从此处下载预构建包:https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.7.2/elasticsearch-analysis-ik-6.7.2.zip
创建插件文件夹cd your-es-root/plugins/ && mkdir ik
将插件解压到文件夹your-es-root/plugins/ik
词典配置
IKAnalyzer.cfg.xml可以位于{conf}/analysis-ik/config/IKAnalyzer.cfg.xml 或{plugins}/elasticsearch-analysis-ik-*/config/IKAnalyzer.cfg.xml
安装es时的错误
1、which: no java in (/sbin:/usr/sbin:/bin:/usr/bin) Could not find any executable java binary. Please install java in your PATH or
which: no java in (/sbin:/usr/sbin:/bin:/usr/bin) Could not find any executable java binary. Please install java in your PATH or
解决:elasticsearch用户没有es安装根路径的权限
2、ERROR: bootstrap checks failed
max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
ERROR: bootstrap checks failed
max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
原因:无法创建本地文件,用户最大可创建文件数不够
解决:vim /etc/security/limits.conf
* hard nofile 65536
* soft nofile 65536
* soft nproc 65536
* hard nproc 65536
3、max number of threads [1024] for user [es] is too low, increase to at least [2048]
max number of threads [1024] for user [es] is too low, increase to at least [2048]
原因:无法创建本地线程问题,用户最大可创建线程数太小
解决:vim /etc/security/limits.d/90-nproc.conf
* soft nproc 2048(这边原本是1024)or * soft nproc 4096
4、max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
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
5、system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
原因:Centos6不支持SecComp
解决:vim elasticsearch.yml 切记在内存锁的下面加
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
6、elasticsearch 已死,但是 subsys 被锁
[root@51mm-webserver elasticsearch]# service elasticsearch status
elasticsearch 已死,但是 subsys 被锁
解决办法:
rm -f /var/lock/subsys/elasticsearch
创建索引
curl -XPUT http://localhost:9200/index
创建映射
curl -XPUT http://localhost:9200/index -H 'Content-Type:application/json' -d'
{
"mappings": {
"_doc": {
"properties": {
"id": { "type": "integer" },
"countryCodes": { "type": "integer" },
"phoneNumber": { "type": "keyword" },
"userName": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"pwd": { "type": "keyword" },
"email": { "type": "keyword" },
"nickName": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"headImage": { "type": "keyword" },
"isDelete": { "type": "integer" },
"invitationCode": { "type": "keyword" },
"remark": { "type": "keyword" },
"source": { "type": "integer" },
"createTime": { "type": "date" },
"updateTime": { "type": "date" }
}
}
}
}'
SpringBoot集成elasticsearch
pom依赖
这里只添加了主要的依赖,其他的如web、mvc、test、json等依赖自行添加
<!-- springboot集成elasticsearch的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!--es客户端-->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.7.2</version>
</dependency>
配置文件:
#elasticsearch配置
data:
elasticsearch:
cluster-name: 你的集群名称
cluster-nodes: 你的ip:你的端口(9300)
实现ElasticsearchRepository
public interface InsuranceProductDao extends ElasticsearchRepository<InsuranceProductEntity,Integer> {
}
调用InsuranceProductDao接口中的方法即可实现增删改查