elasticsearch笔记
Elasticsearch Clients | Elastic 官方文档
安装
下载地址:Elasticsearch, Kibana, and the Elastic Stack | Elastic
window
解压,双击 bin 目录下的 elasticsearch.bat
即可启动,kibana 也是同理。
启动后输入 http://localhost:9200 与 http://localhost:5601/ 显示正常说明两者都安装成功
linux
同 windows 不过多叙述了
docker
当然上面那些安装都过于麻烦,docker 一步到位
elasticsearch
# 创建自定义网络与kibana通信
docker network create esnet
# 挂载目录 端口映射
docker run -d --name elasticsearch --net esnet -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -v /data/elasticsearch:/usr/share/elasticsearch/data -v /data/elasticsearch/plugins:/usr/share/elasticsearch/plugins elasticsearch:tag
参数详解
docker run 创建并启动容器
-d 后台运行
--name elasticsearch 指定容器唯一的名称,方便管理
-p 9200:9200 -p 9300:9300 映射容器端口到宿主机上
-e "discovery.type=single-node" 环境变量配置单机模式
-v /data/elasticsearch:/usr/share/elasticsearch/data 持久化数据存储
-v /data/elasticsearch/plugins:/usr/share/elasticsearch/plugins
elasticsearch:tag 镜像名称及版本 tag最新
kibana
docker run -d --name kibana --net esnet -p 5601:5601 kibana:tag
ik 分词器
cd /usr/share/elasticsearch/plugins/
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.2.0/elasticsearch-analysis-ik-7.2.0.zip
exit
docker restart elasticsearch
或
docker exec -it 容器id /bin/bash
cd /usr/share/elasticsearch/plugins/
mkdir ik
cd ik
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.6.2/elasticsearch-analysis-ik-7.6.2.zip
yum install unzip
unzip elasticsearch-analysis-ik-7.6.2.zip
exit
docker restart elasticsearch
然后可以在 kibana 界面的dev tools
中验证是否安装成功;
POST test/_analyze
{
"analyzer": "ik_max_word",
"text": "你好我是愧怍"
}
设置密码
进入 es 容器
docker exec -it elasticsearch bash
cd config
vi elasticsearch.yml
添加如下代码
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization
xpack.security.enabled: true
重启后,重新进入容器,输入
elasticsearch-setup-passwords interactive
按 y 确认后即可设置密码
进入 kibana 容器
docker exec -it kibana bash
cd config
vi kibana.yml
添加如下代码
elasticsearch.username: "kibana"
elasticsearch.password: "a123456"
顺便在加这几行代码,后续如果导出数据过大的话也导的出来
xpack.reporting.csv.maxSizeBytes: 409715200
xpack.reporting.queue.timeout: 2800000
登录 Kibana 的账户就是 kibana,elasticsearch 的账户为 elastic.
docker-compose
创建 volume 挂载目录,并修改目录用户和用户组。由于 elasticsearch6 之后不允许使用 root 启用,所以需要修改
/usr/share/elasticsearch/data的权限为1000
mkdir -pv /usr/share/elasticsearch/data
chown 1000:1000 /usr/share/elasticsearch/data
部署文件
mkdir /usr/local/elasticsearch-kibana
cd elasticsearch-kibana/
vim docker-compose.yml
docker-compose.yml
version: '3.9'
services:
elasticsearch:
image: elasticsearch:7.2.0
container_name: elasticsearch
volumes:
- /usr/share/elasticsearch/data:/usr/share/elasticsearch/data
- ./elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
ports:
- 9200:9200
- 9300:9300
networks:
- esnet
restart: always
kibana:
image: kibana:7.2.0
container_name: kibana
ports:
- 5601:5601
networks:
- esnet
depends_on:
- elasticsearch
restart: always
networks:
esnet:
vim elasticsearch.yml
#集群名
cluster.name: "elasticsearch"
# 允许外部网络访问
network.host: 0.0.0.0
#支持跨域
http.cors.enabled: true
#支持所有域名
http.cors.allow-origin: "*"
# 开启xpack安全校验,在kibana中使用需要输入账号密码
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
启动 docker-compose docker-compose up -d
至此有关 elasticSearch 安装与配置就告一段落
数据迁移
elasticdump
elasticsearch-dump/elasticsearch-dump
这里使用 elasticdump (因为只会这个)
安装
npm install elasticdump -g
elasticdump
命令
elasticdump --input SOURCE --output DESTINATION [OPTIONS]