下载 & 安装Es
官网下载链接:https://www.elastic.co/cn/downloads/elasticsearch
复制链接地址,使用wget命令下载
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.3-linux-x86_64.tar.gz
解压&重命名
tar -zxvf elasticsearch-7.9.3-linux-x86_64.tar.gz
mv elasticsearch-7.9.3 elasticsearch
创建data目录
cd elasticsearch && mkdir data
修改配置 config/elasticsearch.yml
#集群名称
cluster.name: cluster-1
#节点名称
node.name: node-1
#数据和日志的存储目录
path.data: /usr/local/elasticsearch/data
path.logs: /usr/local/elasticsearch/logs
#设置绑定的ip,设置为0.0.0.0以后就可以让任何计算机节点访问到了
network.host: 0.0.0.0
#默认端口
http.port: 9200
#设置在集群中的所有节点名称
cluster.initial_master_nodes: ["node-1"]
修改jvm参数<根据实际情况调整,默认1G> config/jvm.options
-Xms150m
-Xmx150m
添加es用户<es不允许root操作>
useradd esuser # 添加用户
chown -R esuser:esuser /usr/local/elasticsearch # 赋权用户文件夹操作权限
启动es <./elasticsearch -d 非阻塞启动>
su esuser # 切换esuser
./elasticsearch # 启动
出现错误
ERROR: [3] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
[2]: max number of threads [3833] for user [esuser] is too low, increase to at least [4096]
[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
切换root用户,修改 /etc/security/limits.conf 文件,添加以下内容<* 号别忘记copy了奥>
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
再修改 /etc/sysctl.conf 文件,添加以下内容
vm.max_map_count=262145
执行 sysctl -p 让修改的文件起作用
重新切换es用户,启动es,成功后访问ip:9200
安装IK分词器
下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases
tip:下载版本号和es对应的ik安装包
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v7.9.3
在 plugins/ik目录下,解压 elasticsearch-analysis-ik-7.9.3.zip
cd elasticsearch/plugins/ && mkdir ik # 进入plugins 创建ik目录unzip elasticsearch-analysis-ik-7.9.3.zip # 解压zip
值得注意的是, elasticsearch-analysis-ik-7.9.3.zip需要删除或者移出这个目录
最后,切换es用户,启动es
./elasticsearch -d
安装kibana <es图形界面操作工具>
下载地址:https://www.elastic.co/cn/downloads/kibana
解压 & 重命名
tar -zxvf kibana-7.9.3-linux-x86_64.tar.gz
mv kibana-7.9.3-linux-x86_64 kibana
修改配置 config/kibana.yml 增加如下配置
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.hosts: "http://localhost:9200"
启动 bin/kibana 出现如下提示
很明显,我们修改启动命令重新启动
./kibana --allow-root
非阻塞启动方式
nohup ./kibana --allow-root
浏览器访问kibana
附录1:命令行操作es
检测es是否启动成功
curl http://127.0.0.1:9200
检测es集群的健康状况
curl http://127.0.0.1:9200/_cat/health?v
查看所有索引
curl http://127.0.0.1:9200/_cat/indices?v
新增索引
curl -XPUT http://127.0.0.1:9200/first_index?pretty
新增数据
curl -XPUT http://127.0.0.1:9200/first_index/user/1?pretty -d '{"name":"张三","age":"23"}' --header "Content-Type: application/json"
查看数据
curl -XGET http://127.0.0.1:9200/first_index/user/1?pretty
更新数据并新增列
curl -XPOST http://127.0.0.1:9200/first_index/user/1?pretty -d '{"doc":{"name":"李四","age":"23","address":"北京东直门"}}' --header "Content-Type: application/json"
更新数据并修改age字段属性
curl -XPOST http://127.0.0.1:9200/first_index/user/1?pretty -d '{"doc":{"name":"李四","age":23,"address":"北京东直门"}}' --header "Content-Type: application/json"
删除数据
curl -XDELETE http://127.0.0.1:9200/first_index/user/1?pretty
批量插入
curl -XPOST http://127.0.0.1:9200/first_index/user/_bulk?pretty -d '
{"index":{"_id":"2"}}
{"name":"马云","age":45}
{"index":{"_id":"3"}}
{"name":"王健林","age":50}
' --header "Content-Type: application/json"
批处理语句
curl -XPOST http://127.0.0.1:9200/first_index/user/_bulk?pretty -d '
{"update":{"_id":"2"}}
{"doc": {"name":"马云二号","age":25}}
{"delete":{"_id":"3"}}
' --header "Content-Type: application/json"
查询某个索引的所有数据
curl http://127.0.0.1:9200/first_index/_search?q=*&
分页查询某个索引下的数据
curl -XPOST http://127.0.0.1:9200/first_index/_search?pretty -d '
{
"query":{
"match_all":{
}
},
"sort":{
"age":{
"order":"desc"
}
},
"from": 0,
"size": 20
}
' --header "Content-Type: application/json"
附录2:kibana操作es
新增索引 分片:3 副本:0
PUT /sed_index/
{
"settings":{
"index":{
"number_of_shards":3,
"number_of_replicas":0
}
}
}
添加文档 类型:user
PUT /sed_index/user/1
{
"first_name": "Jane",
"last_name": "Smith",
"age": 32
}
查询文档
GET /sed_index/user/1
查询全部
GET /sed_index/user/_search
编辑文档
PUT /sed_index/user/1
{
"first_name": "Jane",
"last_name": "Smith",
"age": 36
}
修改文档中指定的字段
POST /sed_index/user/1/_update
{
"doc":{
"age":33
}
}
删除文档
DELETE /sed_index/user/1
删除索引
DELETE sed_index