1、Elasticsearch
Elasticsearch(簡稱ES) 是一個分布式 , RESTful風格的搜尋和資料分析引擎 , 使用java開發并且是目前最流行的開源的企業級搜尋引擎,能夠達到近實時搜尋,穩定,可靠,快速,安裝使用友善。
用戶端支援Java、.NET(C#)、PHP、Python、Ruby等多種語言。
官方網站: https://www.elastic.co/
下載下傳位址:https://www.elastic.co/cn/downloads/past-releases#elasticsearch
2、單節點下載下傳&安裝
示範版本為7.17.3 運作Elasticsearch,需安裝并配置JDK
各個版本對Java的依賴 : https://www.elastic.co/support/matrix#matrix_jvm Elasticsearch
5需要Java 8以上的版本 , Elasticsearch 從6.5開始支援Java 11 , 7.0開始,内置了Java環境
2.1.下載下傳
https://www.elastic.co/cn/downloads/past-releases#elasticsearch
将下載下傳下來的壓縮包放到linux安裝的檔案下
2.2 解壓
tar zxvf elasticsearch-7.17.3-linux-x86_64.tar.gz
2.3 修改配置檔案
cd elasticsearch-7.17.3/config
vim elasticsearch.yml
#開啟遠端通路
network.host: 0.0.0.0
2.4 修改jvm參數
# 建議Xms和Xmx設定成一樣 , Xmx不要超過機器的50% ,不要超過30G , jvm.options檔案在config目錄下
vim jvm.options
2.5 啟動
# ES不允許使用Root賬号啟動 , 如果是Root使用者則需要建立一個使用者
# 為elaticsearch建立使用者并賦予相應權限
adduser es
passwd es
chown -R es:es elasticsearch-7.17.3
# 非root使用者啟動
bin/elasticsearch
# 背景啟動
bin/elasticsearch -d
2.6 驗證
啟動之後通路http://ip:9200
2.7 安裝ik分詞器插件
#檢視已安裝插件
bin/elasticsearch-plugin list
#安裝插件
bin/elasticsearch-plugin install analysis-icu
#删除插件
bin/elasticsearch-plugin remove analysis-icu
安裝和删除完之後都需要重新開機es才可以生效
離線安裝 ik分詞器
本地下載下傳相應的插件,解壓,然後手動上傳到elasticsearch的plugins目錄,然後重新開機ES執行個體就可以了。
比如ik中文分詞插件:https://github.com/medcl/elasticsearch-analysis-ik (下載下傳zip檔案)
2.8 測試分詞器
POST _analyze
{
"analyzer":"icu_analyzer",
"text":"中華人民共和國"
}
#ES的預設分詞設定是standard,會單字拆分
POST _analyze
{
"analyzer":"standard",
"text":"中華人民共和國"
}
#ik_smart:會做最粗粒度的拆
POST _analyze
{
"analyzer": "ik_smart",
"text": "中華人民共和國"
}
#ik_max_word:會将文本做最細粒度的拆分
POST _analyze
{
"analyzer":"ik_max_word",
"text":"中華人民共和國"
}
建立索引時可以指定IK分詞器作為預設分詞器
PUT /es_db
{
"settings" : {
"index" : {
"analysis.analyzer.default.type": "ik_max_word"
}
}
}
3.啟動ES服務常見錯誤解決方案
3.1 max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
ES因為需要大量的建立索引檔案,需要大量的打開系統的檔案,是以我們需要解除linux系統當中打開檔案最大數目的限制,不然ES啟動就會抛錯
#切換到root使用者
vim /etc/security/limits.conf
末尾添加如下配置:
* soft nofile 65536
* hard nofile 65536
* soft nproc 4096
* hard nproc 4096
3.2 max number of threads [1024] for user [es] is too low, increase to at least [4096]
無法建立本地線程問題,使用者最大可建立線程數太小
vim /etc/security/limits.d/20-nproc.conf
改為如下配置:
* soft nproc 4096
3.3 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
3.4 the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
缺少預設配置,至少需要配置discovery.seed_hosts/discovery.seed_providers/cluster.initial_master_nodes中的一個參數.
1.discovery.seed_hosts: 叢集主機清單
2.discovery.seed_providers: 基于配置檔案配置叢集主機清單
3.cluster.initial_master_nodes: 啟動時初始化的參與選主的node,生産環境必填
vim config/elasticsearch.yml
#添加配置
discovery.seed_hosts: ["127.0.0.1"]
cluster.initial_master_nodes: ["node-1"]
#或者 單節點(叢集單節點)
discovery.type: single-node
3.5 You must address the points described in the following [1] lines before starting Elasticsearch.
# 配置這兩個參數即可
node.name: node-1
cluster.initial_master_nodes: ["node-1"]
4.三節點es叢集搭建
本叢集基于上面的單節點搭建
4.1 修改hosts檔案
切換到root使用者 , 執行以下操作
vim /etc/hosts
192.168.154.146 cluster-node-146
192.168.154.147 cluster-node-147
192.168.154.148 cluster-node-148
4.2 修改elasticsearch.yml
# 指定叢集名稱3個節點必須一緻
cluster.name: es‐cluster
#指定節點名稱,每個節點名字唯一
node.name: cluster-node-146
#是否有資格為master節點,預設為true
node.master: true
#是否為data節點,預設為true
node.data: true
# 綁定ip,開啟遠端通路,可以配置0.0.0.0
network.host: 0.0.0.0
#指定web端口
#http.port: 9200
#指定tcp端口
#transport.tcp.port: 9300
#用于節點發現
discovery.seed_hosts: ["cluster-node-146", "cluster-node-147", "cluster-node-148"]
#7.0新引入的配置項,初始仲裁,僅在整個叢集首次啟動時才需要初始仲裁。
#該選項配置為node.name的值,指定可以初始化叢集節點的名稱
cluster.initial_master_nodes: ["cluster-node-146", "cluster-node-147", "cluster-node-148"]
#解決跨域問題
http.cors.enabled: true
http.cors.allow‐origin: "*"
4.3 啟動
每個節點的啟動方式和單節點啟動方式相同
4.4 驗證