Linux下安装elasticsearch(Elasticsearch 7.17.23)

发布于:2025-03-11 ⋅ 阅读:(24) ⋅ 点赞:(0)

       Elasticsearch 是一个分布式的搜索和分析引擎,能够以近乎实时的速度存储、搜索和分析大量数据。它被广泛应用于日志分析、全文搜索、应用程序监控等场景。

本文将带你一步步在 Linux 系统上安装 Elasticsearch 7.17.23 版本,并完成基本的配置,为后续的使用打下基础。

你将学到:

  • 如何在 Linux 系统上下载和安装 Elasticsearch 7.17.23

  • 如何配置 Elasticsearch 的基本参数

  • 如何启动和停止 Elasticsearch 服务

  • 如何验证 Elasticsearch 是否安装成功

准备工作:

  • 一台运行 Linux 系统的服务器

  • 确保服务器上已经安装了 Java 8 或更高版本

  • 以 root 用户或具有 sudo 权限的用户身份登录

接下来,我们将按照以下步骤进行安装:

  1. 下载 Elasticsearch 7.17.23 安装包

  2. 解压安装包并配置环境变量

  3. 修改 Elasticsearch 配置文件

  4. 启动 Elasticsearch 服务

  5. 验证 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 descriptorsvm.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服务!大功告成。。