Elasticsearch是目前最流行的开源搜索引擎,广泛用于日志分析、全文搜索、数据可视化等场景。本文整理了ES 7.x在Ubuntu/Debian系统上的三种安装方式,以及IK中文分词器的配置方法。
一、apt在线安装(推荐)
适用场景:服务器可以访问外网,最简单的安装方式。
# 1. 安装JDK11(ES7依赖) apt update apt install openjdk-11-jdk -y java -version # 2. 导入Elastic官方GPG密钥 wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | apt-key add - # 3. 添加ES 7.x软件源 echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" > /etc/apt/sources.list.d/elastic-7.x.list # 4. 更新缓存并安装 apt update apt install elasticsearch -y # 5. 启动并设置开机自启 systemctl start elasticsearch systemctl enable elasticsearch systemctl status elasticsearch
二、deb离线安装
适用场景:服务器无法访问外网,需要提前下载安装包。
# 1. 下载deb包(在有网的机器上)wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.24-amd64.deb
2. 安装JDK11
apt install openjdk-11-jdk -y
3. 安装deb包
dpkg -i elasticsearch-7.17.24-amd64.deb
如果依赖缺失
apt --fix-broken install -y
4. 重载系统服务
temctl daemon-reload
systemctl enable elasticsearch
systemctl start elasticsearch
三、Ubuntu 22.04特殊配置
Ubuntu 22.04的apt-key已过时,需要使用新版写法:
# 导入密钥(新版写法)
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elastic.gpg添加软件源(新版写法)
echo "deb [signed-by=/usr/share/keyrings/elastic.gpg] https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-7.x.list
然后正常安装
sudo apt update
sudo apt install elasticsearch -y
四、基础配置
编辑配置文件 /etc/elasticsearch/elasticsearch.yml:
# 允许外网访问
network.host: 0.0.0.0HTTP端口
http.port: 9200
单节点模式(避免集群报错)
discovery.type: single-node
修改后重启生效:systemctl restart elasticsearch
五、JVM内存配置
编辑 /etc/elasticsearch/jvm.options:
-Xms1g
-Xmx1g
建议堆内存大小一致,生产服务器根据配置调整,不要超过物理内存一半。
六、防火墙放行
# ufw防火墙
ufw allow 9200/tcp
ufw allow 9300/tcpfirewalld
firewall-cmd --permanent --add-port=9200/tcp
firewall-cmd --permanent --add-port=9300/tcp
firewall-cmd --reload
七、验证安装
curl http://127.0.0.1:9200
返回JSON格式的版本信息即表示安装成功:
{
"name" : "xxx",
"cluster_name" : "elasticsearch",
"version" : {
"number" : "7.17.24"
},
"tagline" : "You Know, for Search"
}八、安装IK中文分词器
Elasticsearch默认不支持中文分词,需要安装IK插件。
在线安装(推荐):
# 停止ES
systemctl stop elasticsearch安装IK插件(版本必须与ES一致)
/usr/share/elasticsearch/bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.17.24/elasticsearch-analysis-ik-7.17.24.zip
启动ES
systemctl start elasticsearch
离线安装:
# 1. 下载插件包
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.17.24/elasticsearch-analysis-ik-7.17.24.zip2. 创建插件目录并解压
mkdir -p /usr/share/elasticsearch/plugins/analysis-ik
unzip elasticsearch-analysis-ik-7.17.24.zip -d /usr/share/elasticsearch/plugins/analysis-ik3. 修正权限
chown -R elasticsearch:elasticsearch /usr/share/elasticsearch/plugins/analysis-ik
4. 重启ES
systemctl restart elasticsearch
测试IK分词效果:
# ik_smart(智能粗分词)
curl -X POST "http://localhost:9200/_analyze" -H 'Content-Type: application/json' -d '{
"analyzer": "ik_smart",
"text": "中华人民共和国国歌"
}'ik_max_word(细粒度全切分)
curl -X POST "http://localhost:9200/_analyze" -H 'Content-Type: application/json' -d '{
"analyzer": "ik_max_word",
"text": "中华人民共和国国歌"
}'
九、常见报错解决
# 1. vm.max_map_count过小临时生效
sysctl -w vm.max_map_count=262144
永久生效:编辑/etc/sysctl.conf添加vm.max_map_count=262144
sysctl -p
2. 端口不通
检查network.host配置、防火墙、安全组是否放行9200
3. IK插件版本不匹配
IK插件版本必须与ES版本完全一致
十、目录结构说明
| 路径 | 说明 |
|---|---|
| /etc/elasticsearch/ | 配置文件目录 |
| /var/lib/elasticsearch/ | 数据目录 |
| /var/log/elasticsearch/ | 日志目录 |
| /usr/share/elasticsearch/ | 程序目录 |
| /etc/elasticsearch/jvm.options | JVM配置 |
本文内容整理自豆包AI对话记录。
评论