Linux Centos7.5 安裝elasticsearch(單機/集羣)

一:所需軟件 

ps:

1.需要依賴jdk環境(可以參考:Linux Centos7.5 安裝配置Oracle jdk1.8)。當前Linux是centos7.5版本;

2.如果是linux centos6.+可以參考:Linux centos6.8 安裝配置elasticsearch6.3.2 【單機】(一)Linux centos6.8 安裝配置elasticsearch6.3.2 【單機】(二)

二:安裝elasticsearch-7.4.2

#解壓
tar -zxvf elasticsearch-7.4.2-linux-x86_64.tar.gz

#將文件夾移動到/usr/local目錄下
mv elasticsearch-7.4.2 /usr/local

#進入到elasticsearch目錄下
cd elasticsearch-7.4.2/

#創建data空文件夾(用於存放數據)
mkdir data

#進入config目錄
/cd usr/local/elasticsearch-7.4.2/config

#編輯elasticsearch.yml(elasticsearch的核心配置文件)
vim elasticsearch.yml
注:如果沒有vim命令,可以使用vi命令,或者安裝vim

 編輯elasticsearch.yml:

#集羣名字
cluster.name: lucifer-elasticsearch

#節點
node.name: node-1

#存放數據路徑
path.data: /usr/local/elasticsearch-7.4.2/data

#存放日誌路徑
path.logs: /usr/local/elasticsearch-7.4.2/logs

#設置network.host
network.host: 0.0.0.0

#與node.name一致
cluster.initial_master_nodes: ["node-1"]

編輯jvm.options:

#編輯jvm.options
vim jvm.options

#原本1g修改爲:
-Xms256m
-Xmx256m

ps:whoami命令用於顯示自身用戶名稱.

問題1: 

can not run elasticsearch as root,也就是說不能使用root用戶去啓動elasticsearch,因爲elasticsearch內置的安全性。

解決:

#用於顯示當前用戶
whoami

#添加用戶
useradd esuser

#授權esuser用戶擁有elasticsearch-7.4.2目錄的權限
chown -R esuser /usr/local/elasticsearch-7.4.2

截圖1:

問題2:

Cannot open file logs/gc.log due to Permission denied;由於權限被拒絕,無法打開文件日誌/gc.log

解決:

#解決問題
[esuser@localhost bin]$ su root
Password:
[root@localhost bin]# cd ..
[root@localhost elasticsearch-7.4.2]# pwd
/usr/local/elasticsearch-7.4.2
[root@localhost elasticsearch-7.4.2]# chown -R esuser:esuser /usr/local/elasticsearch-7.4.2


#再次使用esuser用戶執行elasticsearch命令
[root@localhost elasticsearch-7.4.2]# su esuser
[esuser@localhost elasticsearch-7.4.2]$ cd bin
[esuser@localhost bin]$ ./elasticsearch

問題出現兩個:

問題3:

 max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]

[esuser@localhost bin]$ su root
Password:
[root@localhost bin]# vim /etc/security/limits.conf

解決:

ps:需要切換到root的用戶,系統文件需要root用戶權限纔可以修改。

#添加如下內容:
* soft nofile 65536
* hard nofile 65536
* soft nproc 4096
* hard nproc 4096

問題4:

max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

解決:

#編輯sysctl.conf
[root@localhost bin]# vim /etc/sysctl.conf
[root@localhost bin]# sysctl -p
vm.max_map_count = 262144
[root@localhost bin]# su esuser
[esuser@localhost bin]$ ./elasticsearch

這次啓動,正常。

訪問瀏覽器http://192.168.160.130:9200/可以看到elasticsearch的相關配置信息。

ps: 也可以後臺啓動elasticsearch ,命令:./elasticsearch -d

[esuser@localhost bin]$ ./elasticsearch -d
future versions of Elasticsearch will require Java 11; your Java version from [/usr/local/jdk1.8.0_11/jre] does not meet this requirement
[esuser@localhost bin]$

可以使用命令查看:jps

或者ps -ef | grep elasticsearch

三、安裝es-header插件

方式一使用谷歌商店提供的es-header插件(需要會科學上網)

附:chrome 網上應用店

插件名:ElasticSearch Head

四、安裝ik分詞器

1.安裝ik分詞器

