???????Elasticsearch 是一個分布式的搜索和分析引擎,能夠以近乎實時的速度存儲、搜索和分析大量數據。它被廣泛應用于日志分析、全文搜索、應用程序監控等場景。
本文將帶你一步步在 Linux 系統上安裝 Elasticsearch 7.17.23 版本,并完成基本的配置,為后續的使用打下基礎。
你將學到:
-
如何在 Linux 系統上下載和安裝 Elasticsearch 7.17.23
-
如何配置 Elasticsearch 的基本參數
-
如何啟動和停止 Elasticsearch 服務
-
如何驗證 Elasticsearch 是否安裝成功
準備工作:
-
一臺運行 Linux 系統的服務器
-
確保服務器上已經安裝了 Java 8 或更高版本
-
以 root 用戶或具有 sudo 權限的用戶身份登錄
接下來,我們將按照以下步驟進行安裝:
-
下載 Elasticsearch 7.17.23 安裝包
-
解壓安裝包并配置環境變量
-
修改 Elasticsearch 配置文件
-
啟動 Elasticsearch 服務
-
驗證 Elasticsearch 是否安裝成功
1.?下載 Elasticsearch 7.17.23 安裝包
Elasticsearch 7.17.23 | Elastic
直接在官方下載對應安裝包。
2.?解壓安裝包并配置環境變量
把安裝包上傳到服務器后,拷貝到/usr/local目錄下,執行解壓命令:
tar -xzf elasticsearch-7.3.2-linux-x86_64.tar.gz
由于ES不允許用root賬號啟動,這里需要創建用戶,例如:elastic
創建過程如下:
sudo useradd -m -d /home/elastic -s /bin/bash elastic
sudo passwd elastic # 設置密碼
sudo chown -R elastic:elastic /usr/local/elasticsearch-7.17.23
切換elastic賬號
su - elastic
3.修改 Elasticsearch 配置文件
進入Elasticsearch目錄:
cd /usr/local/elasticsearch/config
例如:
開始修改配置文件elasticsearch.yml:
?
記得自己創建好對應路徑的文件夾分配好權限!!!!
sudo chown -R elastic:elastic /usr/local/elasticsearch/
sudo chmod -R 755 /usr/local/elasticsearch/
?修改配置文件jvm.options:
4.啟動 Elasticsearch 服務
執行以下命令。如果不報錯,則跳轉到第五點看結果。
/usr/local/elasticsearch-7.17.23/bin/elasticsearch -d
5.如何驗證 Elasticsearch 是否安裝成功
一切正常的情況下,配置完成后,運行:curl -X GET "localhost:9200",出現如下圖,則表示安裝完成!
但是,肯定不會有正常情況
我們來說一下常見的錯誤:
(1)如果系統配置的是jdk8,啟動會出現jdk版本不匹配等錯誤,如下:
warning: usage of JAVA_HOME is deprecated, use ES_JAVA_HOME
Future versions of Elasticsearch will require Java 11; your Java version from [/usr/local/java/jdk1.8.0_144/jre] does not meet this requirement. Consider switching to a distribution of Elasticsearch with a bundled JDK. If you are already using a distribution with a bundled JDK, ensure the JAVA_HOME environment variable is not set.
warning: usage of JAVA_HOME is deprecated, use ES_JAVA_HOME
Future versions of Elasticsearch will require Java 11; your Java version from [/usr/local/java/jdk1.8.0_144/jre] does not meet this requirement. Consider switching to a distribution of Elasticsearch with a bundled JDK. If you are already using a distribution with a bundled JDK, ensure the JAVA_HOME environment variable is not set.
解決辦法就是,修改jdk指向路徑,默認使用es自帶的JDK,找到文件
?/usr/local/elasticsearch-7.17.23/bin下的:elasticsearch-env編輯,
vim?elasticsearch-env
注釋掉一部分配置,如下圖:?
這樣就解決jdk問題了。這個是常見問題。。另外還有幾個不常見的:
(1)?兩項系統限制值過低,需要調整 max file descriptors
和 vm.max_map_count
錯誤信息如下:
ERROR: [2] bootstrap checks failed. You must address the points described in the following [2] lines before starting Elasticsearch.
bootstrap check failure [1] of [2]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
bootstrap check failure [2] of [2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
ERROR: Elasticsearch did not exit normally - check the logs at /usr/local/elasticsearch/logs/my-cluster.log
直接切換root賬號調整:
sudo vi /etc/security/limits.conf
在文件末尾添加以下兩行:
elastic soft nofile 65535
elastic hard nofile 65535
退出保存!解決
(2)vm.max_map_count
的值太低,錯誤信息如下:
ERROR: [1] bootstrap checks failed. You must address the points described in the following [1] lines before starting Elasticsearch.
bootstrap check failure [1] of [1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
ERROR: Elasticsearch did not exit normally - check the logs at /usr/local/elasticsearch/logs/my-cluster.log
切換root,編輯sysctl.conf
vi /etc/sysctl.conf
添加:vm.max_map_count=262144
保存退出!解決!!
最后,我們得給es配置個密碼吧,有密碼才像那么一回事!
設置密碼方式有三種,我們選擇最穩妥的通過 elasticsearch-setup-passwords
工具設置密碼
修改配置文件
sudo vi /usr/local/elasticsearch-7.17.23/config/elasticsearch.yml
新增一下內容:
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
如圖:?
?運行命令:
/usr/local/elasticsearch-7.17.23/bin/elasticsearch-setup-passwords interactive
提示是否設置密碼。如圖:?
會讓設置很多賬號的密碼:
依次輸入密碼。建議同一個密碼!
修改完成后,重啟es服務!大功告成。。?