前提
ElasticSearch 同样需要 Java 环境,如果没有则需要先安装Java,关于Linux 下安装 Java 可以看这篇文章:Liunx 搭建 Java 开发环境。
安装
下载安装包
在 ElasticSearch 的下载页面 获取下载链接,然后通过 wget
命令下载
# 切换目录
cd /usr/local
# 下载
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.1.tar.gz
解压
tar -zxvf elasticsearch-6.5.1.tar.gz
创建用户
Elastic Serch 默认是不允许 root 用户启动的,所以我们要新建一个用户来启动 Elastic Serch。
使用 adduser 命令添加用户,输入密码后一路按Enter
键即可。
adduser elsearch
授权
chown -R elsearch ./elasticsearch/
修改配置文件(可选)
默认情况下,Elastic 只允许本机访问,如果需要远程访问,可以修改 ElasticSearch
安装目录的 config/elasticsearch.yml
文件,去掉network.host
的注释,将它的值改成 0.0.0.0
,然后重新启动 Elastic。
elasticsearch.yml
# ======================== 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: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
# 节点名字(可自定义)
node.name: node-1
#
# 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: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- 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
#
# Set a custom port for HTTP:
# 端口
http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
#discovery.zen.minimum_master_nodes:
#
# For more information, consult the zen discovery 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 REST 请求来直接查询
http.cors.enabled: true
# 如果 http.cors.enabled 的值为 true,那么该属性会指定允许 REST 请求来自何处。
http.cors.allow-origin: "*"
启动 ElasticSearch
# 进入安装目录
cd ./elasticsearch
# 切换用户
su elsearch
# 启动
./bin/elasticsearch
如果这时报错"max virtual memory areas vm.maxmapcount [65530] is too low",要运行下面的命令。
sudo sysctl -w vm.max_map_count=262144
如果一切正常,Elastic 就会在默认的 9200 端口运行。这时,打开另一个命令行窗口,请求该端口,会得到 ElasticSearch 集群的信息。
$ curl localhost:9200
{
"name" : "node-1",
"cluster_name" : "my-application",
"cluster_uuid" : "H4DwAEd2QIqaRtYFtx98TQ",
"version" : {
"number" : "6.5.1",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "8c58350",
"build_date" : "2018-11-16T02:22:42.182257Z",
"build_snapshot" : false,
"lucene_version" : "7.5.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
出现上面的信息说明 ElasticSearch 已经启动成功, 要退出可按 Enter + C
。
如果想后台运行可以使用 ./elasticesrarch -d
命令,这样 ElasticSearch 就会以守护进程运行。
参考资料
ElasticSearch 权威指南
Elasticsearch 服务配置属性