一、ES安装
1.时间同步
[root@db02 ~]
2.安装Java环境
[root@db02 ~]
[root@db02 ~]
3.上传或下载ES的包
[root@db02 ~]
4.安装ES
[root@db02 ~]
sudo systemctl daemon-reload
sudo systemctl enable elasticsearch.service
5.配置
[root@db02 ~]
node.name: es02
path.data: /server/es/data
path.logs: /server/es/logs
bootstrap.memory_lock: true
network.host: 10.0.0.52,127.0.0.1
http.port: 9200
6.根据配置创建目录
[root@db02 ~]
[root@db02 ~]
[root@db02 ~]
7.启动ES
[root@db02 ~]
[Service]
LimitMEMLOCK=infinity
[root@db02 ~]
[root@db02 ~]
8.验证启动
[root@db02 ~]
tcp6 0 0 10.0.0.52:9200 :::* LISTEN 12627/java
tcp6 0 0 127.0.0.1:9200 :::* LISTEN 12627/java
tcp6 0 0 10.0.0.52:9300 :::* LISTEN 12627/java
tcp6 0 0 127.0.0.1:9300 :::* LISTEN 12627/java
二、与ES交互的方式
1.使用curl命令
1.不需要安装任何服务,只需要curl命令
2.命令复杂,容易出错
curl -XPUT 'localhost:9200/student?pretty'
curl -XPUT 'localhost:9200/student/user/1?pretty' -H 'Content-Type: application/json' -d '{"name": "lhd","sex":"man","age":"18","about":"good good study","interests":["chinese","english"]}'
curl -GET 'localhost:9200/student/user/1?pretty'
2.使用head插件进行交互
1.操作简单,数据展示明显
2.需要安装nodejs,安装复杂
3.可以直接使用插件
3.kibana的方式
1)特点
1.数据展示更好看
2.查询更方便
3.需要安装kibana
2)安装kibana
[root@db01 ~]
[root@db01 ~]
3)配置kibana
[root@db01 ~]
server.port: 5601
server.host: "10.0.0.51"
elasticsearch.hosts: ["http://localhost:9200" ]
kibana.index: ".kibana"
4)启动kibana
[root@db01 ~]
[root@db01 ~]
tcp 0 0 10.0.0.51:5601
5)访问kibana
三、ES数据的操作
1.创建索引
PUT /<index>
[root@db01 ~]
PUT /suoyin
PUT suoyin2
2.插入数据
POST /<index>/_type/
PUT /<index>/_type/<_id>
<index>
_type
<_id>
1)自定义ID插入数据
PUT /suoyin3/_doc/1
{
"name" :"qiudao" ,
"age" :"38"
}
2)使用随机ID插入数据(企业使用的方式)
POST /suoyin3/_doc/
{
"name" :"qiudao" ,
"age" :"38"
}
3)使用自定义字段定义id
POST /suoyin5/_doc/
{
"id" :"1" ,
"name" :"qiudao" ,
"age" :"38" ,
"sex" :"nan"
}
3.查询数据
1)简单查询
GET /_all
GET /_all/_search
GET /student
GET /student/_search
GET /suoyin3/_doc/1
2)条件查询
GET /suoyin3/_search
{
"query" : {
"term" : {
"age" : {
"value" : "38"
}
}
}
}
GET /suoyin3/_search
{
"query" : {
"term" : {
"age" : "38"
}
}
}
GET /suoyin3/_search
{
"query" : {
"match" : {
"age" : "38"
}
}
}
3)多条件查询
GET /suoyin3/_search
{
"query" : {
"bool" : {
"must" : [
{
"term" : {
"age" : {
"value" : "48"
}
}
},
{
"term" : {
"name" : {
"value" : "guolaoshi"
}
}
}
]
}
}
}
GET /suoyin3/_search
{
"query" : {
"bool" : {
"filter" : [
{
"term" : {
"age" : {
"value" : "48"
}
}
},
{
"term" : {
"name" : {
"value" : "guolaoshi"
}
}
}
]
}
}
}
GET /suoyin3/_search
{
"query" : {
"bool" : {
"should" : [
{
"term" : {
"age" : {
"value" : "18"
}
}
},
{
"term" : {
"name" : {
"value" : "qiudao"
}
}
}
]
}
}
}
GET /suoyin3/_search
{
"query" : {
"bool" : {
"must_not" : [
{
"term" : {
"age" : {
"value" : "18"
}
}
},
{
"term" : {
"name" : {
"value" : "qiudao"
}
}
}
]
}
}
}
GET /suoyin3/_search
{
"query" : {
"bool" : {
"should" : [
{
"bool" : {
"must" : [
{
"term" : {
"name" : {
"value" : "qiudao"
}
}
},
{
"term" : {
"age" : {
"value" : "38"
}
}
}
]
}
},
{
"bool" : {
"must" : [
{
"term" : {
"name" : {
"value" : "guolaoshi"
}
}
}
]
}
}
]
}
}
}
4.修改数据
PUT /suoyin4/_doc/Gy57THMBH5itxDSR1k9x
{
"id" :"1" ,
"name" :"qiudao" ,
"age" :"48"
}
POST /suoyin4/_doc/Gy57THMBH5itxDSR1k9x
{
"id" :"1" ,
"name" :"qiudao" ,
"age" :"48"
}
5.删除数据
DELETE /suoyin4/_doc/Gy57THMBH5itxDSR1k9x
DELETE /suoyin3
POST suoyin5/_delete_by_query
{
"query" : {
"match" : {
"name" : "qiudao"
}
}
}
四、集群
1.集群名词
1)集群状态
1.绿色:所有数据都完整,并且副本数正常
2.黄色:数据完整,但是有索引的副本有问题
3.红色:数据都不完整
2)节点类型
1.主节点:调度分配数据(主节点也是数据节点)
2.数据节点:负责分配自己的数据
3)数据分片
1.主分片:存储数据,负责读写数据
2.副本分片:主分片的备份(类似于从库)
2.搭建集群
1.时间同步
2.安装java环境
3.安装ES
4.配置ES
5.配置启动文件
6.启动ES
1)配置ES
[root@db03 ~]
cluster.name: es-cluster
node.name: es03
path.data: /server/es/data
path.logs: /server/es/logs
bootstrap.memory_lock: true
network.host: 10.0.0.53,127.0.0.1
http.port: 9200
discovery.zen.ping.unicast.hosts: ["10.0.0.51" ,"10.0.0.52" ,"10.0.0.53" ]
discovery.zen.minimum_master_nodes: 2
2)创建目录
[root@db03 ~]
[root@db03 ~]
3)配置启动文件
[root@db03 ~]
[Service]
LimitMEMLOCK=infinity
[root@db03 ~]
4)启动ES
[root@db03 ~]
5)到head插件页面查看集群
2.集群特点
1.集群中的数据,不论在哪一台操作,都可以使用插件查看所有数据
2.es-head插件不论连接到哪一台机器都能看到集群中所有节点
3.数据会自动平均调度到所有节点
4.如果主分片一但坏掉,副本分片会自动提升为主分片
5.如果主节点坏了,数据节点会自动升为主节点
3.ES集群注意事项
1.集群配置IP的时候,不需要填写全部IP,只需要填写本机IP和集群中任意一台机器的IP即可
discovery.zen.ping.unicast.hosts: ["10.0.0.51" , "10.0.0.52" ]
discovery.zen.ping.unicast.hosts: ["10.0.0.51" , "10.0.0.52" ]
discovery.zen.ping.unicast.hosts: ["10.0.0.51" , "10.0.0.53" ]
2.配置集群选主节点投票数量 (total number of master-eligible nodes / 2 + 1):
discovery.zen.minimum_master_nodes: 2
3.ES默认五个分片,一个副本
4.三个节点,出现故障
1)没有副本,坏一台都不行
2)有一个副本,最多可以坏一台
3)有两个副本,剩一台也可以提供服务
5.创建索引
1)创建索引时可以指定分片,但是分片一旦创建就不能修改了
2)副本数量可以根据需求修改
4.集群中使用的命令
GET _cluster/health
curl -XGET 'http://localhost:9200/_cluster/health?pretty'
GET _cat/nodes
curl -XGET 'http://localhost:9200/_cat/nodes?pretty'
GET _cat/indices
GET _cat/health
GET _cat/master
GET _cat/shards
GET _cat/shards/suoyin
4.集群内修改内容
1)修改指定索引的副本数
PUT /teacher/_settings
{
"number_of_replicas" : 1
}
2)修改所有索引的副本数
PUT _all/_settings
{
"number_of_replicas" : 2
}
3)配置文件修改默认的分片数和副本数
index.number_of_shards: 5
index.number_of_replicas:1
4)创建索引时指定分片数量
PUT /fenpian
{
"settings" : {
"number_of_replicas" : 2,
"number_of_shards" : 3
}
}
5)企业中一般怎么设置
1.跟开发沟通
2.看一共要几个节点
2个节点,默认就可以了
3个节点,重要的数据,2副本5分片,不重要的数据,1副本5分片
3.在开始阶段, 一个好的方案是根据你的节点数量按照1.5~3倍的原则来创建分片.
例如:如果你有3个节点, 则推荐你创建的分片数最多不超过9(3x3)个.
4.存储数据量多的可以设置分片多一些,存储数据量少的,可以少分写分片
五、集群监控
1.监控的项目
1.监控节点数量
GET _cat/nodes
2.监控集群状态
GET _cat/health
2.监控的脚本
[root@elkstack01 ~]
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
import subprocess
body = ""
false = "false"
clusterip = "10.0.0.51"
obj = subprocess.Popen(("curl -sXGET http://" +clusterip+":9200/_cluster/health?pretty=true" ),shell=True, stdout=subprocess.PIPE)
data = obj.stdout.read()
data1 = eval (data)
status = data1.get("status" )
if status == "green" :
print "\033[1;32m 集群运行正常 \033[0m"
elif status == "yellow" :
print "\033[1;33m 副本分片丢失 \033[0m"
else :
print "\033[1;31m 主分片丢失 \033[0m"
[root@elkstack01 ~]
集群运行正常
3.ES监控 X-pack
六、ES优化
1.限制内存
1.ES内存最大不超过32G
2.一般给ES运行内存为服务器内存的一半
[root@db02 ~]
-Xms1g
-Xmx1g
3.设置的时候先给小一点,不足时慢慢加
4.内存不足的时候,先让开发删除没用的数据,实在没有可以删除的再加内存
5.关闭swap空间
2.优化文件句柄数
[root@db02 ~]
* - nofile 65535
3.语句优化
1.查询的时候尽量用term
2.建立索引的时候尽量建立容易匹配的词
七、ES数据备份
1.安装npm
2.安装备份工具
[root@db01 ~]
3.备份类型
settings
analyzer
data
mapping
alias
template
4.备份命令
elasticdump \
--input=http://10.0.0.51:9200/my_index \
--output=http://11.0.0.51:9200/my_index \
--type =analyzer
elasticdump \
--input=http://10.0.0.51:9200/my_index \
--output=http://11.0.0.51:9200/my_index \
--type =mapping
elasticdump \
--input=http://10.0.0.51:9200/my_index \
--output=http://11.0.0.51:9200/my_index \
--type =data
elasticdump \
--input=http://10.0.0.51:9200/my_index \
--output=http://11.0.0.51:9200/my_index \
--type =alias
elasticdump \
--input=http://10.0.0.51:9200/my_index \
--output=http://11.0.0.51:9200/my_index \
--type =template
elasticdump \
--input=http://10.0.0.51:9200/my_index \
--output=/data/es_analyzer.json \
--type =analyzer
elasticdump \
--input=http://10.0.0.51:9200/my_index \
--output=/data/es_mapping.json \
--type =mapping
elasticdump \
--input=http://10.0.0.51:9200/my_index \
--output=/data/es_data.json \
--type =data
elasticdump \
--input=http://10.0.0.51:9200/my_index \
--output=/data/es_alias.json \
--type =alias
elasticdump \
--input=http://10.0.0.51:9200/my_index \
--output=/data/es_template.json \
--type =template
https://github.com/elasticsearch-dump/elasticsearch-dump
5.导入命令
elasticdump \
--input=/data/es_analyzer.json \
--output=http://10.0.0.51:9200/my_index \
--type =analyzer
elasticdump \
--input=/data/es_mapping.json \
--output=http://10.0.0.51:9200/my_index \
--type =mapping
elasticdump \
--input=/data/es_data.json \
--output=http://10.0.0.51:9200/my_index \
--type =data
elasticdump \
--input=/data/es_alias.json \
--output=http://10.0.0.51:9200/my_index \
--type =alias
elasticdump \
--input=/data/es_alias.json \
--output=http://10.0.0.51:9200/my_index \
--type =template
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现