[root@localhost opt]# unzip elasticsearch-analysis-ik-7.4.2.zip -d /usr/local/elasticsearch-7.4.2/plugins/ik
[root@localhost opt]# cd /usr/local/elasticsearch-7.4.2/plugins/ik/
[root@localhost ik]# ls
commons-codec-1.9.jar                httpclient-4.5.2.jar
commons-logging-1.2.jar              httpcore-4.4.4.jar
config                               plugin-descriptor.properties
elasticsearch-analysis-ik-7.4.2.jar  plugin-security.policy
[root@localhost ik]# su esuser
[esuser@localhost ik]$ jps
58434 Jps
39194 Elasticsearch
[esuser@localhost ik]$ kill 39194
[esuser@localhost ik]$ cd /usr/local/elasticsearch-7.4.2/bin
[esuser@localhost bin]$ ./elasticsearch -d
future versions of Elasticsearch will require Java 11; your Java version from [/usr/local/jdk1.8.0_11/jre] does not meet this requirement
[esuser@localhost bin]$ jps
58507 Elasticsearch
58526 Jps

ps:安裝完ik分詞器後,需要重啓elasticsearch。

測試:使用postman測試ik分詞效果:

ps: ik_max_word 會將文本做最細粒度的劃分。

      ik_smart 會將文本做最粗粒度的劃分。

效果1: ik_max_word:


分詞效果: 

{
    "tokens": [
        {
            "token": "中華人民共和國",
            "start_offset": 0,
            "end_offset": 7,
            "type": "CN_WORD",
            "position": 0
        },
        {
            "token": "中華人民",
            "start_offset": 0,
            "end_offset": 4,
            "type": "CN_WORD",
            "position": 1
        },
        {
            "token": "中華",
            "start_offset": 0,
            "end_offset": 2,
            "type": "CN_WORD",
            "position": 2
        },
        {
            "token": "華人",
            "start_offset": 1,
            "end_offset": 3,
            "type": "CN_WORD",
            "position": 3
        },
        {
            "token": "人民共和國",
            "start_offset": 2,
            "end_offset": 7,
            "type": "CN_WORD",
            "position": 4
        },
        {
            "token": "人民",
            "start_offset": 2,
            "end_offset": 4,
            "type": "CN_WORD",
            "position": 5
        },
        {
            "token": "共和國",
            "start_offset": 4,
            "end_offset": 7,
            "type": "CN_WORD",
            "position": 6
        },
        {
            "token": "共和",
            "start_offset": 4,
            "end_offset": 6,
            "type": "CN_WORD",
            "position": 7
        },
        {
            "token": "國",
            "start_offset": 6,
            "end_offset": 7,
            "type": "CN_CHAR",
            "position": 8
        },
        {
            "token": "國歌",
            "start_offset": 7,
            "end_offset": 9,
            "type": "CN_WORD",
            "position": 9
        }
    ]
}

 效果2: ik_smart:

{
    "tokens": [
        {
            "token": "中華人民共和國",
            "start_offset": 0,
            "end_offset": 7,
            "type": "CN_WORD",
            "position": 0
        },
        {
            "token": "國歌",
            "start_offset": 7,
            "end_offset": 9,
            "type": "CN_WORD",
            "position": 1
        }
    ]
}

2、自定義詞庫

附:elasticsearch-analysis-ik

2.1

{
    "tokens": [
        {
            "token": "騷",
            "start_offset": 0,
            "end_offset": 1,
            "type": "CN_CHAR",
            "position": 0
        },
        {
            "token": "年在",
            "start_offset": 1,
            "end_offset": 3,
            "type": "CN_WORD",
            "position": 1
        },
        {
            "token": "幹嘛",
            "start_offset": 3,
            "end_offset": 5,
            "type": "CN_WORD",
            "position": 2
        }
    ]
}

 2.2 編輯IKAnalyzer.cfg.xml

