从https://www.elastic.co/cn/
网站下载elasticsearch
的安装包,笔者下载的安装包为elasticsearch-7.1.0
的版本。elasticsearch
在7版本之后已经自带JDK,但是这里建议使用自己系统安装的JDK。安装JDK可以参考笔者的博文:CentOS7安装JDK8
笔者打算将elasticsearch
安装在/usr/local
目录下,因而将安装包使用xftp
工具上传到该目录下。
使用tar -zxvf elasticsearch-7.1.0-linux-x86_64.tar.gz
命令解压elasticsearch-7.1.0
的安装包,解压完成后如图所示,会多出一个elasticsearch-7.1.0
的文件夹:
可以使用 rm -rf elasticsearch-7.1.0-linux-x86_64.tar.gz
命令将安装包删除。然后我们进入到elasticsearch
的文件夹内,看看elasticsearch
的目录结构。
bin //主要存放的是elasticsearch相关的脚本文件
config // 存放的是elasticsearch的配置文件
jdk //elasticsearch自带的jdk
logs //elasticsearch存放日志
modules //elasticsearch的功能模块
plugins //elasticsearch的插件,我们安装的插件存放在此目录
在这里我们需要对elasticsearch
进行相关的配置,编辑config
目录下的elasticsearch.yml
文件,重点关注以下7个配置项:
# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: yourscat-es-cluster //-------------(1)---------------------
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: node-1 //-------------(2)---------------------
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: /usr/local/elasticsearch-7.1.0/data //-------------(3)---------------------
#
# Path to log files:
#
path.logs: /usr/local/elasticsearch-7.1.0/logs //-------------(4)---------------------
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 0.0.0.0 //-------------(5)---------------------
#
# Set a custom port for HTTP:
#
http.port: 9200 //-------------(6)---------------------
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.seed_hosts: ["host1", "host2"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
cluster.initial_master_nodes: ["node-1"] //-------------(7)---------------------
#
# For more information, consult the discovery and cluster formation module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
配置完成后,我们进入到elasticsearch
的bin
目录下启动elasticsearch
,大概率会报错,我们根据报错提示信息进行修改。
cd /usr/local/elasticsearch-7.1.0/bin
./elasticsearch
会报以下的错误信息:
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:163) ~[elasticsearch-7.1.0.jar:7.1.0]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:150) ~[elasticsearch-7.1.0.jar:7.1.0]
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-7.1.0.jar:7.1.0]
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[elasticsearch-cli-7.1.0.jar:7.1.0]
at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-7.1.0.jar:7.1.0]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:115) ~[elasticsearch-7.1.0.jar:7.1.0]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-7.1.0.jar:7.1.0]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:102) ~[elasticsearch-7.1.0.jar:7.1.0]
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:169) ~[elasticsearch-7.1.0.jar:7.1.0]
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:325) ~[elasticsearch-7.1.0.jar:7.1.0]
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:159) ~[elasticsearch-7.1.0.jar:7.1.0]
主要原因是因为elasticsearch
默认不允许使用root
用户启动,我们可以单独创建一个用户来专门启动elasticsearch
,使用以下命令创建esGroup
用户组和esUser
用户,用户密码为123456
:
groupadd esGroup
useradd esUser -g esGroup -p 123456
权限的分配
cd /usr/local
chown -R esUser:esGroup elasticsearch-7.1.0
修改好以后启动出现以下错误:
ERROR: [2] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
[2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
解决第一个错误,打开新的窗口切换到root
用户,使用vim /etc/security/limits.conf
命令编辑limits.conf
,添加以下内容:
* soft nofile 65536
* hard nofile 65536
解决第二个错误,elasticsearch用户拥有的内存权限太小,至少需要262144。切换root
用户下,使用vim /etc/sysctl.conf
命令编辑sysctl.conf
文件
添加以下内容
vm.max_map_count=262144
执行sysctl -p
使修改生效。
这里两个错误修改以后,在服务器没有重启的情况下重新启动elasticsearch
,第一个问题依然存在,需要重启服务器。
进入elasticsearch
的安装目录,执行命令如下:
su esUser
cd /usr/local/elasticsearch-7.1.0
bin/elasticsearch -d //-d表是后台启动的方式
elasticsearch
的默认端口为9200
,为方便测试,笔者将服务器9200
端口开放,执行命令如下:
firewall-cmd --zone=public --add-port=9200/tcp --permanent
firewall-cmd --reload
在Chrome浏览器输入http://10.168.1.220/9200
,IP
地址和端口根据自己的安装环境进行修改,浏览器访问后得到以下结果,证明elasticsearch
安装成功。
{
"name": "node-1",
"cluster_name": "yourscat-es-cluster",
"cluster_uuid": "j4DTaY_dQHKb9T8-Q55lMQ",
"version": {
"number": "7.1.0",
"build_flavor": "default",
"build_type": "tar",
"build_hash": "606a173",
"build_date": "2019-05-16T00:43:15.323135Z",
"build_snapshot": false,
"lucene_version": "8.0.0",
"minimum_wire_compatibility_version": "6.8.0",
"minimum_index_compatibility_version": "6.0.0-beta1"
},
"tagline": "You Know, for Search"
}