CentOS 7.9 安装 Elasticsearch 7.8.1

  |   0 评论   |   0 浏览

安装环境

CentOS 安装的是最小版本:CentOS-7-x86_64-Minimal-2009

1[root@localhost ~]# cat /etc/redhat-release
2CentOS Linux release 7.9.2009 (Core)

关闭防火墙:

1systemctl stop firewalld.service
2systemctl disable firewalld.service

前期准备

新建用户

默认情况下,Elasticsearch 不允许使用 root 用户启动。

1groupadd esgroup
2useradd esapp -g esgroup

修改资源配置

修改 /etc/security/limits.conf 文件,在文件中添加以下内容:

1esapp  -  nofile  65535
2esapp  -  nproc   4096

修改 /etc/sysctl.conf 文件,在文件中添加以下内容:

1vm.max_map_count=262144

禁用内存交换

临时关闭命令:

1swapoff -a

永久修改的方法:修改 /etc/fstab 文件,将注释掉所有包含 swap 的行。

全部配置完后,重启电脑,因为有些配置需要重启电脑才能生效。

关于 Java

因为 Elasticsearch 自带了 OpenJDK,所以我这里没有额外安装 JDK。

下载安装

使用 esapp 用户登录,或使用 su - esapp 切换到 esapp 用户,运行下面的命令:

1wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.8.1-linux-x86_64.tar.gz
2wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.8.1-linux-x86_64.tar.gz.sha512
3shasum -a 512 -c elasticsearch-7.8.1-linux-x86_64.tar.gz.sha512
4tar -xzf elasticsearch-7.8.1-linux-x86_64.tar.gz
5cd elasticsearch-7.8.1/

如果在校验时,提示 -bash: shasum: command not found,则需要安装 perl-Digest-SHA

1yum install -y perl-Digest-SHA

修改配置文件

1cd /home/esapp/elasticsearch-7.8.1/config
2cp elasticsearch.yml elasticsearch.yml.bak
3vi elasticsearch.yml

network.host: 192.168.0.1 改为 network.host: 0.0.0.0 并取消注释。以便外部浏览器可以访问。

添加 discovery.type: single-node

启动并验证

启动

1# 依然使用普通用户
2cd /home/esapp/elasticsearch-7.8.1/bin
3# 启动
4./elasticsearch

检查是否启动成功

打开新的终端,输入下面的命令。

 1[root@localhost ~]# curl -X GET "localhost:9200/?pretty"
 2{
 3  "name" : "localhost.localdomain",
 4  "cluster_name" : "elasticsearch",
 5  "cluster_uuid" : "7iE1xknOQLiIJiDnK7dJug",
 6  "version" : {
 7    "number" : "7.8.1",
 8    "build_flavor" : "default",
 9    "build_type" : "tar",
10    "build_hash" : "b5ca9c58fb664ca8bf9e4057fc229b3396bf3a89",
11    "build_date" : "2020-07-21T16:40:44.668009Z",
12    "build_snapshot" : false,
13    "lucene_version" : "8.5.1",
14    "minimum_wire_compatibility_version" : "6.8.0",
15    "minimum_index_compatibility_version" : "6.0.0-beta1"
16  },
17  "tagline" : "You Know, for Search"
18}

在非本地浏览器中访问 http://ES所在机器的IP:9200/,正常的话同样也可以得到上面的信息。
image202112310934021de4oar.png

若尝试使用 root 用户启动,则会报错:

 1[root@localhost bin]# ./elasticsearch
 2[2021-12-31T02:26:51,276][ERROR][o.e.b.ElasticsearchUncaughtExceptionHandler] [localhost.localdomain] uncaught exception in thread [main]
 3org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
 4        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:174) ~[elasticsearch-7.8.1.jar:7.8.1]
 5        at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161) ~[elasticsearch-7.8.1.jar:7.8.1]
 6        at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-7.8.1.jar:7.8.1]
 7        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:127) ~[elasticsearch-cli-7.8.1.jar:7.8.1]
 8        at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-7.8.1.jar:7.8.1]
 9        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126) ~[elasticsearch-7.8.1.jar:7.8.1]
10        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-7.8.1.jar:7.8.1]
11Caused by: java.lang.RuntimeException: can not run elasticsearch as root
12        at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:111) ~[elasticsearch-7.8.1.jar:7.8.1]
13        at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:178) ~[elasticsearch-7.8.1.jar:7.8.1]
14        at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:393) ~[elasticsearch-7.8.1.jar:7.8.1]
15        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170) ~[elasticsearch-7.8.1.jar:7.8.1]
16        ... 6 more
17uncaught exception in thread [main]
18java.lang.RuntimeException: can not run elasticsearch as root
19        at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:111)
20        at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:178)
21        at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:393)
22        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170)
23        at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161)
24        at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86)
25        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:127)
26        at org.elasticsearch.cli.Command.main(Command.java:90)
27        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126)
28        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92)
29For complete error details, refer to the log at /home/esapp/elasticsearch-7.8.1/logs/elasticsearch.log

相关资料

Install Elasticsearch from archive on Linux or MacOS

Important System Configuration