一、下载
官网下载地址:https://www.elastic.co/cn/downloads/elasticsearch
国内镜像源(华为):https://mirrors.huaweicloud.com/elasticsearch/
推荐使用国内的下载地址,官网下载太慢了。
选择合适的版本下载(推荐下载自带 JDK 的版本,否者自己配置的 JDK 可能不符合版本要求。注意:Elasticsearch 会优先使用系统配置的 JDK 。可将 Elasticsearch 自带的 JDK 配置到系统的环境变量中,如果不这样做的话,在安装 Logstash 时,启动会报没有配置 JDK 环境变量的错误。)
[root@localhost ~]# wget https://mirrors.huaweicloud.com/elasticsearch/7.8.0/elasticsearch-7.8.0-linux-x86_64.tar.gz
二、解压到指定目录
# 新建文件夹
[root@localhost ~]# mkdir /usr/local/elasticsearch
# 解压到指定文件夹
[root@localhost ~]# tar -zxvf elasticsearch-7.8.0-linux-x86_64.tar.gz -C /usr/local/elasticsearch/
三、修改配置文件
# 进入安装目录
[root@localhost ~]# cd /usr/local/elasticsearch/elasticsearch-7.8.0/
# 修改config/elasticsearch.yml
[root@localhost elasticsearch-7.8.0]# vim ./config/elasticsearch.yml
# 修改以下几项:
node.name: node-1 # 设置节点名
network.host: 0.0.0.0 # 允许外部 ip 访问
cluster.initial_master_nodes: ["node-1"] # 设置集群初始主节点
四、新建用户并赋权
ES为了安全考虑不允许使用root用户启动ElasticSearch,所以需要新建一个普通用户启动程序。
# 添加用户 es
[root@localhost elasticsearch-7.8.0]# adduser es
# 设置用户 es 的密码(需要输入两遍密码)
# (如果设置密码过于简单可能会提示 BAD PASSWORD: XXX ,如果是用 root 用户操作可忽略提示继续输入第二遍密码强制设置密码)
[root@localhost elasticsearch-7.8.0]# passwd es
Changing password for user es.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
# 将对应的文件夹权限赋予用户 es
[root@localhost elasticsearch-7.8.0]# chown -R es /usr/local/elasticsearch
五、切换至新建的用户并启动 Elasticsearch
# 切换至用户 es
[root@localhost elasticsearch-7.8.0]# su es
# 启动 ElasticSearch (-d 表示在后台启动)
[es@localhost elasticsearch-7.8.0]$ ./bin/elasticsearch -d
错误处理
启动之后可能会报以下三个错误:
ERROR: [3] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
[2]: max number of threads [3795] for user [es] is too low, increase to at least [4096]
[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
解决方式:
需切换到root用户解决错误:
# 切换到 root 用户
[es@localhost elasticsearch-7.8.0]$ su root
[1] 和 [2] 的解决方法:
# 修改 /etc/security/limits.conf 文件
[root@localhost elasticsearch-7.8.0]# vim /etc/security/limits.conf
# 添加以下四行
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
[3] 的解决方法:
# 修改 /etc/sysctl.conf 文件
[root@localhost elasticsearch-7.8.0]# vim /etc/sysctl.conf
# 添加下面一行
vm.max_map_count=655360
# 执行命令
[root@localhost elasticsearch-7.8.0]# sysctl -p
切换到用户 es 重新启动程序就可以了。
六、验证
注意: 防火墙需要开放9200端口
[root@localhost elasticsearch-7.8.0]# firewall-cmd --permanent --add-port=9200/tcp
success
[root@localhost elasticsearch-7.8.0]# firewall-cmd --permanent --add-port=9200/udp
success
[root@localhost elasticsearch-7.8.0]# firewall-cmd --reload
success
访问:http://192.168.40.149:9200
返回结果:
{
"name": "node-1",
"cluster_name": "elasticsearch",
"cluster_uuid": "bZD6PjHIQ1uovZrQ6nXa8Q",
"version": {
"number": "7.8.0",
"build_flavor": "default",
"build_type": "tar",
"build_hash": "757314695644ea9a1dc2fecd26d1a43856725e65",
"build_date": "2020-06-14T19:35:50.234439Z",
"build_snapshot": false,
"lucene_version": "8.5.1",
"minimum_wire_compatibility_version": "6.8.0",
"minimum_index_compatibility_version": "6.0.0-beta1"
},
"tagline": "You Know, for Search"
}
至此 Elasticsearch 单机部署启动完毕。