文章目录
- [Elasticsearch 8.15.2 完整安装流程(Ubuntu/Debian 系统,国内源提速版)](#Elasticsearch 8.15.2 完整安装流程(Ubuntu/Debian 系统,国内源提速版))
-
- 一、核心目标
- 二、前置检查
- 三、完整安装步骤
-
- [步骤 1:添加国内 Elastic 镜像源(阿里云,速度快)](#步骤 1:添加国内 Elastic 镜像源(阿里云,速度快))
- [步骤 2:导入 Elastic 官方 GPG 密钥(适配新版系统)](#步骤 2:导入 Elastic 官方 GPG 密钥(适配新版系统))
- [步骤 3:更新 apt 缓存(让源和密钥生效)](#步骤 3:更新 apt 缓存(让源和密钥生效))
- [步骤 4:安装指定版本 Elasticsearch 8.15.2](#步骤 4:安装指定版本 Elasticsearch 8.15.2)
- [步骤 5:验证安装是否成功](#步骤 5:验证安装是否成功)
- 四、常见问题解决
- [五、 安装ik分词器](#五、 安装ik分词器)
-
- [核心逻辑:使用 ES 自带的 `elasticsearch-plugin` 工具安装(官方推荐)](#核心逻辑:使用 ES 自带的
elasticsearch-plugin
工具安装(官方推荐)) - [步骤 1:停止 Elasticsearch 服务](#步骤 1:停止 Elasticsearch 服务)
- [步骤 2:通过官方链接安装 IK 分词器(适配 ES 8.15.2)](#步骤 2:通过官方链接安装 IK 分词器(适配 ES 8.15.2))
- [步骤 3:修复插件目录权限(关键!)](#步骤 3:修复插件目录权限(关键!))
- [步骤 4:重启 Elasticsearch 并验证](#步骤 4:重启 Elasticsearch 并验证)
- [核心逻辑:使用 ES 自带的 `elasticsearch-plugin` 工具安装(官方推荐)](#核心逻辑:使用 ES 自带的
- [六、 ES 初始密码文件不存在 导致认证失败](#六、 ES 初始密码文件不存在 导致认证失败)
-
- [步骤 1:解决 ES 密码问题(核心)](#步骤 1:解决 ES 密码问题(核心))
-
- [方法 1:查看 ES 首次启动日志,获取初始密码](#方法 1:查看 ES 首次启动日志,获取初始密码)
- [方法 2:若日志中无密码,重置 `elastic` 用户密码](#方法 2:若日志中无密码,重置
elastic
用户密码)
- [步骤 2:设置一个新密码](#步骤 2:设置一个新密码)
-
- [步骤 1:设置符合要求的密码](#步骤 1:设置符合要求的密码)
- [步骤 2:验证 IK 插件(使用新密码)](#步骤 2:验证 IK 插件(使用新密码))
- [步骤 3:确认成功结果](#步骤 3:确认成功结果)
- 重要提醒
Elasticsearch 8.15.2 完整安装流程(Ubuntu/Debian 系统,国内源提速版)
一、核心目标
解决境外源下载慢、GPG 密钥失效、apt-key
过时等问题,通过国内镜像源 + 新版密钥管理,快速安装 Elasticsearch 8.15.2。
二、前置检查
-
确认系统版本(需 Debian 9+/Ubuntu 18.04+):
bashlsb_release -a # 查看系统版本,确保适配
-
清理旧的错误配置(若之前有安装失败记录):
bash# 删除旧的 Elasticsearch 源配置(若存在) sudo rm -f /etc/apt/sources.list.d/elasticsearch.list # 卸载残留的 Elasticsearch(若有) sudo apt remove -y elasticsearch
三、完整安装步骤
步骤 1:添加国内 Elastic 镜像源(阿里云,速度快)
创建并编辑源配置文件,使用国内镜像替代境外官方源:
bash
# 用 tee 命令直接写入源配置(避免编辑器操作错误)
sudo tee /etc/apt/sources.list.d/elasticsearch.list << 'EOF'
deb https://mirrors.aliyun.com/elasticstack/8.x/apt/ stable main
EOF
步骤 2:导入 Elastic 官方 GPG 密钥(适配新版系统)
apt-key
已过时,改用 trusted.gpg.d
目录管理密钥,从官方地址获取可靠密钥:
bash
# 1. 下载官方 GPG 密钥到临时目录(文件极小,几秒完成)
wget -O /tmp/GPG-KEY-elasticsearch https://artifacts.elastic.co/GPG-KEY-elasticsearch
# 若 wget 报错,换用 curl:
# curl -o /tmp/GPG-KEY-elasticsearch https://artifacts.elastic.co/GPG-KEY-elasticsearch
# 2. 转换密钥格式并放入系统信任目录(新版 apt 仅识别二进制密钥)
sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/elasticsearch.gpg /tmp/GPG-KEY-elasticsearch
# 3. 验证密钥是否成功导入(可选,确认文件存在)
ls /etc/apt/trusted.gpg.d/elasticsearch.gpg # 输出文件名即成功
步骤 3:更新 apt 缓存(让源和密钥生效)
bash
sudo apt update
- 若出现
Hit: https://mirrors.aliyun.com/elasticstack/8.x/apt stable InRelease
,说明源和密钥已正常生效。
步骤 4:安装指定版本 Elasticsearch 8.15.2
bash
# 安装 8.15.2 版本(指定版本避免自动升级到高版本)
sudo apt-get install -y elasticsearch=8.15.2
- 下载速度应在 1-10 MB/s(国内源正常速度),327MB 安装包约 1-5 分钟完成。
步骤 5:验证安装是否成功
-
启动 Elasticsearch 服务:
bashsudo systemctl start elasticsearch
-
设为开机自启(可选,推荐生产环境配置):
bashsudo systemctl enable elasticsearch
-
检查服务状态(确保为
active (running)
):bashsudo systemctl status elasticsearch
-
测试服务可用性(本地访问 9200 端口):
bash# 用 curl 访问 Elasticsearch 接口 curl http://127.0.0.1:9200
-
若返回类似以下 JSON 内容,说明安装成功:
json{ "name" : "localhost", "cluster_name" : "elasticsearch", "version" : { "number" : "8.15.2", "build_flavor" : "default", "build_type" : "deb", "build_hash" : "unknown", "build_date" : "2024-05-21T15:48:25.524622638Z", "build_snapshot" : false, ............ //省略 }, "tagline" : "You Know, for Search" }
-
四、常见问题解决
-
安装后启动失败:可能是内存不足,编辑配置文件限制内存:
bashsudo nano /etc/elasticsearch/jvm.options
修改内存配置(根据服务器内存调整,如 2G 内存设为 512m):
ini-Xms512m -Xmx512m
重启服务:
sudo systemctl restart elasticsearch
。 -
curl 访问 9200 端口失败:检查防火墙是否放行 9200 端口:
bash# 开放 9200 端口(临时生效) sudo ufw allow 9200 # 永久生效(可选) sudo ufw enable
-
多余依赖清理:安装后若提示 "自动安装的软件包不需要了",执行:
bashsudo apt autoremove -y
五、 安装ik分词器
IK 官方仓库最新 Releases 信息可知,IK 已不再在 GitHub Releases 直接提供预编译包,而是通过 Infinilabs 官方下载地址 分发。以下是针对 ES 8.15.2(或选择的 8.x 版本)的 精准安装步骤,无需手动编译,直接通过官方链接安装:
核心逻辑:使用 ES 自带的 elasticsearch-plugin
工具安装(官方推荐)
ES 提供了 bin/elasticsearch-plugin
命令,可直接从官方链接下载并安装 IK 插件,自动适配版本,避免手动下载和解压的权限 / 路径问题。
步骤 1:停止 Elasticsearch 服务
安装插件前必须停止 ES,否则会导致插件加载异常:
bash
sudo systemctl stop elasticsearch
步骤 2:通过官方链接安装 IK 分词器(适配 ES 8.15.2)
根据 IK 官方指引,安装链接格式为:https://get.infini.cloud/elasticsearch/analysis-ik/[ES版本号]
需将 [ES版本号]
替换为你的 ES 版本(如 8.15.2),执行以下命令:
bash
# 切换到 ES 安装目录(默认路径为 /usr/share/elasticsearch)
cd /usr/share/elasticsearch
# 执行官方安装命令(替换 8.15.2 为你的 ES 版本)
sudo bin/elasticsearch-plugin install https://get.infini.cloud/elasticsearch/analysis-ik/8.15.2
安装过程中的交互确认:
- 命令执行后,会提示
Continue with installation? [y/N]
,输入y
并回车; - 若提示 "插件未签名"(
Plugin is unsigned
),无需担心,这是 IK 官方插件的正常提示,继续确认即可。
步骤 3:修复插件目录权限(关键!)
elasticsearch-plugin
安装后,插件目录权限可能归 root
用户,需改为 elasticsearch
用户,否则 ES 无法读取:
bash
# 递归设置 IK 插件目录的归属(默认插件目录为 /usr/share/elasticsearch/plugins/analysis-ik)
sudo chown -R elasticsearch:elasticsearch /usr/share/elasticsearch/plugins/analysis-ik/
步骤 4:重启 Elasticsearch 并验证
bash
# 1. 重启 ES 服务
sudo systemctl restart elasticsearch
# 2. 查看 ES 状态(确保显示 "active (running)")
sudo systemctl status elasticsearch.service
# 3. 验证 IK 插件是否成功加载(8.x 默认启用 HTTPS)
# 方式:通过 ES API 查看已安装插件,需使用初始密码(首次启动时自动生成)
curl -k -u elastic:$(sudo cat /etc/elasticsearch/elastic-password) https://localhost:9200/_cat/plugins?v
六、 ES 初始密码文件不存在 导致认证失败
步骤 1:解决 ES 密码问题(核心)
ES 8.x 首次启动时会自动生成 elastic
用户的初始密码,并保存在 /etc/elasticsearch/elastic-password
文件中。若该文件不存在,可能是首次启动未完成或文件被误删,需重新生成密码:
方法 1:查看 ES 首次启动日志,获取初始密码
ES 会将初始密码打印在首次启动的日志中,可通过日志找回:
bash
# 查看 ES 日志中包含 "elastic" 密码的记录(按时间倒序,取最新的)
sudo grep -r "elastic" /var/log/elasticsearch/ | grep -i "password" | tail -n 10
日志中会有类似以下的内容(password
后即为初始密码):
plaintext
[2025-10-05T16:00:00,123][INFO ][o.e.x.s.a.s.FileRolesStore] [person-VirtualBox] parsed [0] roles from file [/etc/elasticsearch/roles.yml]
[2025-10-05T16:00:01,456][INFO ][o.e.x.s.a.AuthenticationService] [person-VirtualBox] Authentication of [elastic] user failed - invalid password
[2025-10-05T16:00:02,789][INFO ][o.e.x.s.a.InitialPasswordHash] [person-VirtualBox] Generated initial password for user [elastic]: 1234567890abcdef # 这是初始密码
复制日志中的密码(如 1234567890abcdef
),后续用于认证。
方法 2:若日志中无密码,重置 elastic
用户密码
若日志中未找到初始密码,直接重置 elastic
用户密码(需确保 ES 已启动):
bash
# 1. 进入 ES 安装目录(默认路径)
cd /usr/share/elasticsearch
# 2. 执行重置密码命令(需用 root 权限,且 ES 处于运行状态)
sudo bin/elasticsearch-reset-password -u elastic -i
- 执行后会提示
This tool will reset the password of the [elastic] user. Do you want to continue [y/N]?
,输入y
;
步骤 2:设置一个新密码
接下来需要你为 elastic
用户设置一个新密码(这是 ES 超级管理员账户,用于后续访问和管理 ES 服务),按以下流程操作即可:
步骤 1:设置符合要求的密码
ES 对密码有 复杂度要求 (至少 8 位,包含大小写字母、数字和特殊字符,例如 Elastic@123
、IkPlugin!876
),输入时注意:
- 输入密码时 终端不会显示任何字符(隐藏输入,是 Linux 安全机制),正常输入即可;
- 输入完成后按
Enter
,会提示Re-enter password for [elastic]:
,需再次输入相同密码确认; - 若两次输入一致且密码符合复杂度要求,会显示
Password for [elastic] successfully reset.
,表示密码重置成功。
步骤 2:验证 IK 插件(使用新密码)
密码重置成功后,立即用新密码验证 IK 插件是否安装成功:
bash
# 替换 "你的新密码" 为刚设置的密码(如 Elastic@123)
curl -k -u elastic:你的新密码 https://localhost:9200/_cat/plugins?v
步骤 3:确认成功结果
若输出类似以下内容,说明 ES 已正常启动且 IK 分词器安装成功:
plaintext
name component version type url
person-VirtualBox analysis-ik 8.15.2 plugin https://get.infini.cloud/elasticsearch/analysis-ik/8.15.2
component
列显示analysis-ik
(IK 插件标识);version
列显示与 ES 一致的版本(如 8.15.2),表示版本匹配。
重要提醒
- 保存密码 :将刚设置的
elastic
用户密码妥善保存(例如记录在本地文档),后续访问 ES 服务(如 Kibana 连接、API 调用)都需要用到; - 密码复杂度:若提示密码不符合要求,需重新设置(例如增加长度、补充特殊字符,避免纯数字或纯字母);
- 后续操作 :若需使用 IK 分词器创建索引,可参考 ES 官方文档配置
ik_max_word
(最细粒度分词)或ik_smart
(粗粒度分词)作为分词器。