[esuser@localhost ~]$ cd /usr/local/elasticsearch-7.4.2/plugins/ik/config
[esuser@localhost config]$ ls
extra_main.dic                  extra_stopword.dic  quantifier.dic
extra_single_word.dic           IKAnalyzer.cfg.xml  stopword.dic
extra_single_word_full.dic      main.dic            suffix.dic
extra_single_word_low_freq.dic  preposition.dic     surname.dic
[esuser@localhost config]$ su root
Password:
[root@localhost config]# vim IKAnalyzer.cfg.xml

 IKAnalyzer.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
        <comment>IK Analyzer 擴展配置</comment>
        <!--用戶可以在這裏配置自己的擴展字典 -->
        <entry key="ext_dict">custom.dic</entry>
         <!--用戶可以在這裏配置自己的擴展停止詞字典-->
        <entry key="ext_stopwords"></entry>
        <!--用戶可以在這裏配置遠程擴展字典 -->
        <!-- <entry key="remote_ext_dict">words_location</entry> -->
        <!--用戶可以在這裏配置遠程擴展停止詞字典-->
        <!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>

ps:這裏的custom.dic是自己的詞庫。

[root@localhost config]# vim custom.dic
[root@localhost config]# ls
custom.dic                      extra_stopword.dic  stopword.dic
extra_main.dic                  IKAnalyzer.cfg.xml  suffix.dic
extra_single_word.dic           main.dic            surname.dic
extra_single_word_full.dic      preposition.dic
extra_single_word_low_freq.dic  quantifier.dic
[root@localhost config]# cd ..
[root@localhost ik]# cd /usr/local/elasticsearch-7.4.2/bin
[root@localhost bin]# su root
[root@localhost bin]# su esuser
[esuser@localhost bin]$ jps
58938 Jps
58507 Elasticsearch
[esuser@localhost bin]$ kill 58507
[esuser@localhost bin]$ jps
59034 Jps
[esuser@localhost bin]$ ./elasticsearch -d
future versions of Elasticsearch will require Java 11; your Java version from [/usr/local/jdk1.8.0_11/jre] does not meet this requirement

ps:custom.dic內容如下:

重啓elasticsearch後:

再次測試,”騷年“兩個字已經變成了一個詞了。

 

以上部分是單機。

================================搭建集羣====================================

以下部分就是改造爲集羣模式。

五、搭建集羣

虛擬機 ip地址
01 192.168.160.130
01-1 192.168.160.133
01-2 192.168.160.134

ps:01-1,01-2爲虛擬機01的克隆虛擬機。

ps:由於是克隆虛擬機,所以在每一臺虛擬機中都刪除cd /usr/local/elasticsearch-7.4.2/data的nodes文件夾中所有文件

5.1 刪除data文件夾下notes文件數據(三臺虛擬機都需要刪除

[root@localhost ~]# cd /usr/local/elasticsearch-7.4.2/data
[root@localhost data]# ls
nodes
[root@localhost data]# rm -rf nodes/
[root@localhost data]# ls
[root@localhost data]#

5.2 編輯elasticsearch.yml(三臺虛擬機都需要配置

192.168.160.130:

[root@localhost config]# vim elasticsearch.yml
[root@localhost config]# more elasticsearch.yml | grep ^[^#]
cluster.name: lucifer-es-cluster
node.name: node-1
path.data: /usr/local/elasticsearch-7.4.2/data
path.logs: /usr/local/elasticsearch-7.4.2/logs
network.host: 0.0.0.0
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
discovery.seed_hosts: ["192.168.160.130","192.168.160.133","192.168.160.134"]
cluster.initial_master_nodes: ["node-1"]


 ps: more elasticsearch.yml | grep ^[^#]  表示查看elasticsearch.yml中除去#註釋的配置

192.168.160.133;192.168.160.134這兩臺虛擬機elasticsearch.yml大致相同,除了node.name。192.168.160.130==》node.name: node-1;192.168.160.133==》node.name: node-2;92.168.160.134==》node.name: node-3;

再次切換爲esuser用戶,啓動elasticsearch;

ps:

如果遇到如下報錯信息:java.io.FileNotFoundException: /usr/local/elasticsearch-7.4.2/logs/lucifer-es-cluster_index_indexing_slowlog.json (Permission denied)

解決:刪除logs文件夾下所有文件

[esuser@localhost elasticsearch-7.4.2]$ cd logs
[esuser@localhost logs]$ pwd
/usr/local/elasticsearch-7.4.2/logs
[esuser@localhost logs]$ rm -rf *

三臺虛擬機均啓動成功後,訪問瀏覽器:

用Elasticsearch Head查看es集羣信息:

 

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章