Elasticsearch Linux 服务器安装使用指南
1460 字
7 分钟
Elasticsearch Linux 服务器安装使用指南
Elasticsearch是基于Apache Lucene的开源分布式搜索和分析引擎,提供RESTful API进行交互,支持近实时搜索和全文检索。
1. 环境要求
1.1 硬件配置
| 组件 | 最低配置 | 推荐配置 |
|---|---|---|
| CPU | 2核 | 4核以上 |
| 内存 | 4GB | 16GB以上 |
| 磁盘 | 10GB | 100GB以上 SSD |
1.2 软件要求
- 操作系统:CentOS 7/8、Ubuntu 18.04/20.04/22.04、RHEL 7/8
- Java:JDK 11或更高版本(Elasticsearch 8.x自带OpenJDK)
1.3 系统参数要求
- 最大文件描述符:至少65535
- 最大线程数:至少4096
- 虚拟内存:至少262144
2. 安装前准备
2.1 创建Elasticsearch用户
sudo groupadd elasticsearchsudo useradd -g elasticsearch -s /bin/bash -m elasticsearch2.2 配置系统参数
文件描述符限制:
sudo vim /etc/security/limits.conf# 添加以下内容elasticsearch soft nofile 65535elasticsearch hard nofile 65535elasticsearch soft nproc 4096elasticsearch hard nproc 4096虚拟内存设置:
sudo vim /etc/sysctl.confvm.max_map_count=262144sudo sysctl -p禁用Swap(推荐):
sudo swapoff -a3. 安装Elasticsearch
3.1 RPM包安装(CentOS/RHEL)
# 导入GPG密钥sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
# 创建仓库(清华大学镜像源)sudo vim /etc/yum.repos.d/elasticsearch.repo# 添加以下内容:# [elasticsearch]# baseurl=https://mirrors.tuna.tsinghua.edu.cn/elasticstack/yum/elastic-8.x/# gpgcheck=1# gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
sudo yum install elasticsearch -y3.2 DEB包安装(Ubuntu/Debian)
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpgecho "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://mirrors.tuna.tsinghua.edu.cn/elasticstack/apt/8.x stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.listsudo apt-get update && sudo apt-get install elasticsearch -y3.3 tar.gz包安装(通用)
cd /optsudo wget https://mirrors.tuna.tsinghua.edu.cn/elasticstack/downloads/elasticsearch/elasticsearch-8.11.0-linux-x86_64.tar.gzsudo tar -xzf elasticsearch-8.11.0-linux-x86_64.tar.gzsudo mv elasticsearch-8.11.0 elasticsearchsudo chown -R elasticsearch:elasticsearch /opt/elasticsearch4. 启动服务
4.1 Systemd管理(RPM/DEB安装)
sudo systemctl daemon-reloadsudo systemctl enable elasticsearchsudo systemctl start elasticsearchsudo systemctl status elasticsearch4.2 手动启动(tar.gz安装)
su - elasticsearch/opt/elasticsearch/bin/elasticsearch -d -p pid4.3 保存初始密码
安装完成后会显示自动生成的密码,请务必保存:
用户名:elastic密码:d8Yoo5g5g*TFbcAFpJSg重置密码命令:
sudo /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic5. 配置Elasticsearch
5.1 配置文件位置
- RPM/DEB安装:
/etc/elasticsearch/elasticsearch.yml - tar.gz安装:
/opt/elasticsearch/config/elasticsearch.yml
5.2 基本配置
sudo vim /etc/elasticsearch/elasticsearch.yml添加以下内容:
cluster.name: my-elasticsearch-clusternode.name: node-1discovery.type: single-nodenetwork.host: 0.0.0.0http.port: 9200path.data: /var/lib/elasticsearchpath.logs: /var/log/elasticsearch5.3 JVM配置
编辑jvm.options文件:
sudo vim /etc/elasticsearch/jvm.options设置堆内存(建议不超过物理内存的50%):
-Xms512m-Xmx1g5.4 2核2G环境优化配置
indices.queries.cache.size: 5%indices.requests.cache.size: 1%thread_pool.write.queue_size: 200thread_pool.search.queue_size: 5005.5 目录权限
sudo chown -R elasticsearch:elasticsearch /var/lib/elasticsearchsudo chown -R elasticsearch:elasticsearch /var/log/elasticsearchsudo chown -R elasticsearch:elasticsearch /etc/elasticsearch6. 验证安装
6.1 检查服务状态
# 等待服务启动(通常需要10-30秒)sleep 15
# 使用curl测试curl -k -u elastic:你的密码 https://localhost:92006.2 预期输出
{ "name" : "node-1", "cluster_name" : "my-elasticsearch-cluster", "version" : { "number" : "8.11.0" }, "tagline" : "You Know, for Search"}6.3 查看集群健康
curl -k -u elastic:你的密码 https://localhost:9200/_cluster/health?pretty健康状态说明:
- green:所有分片都可用
- yellow:主分片可用,副本分片不可用
- red:部分主分片不可用
7. 基本使用
7.1 索引操作
创建索引:
curl -k -X PUT "https://localhost:9200/products" \ -u elastic:你的密码 \ -H "Content-Type: application/json" \ -d @"{ \"settings\": { \"number_of_shards\": 1, \"number_of_replicas\": 0 }, \"mappings\": { \"properties\": { \"name\": { \"type\": \"text\" }, \"price\": { \"type\": \"float\" }, \"description\": { \"type\": \"text\" }, \"created_at\": { \"type\": \"date\" } } } }"@查看所有索引:
curl -k -u elastic:你的密码 "https://localhost:9200/_cat/indices?v"删除索引:
curl -k -X DELETE "https://localhost:9200/products" -u elastic:你的密码7.2 文档操作
添加文档:
curl -k -X POST "https://localhost:9200/products/_doc" \ -u elastic:你的密码 \ -H "Content-Type: application/json" \ -d "{\"name\": \"笔记本电脑\", \"price\": 5999.00, \"description\": \"高性能办公笔记本\"}"
curl -k -X PUT "https://localhost:9200/products/_doc/1" \ -u elastic:你的密码 \ -H "Content-Type: application/json" \ -d "{\"name\": \"台式电脑\", \"price\": 8999.00}"查询文档:
curl -k -X GET "https://localhost:9200/products/_doc/1?pretty" -u elastic:你的密码curl -k -X GET "https://localhost:9200/products/_search?pretty" -u elastic:你的密码更新文档:
curl -k -X POST "https://localhost:9200/products/_update/1" \ -u elastic:你的密码 \ -H "Content-Type: application/json" \ -d "{\"doc\": {\"price\": 7999.00}}"删除文档:
curl -k -X DELETE "https://localhost:9200/products/_doc/1" -u elastic:你的密码7.3 搜索查询
全文搜索:
curl -k -X GET "https://localhost:9200/products/_search?pretty" \ -u elastic:你的密码 \ -H "Content-Type: application/json" \ -d "{\"query\": {\"match\": {\"name\": \"电脑\"}}}"精确匹配:
curl -k -X GET "https://localhost:9200/products/_search?pretty" \ -u elastic:你的密码 \ -H "Content-Type: application/json" \ -d "{\"query\": {\"term\": {\"price\": 5999.00}}}"范围查询:
curl -k -X GET "https://localhost:9200/products/_search?pretty" \ -u elastic:你的密码 \ -H "Content-Type: application/json" \ -d "{\"query\": {\"range\": {\"price\": {\"gte\": 100, \"lte\": 6000}}}}"组合查询:
curl -k -X GET "https://localhost:9200/products/_search?pretty" \ -u elastic:你的密码 \ -H "Content-Type: application/json" \ -d "{\"query\": {\"bool\": {\"must\": [{\"match\": {\"description\": \"办公\"}}], \"filter\": [{\"range\": {\"price\": {\"lte\": 6000}}}]}}}"8. 安全配置
8.1 重置用户密码
cd /usr/share/elasticsearch/binsudo ./elasticsearch-reset-password -u elastic8.2 创建新用户
curl -k -X POST "https://localhost:9200/_security/user/myuser" \ -u elastic:你的密码 \ -H "Content-Type: application/json" \ -d "{\"password\": \"mypassword\", \"roles\": [\"kibana_admin\", \"monitoring_user\"]}"8.3 配置防火墙
# CentOS/RHELsudo firewall-cmd --permanent --add-port=9200/tcpsudo firewall-cmd --permanent --add-port=9300/tcpsudo firewall-cmd --reload
# Ubuntu/Debiansudo ufw allow 9200/tcpsudo ufw allow 9300/tcp9. 常见问题与解决方案
9.1 vm.max_map_count is too low
sudo sysctl -w vm.max_map_count=262144echo "vm.max_map_count=262144" | sudo tee -a /etc/sysctl.conf9.2 max file descriptors too low
编辑/etc/security/limits.conf添加:
elasticsearch soft nofile 65535elasticsearch hard nofile 65535然后重新登录elasticsearch用户。
9.3 OutOfMemoryError
sudo vim /etc/elasticsearch/jvm.options-Xms512m-Xmx1gsudo systemctl restart elasticsearch9.4 集群状态为Yellow
单节点模式下设置副本数为0:
curl -k -X PUT "https://localhost:9200/_settings" \ -u elastic:你的密码 \ -H "Content-Type: application/json" \ -d "{\"index\": {\"number_of_replicas\": 0}}"9.5 无法连接Elasticsearch
sudo systemctl status elasticsearchsudo netstat -tlnp | grep 9200sudo tail -f /var/log/elasticsearch/*.log10. 备份与恢复
10.1 创建快照仓库
sudo mkdir -p /opt/elasticsearch/backupsudo chown -R elasticsearch:elasticsearch /opt/elasticsearch/backup
# 编辑elasticsearch.yml添加:path.repo: ["/opt/elasticsearch/backup"]
curl -k -X PUT "https://localhost:9200/_snapshot/my_backup" \ -u elastic:你的密码 \ -H "Content-Type: application/json" \ -d "{\"type\": \"fs\", \"settings\": {\"location\": \"/opt/elasticsearch/backup\"}}"10.2 创建快照
curl -k -X PUT "https://localhost:9200/_snapshot/my_backup/snapshot_1?wait_for_completion=true" \ -u elastic:你的密码10.3 恢复快照
curl -k -X POST "https://localhost:9200/_snapshot/my_backup/snapshot_1/_restore" \ -u elastic:你的密码11. 监控与维护
# 集群健康curl -k -u elastic:你的密码 "https://localhost:9200/_cluster/health?pretty"
# 节点信息curl -k -u elastic:你的密码 "https://localhost:9200/_cat/nodes?v"
# 索引状态curl -k -u elastic:你的密码 "https://localhost:9200/_cat/indices?v&s=store.size:desc"
# 强制合并索引curl -k -X POST "https://localhost:9200/products/_forcemerge?max_num_segments=1" \ -u elastic:你的密码
# 清理缓存curl -k -X POST "https://localhost:9200/_cache/clear" -u elastic:你的密码12. 重要文件路径
| 类型 | RPM/DEB路径 | tar.gz路径 |
|---|---|---|
| 配置文件 | /etc/elasticsearch | /opt/elasticsearch/config |
| 数据目录 | /var/lib/elasticsearch | /opt/elasticsearch/data |
| 日志目录 | /var/log/elasticsearch | /opt/elasticsearch/logs |
13. 常用端口
| 端口 | 用途 |
|---|---|
| 9200 | HTTP REST API |
| 9300 | 节点间通信 |
结语
本文详细介绍了Elasticsearch在Linux服务器上的完整安装和使用流程。建议:
- 合理配置集群(生产环境至少3个主节点)
- 定期备份重要数据
- 监控集群健康状态和性能指标
- 根据业务需求调整索引策略
- 保持软件版本更新
支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或赞助支持!
相关文章 智能推荐
1
Docker:轻量级容器化技术的实战指南
Docker 告别"在我机器上能跑"的尴尬。深入浅出讲解 Docker 核心概念与实战技巧,教你如何利用容器化技术快速搭建开发环境、部署应用,提升开发与部署效率。
2
Debian系统重置忘记的 root 密码
系统运维 忘记root密码怎么办?教你重置忘记的 root 密码
3
Docker常用命令
Docker 本文整理了Docker日常开发与运维中最高频使用的命令清单,涵盖镜像管理、容器生命周期控制、数据卷操作、网络配置及系统清理等核心模块。通过结构化的分类与实例演示,助你快速掌握容器化操作精髓,提升云原生环境下的工作效率。
4
Java数组元素反转:详解双指针交换算法
Java 本文深入解析Java数组元素反转的核心逻辑。通过图解"借用第三方变量"的数据交换原理,详细演示如何利用双指针(首尾索引)技术高效实现数组倒序。文中包含完整的代码实现与步骤分析,助你掌握数组操作中经典的交换算法。
5
Java数组
Java 本文深入解析Java编程中最基础的数据结构——数组。内容涵盖数组的核心概念、静态与动态两种初始化方式及其内存原理图解,详细讲解了如何通过索引访问与修改元素、使用for循环实现数组遍历,并结合"计算销售总额"与"评委打分系统"两个实战案例,演示了数组在实际开发中的应用,助你夯实Java基础。
随机文章 随机推荐