centos7下安装ElasticSearch

CentOS7安装ElasticSearch7.12

1.安装JDK

安装JDK 并配置环境变量,配置如下:

1
2
3
4
export JAVA_HOME=/usr/local/software/jdk1.8.0_281
export JRE_HOME=/usr/local/software/jdk1.8.0_281/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin

2.安装ElasticSearch

注意:我下载的是7版本,需要jdk11的支持,但是开发环境我选择 jdk1.8

​下载6版本的可以使用jdk1.8

ElasticSearch文档地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/elasticsearch-intro.html
ElasticSearch下载地址:https://www.elastic.co/cn/downloads/elasticsearch

1
tar -zxvf elasticsearch-7.12.0-linux-x86_64.tar.gz

下载完成后的地址路径如下:

3.启动ES,并解决ES启动报错

进入bin目录下面执行 ./elasticsearch

错误一:ES7.12 需要使用JDK11的版本

报错,提示信息如下:

1
2
3
4
distribution with a bundled JDK, ensure the JAVA_HOME environment variable is not set.
warning: usage of JAVA_HOME is deprecated, use ES_JAVA_HOME
Future versions of Elasticsearch will require Java 11; your Java version from [/usr/local/software/jdk1.8.0_281/jre] does not meet this requirement. Consider switching to a distribution of Elasticsearch with a bundled JDK. If you are already using a distribution with a bundled JDK, ensure the JAVA_HOME environment variable is not set.
[2021-04-18T19:54:57,709][ERROR][o.e.b.ElasticsearchUncaughtExceptionHandler] [MiWiFi-R3-srv] uncaught exception in thread [main]

这个意思提示我们,使用ES7.12需要jdk11的版本
下载安装jdk11,并修改es需要的jdk环境变量
修改vim elasticsearch 文件,并在文件最上方添加如下配置:

1
2
3
4
5
6
7
8
9
10
#配置自己的jdk11
export JAVA_HOME=/usr/local/jdk-11.0.10/
export PATH=$JAVA_HOME/bin:$PATH

#添加jdk判断
if [ -x "$JAVA_HOME/bin/java" ]; then
JAVA="/usr/local/jdk-11.0.10/bin/java"
else
JAVA=`which java`
fi

再次启动ES ./elasticsearch

错误二:Es提示我们不可以使用root用户

提示信息如下:

1
2
3
4
5
6
7
8
9
10
11
12
java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:101)
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:168)
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:397)
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:159)
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:150)
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:75)
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:116)
at org.elasticsearch.cli.Command.main(Command.java:79)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:115)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:81)
For complete error details, refer to the log at /usr/local/software/elasticsearch-7.12.0/logs/elasticsearch.log

解决办法:创建新用户,并用新用户进行启动
创建用户组:groupadd esgroup
创建es用户:useradd esuser -g esgroup -p 123456
然后使用chown命令来让这个文件夹属于这个用户
chown -R esuser:esgroup elasticsearch-7.12.0
切换用户,使用新用户esuser进行服务启动

再次启动,启动成功后,使用curl 127.0.0.1:9200 进行访问,如果返回一段json。说明启动成功了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@MiWiFi-R3-srv bin]# curl 127.0.0.1:9200
{
"name" : "MiWiFi-R3-srv",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "htoKhKvMRfCpzscUQLWWAQ",
"version" : {
"number" : "7.12.0",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "78722783c38caa25a70982b5b042074cde5d3b3a",
"build_date" : "2021-03-18T06:17:15.410153305Z",
"build_snapshot" : false,
"lucene_version" : "8.8.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}

但是ES默认支持localhost启动,修改配置文件config下面的vim elasticsearch.yml 如下图所示

启动完成后,重新启动,又出现了新的警告信息

错误三:Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release

这个是警告信息,垃圾回收器的问题,可以不修改

1
Java HotSpot(TM) 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.

错误四:提示:在启动Elasticsearch之前,您必须解决以下行中描述的要点

提示信息如下:

1
2
3
4
5
6
ERROR: [3] bootstrap checks failed. You must address the points described in the following [3] lines before starting Elasticsearch.
bootstrap check failure [1] of [3]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
bootstrap check failure [2] of [3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
bootstrap check failure [3] of [3]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
ERROR: Elasticsearch did not exit normally - check the logs at /usr/local/software/elasticsearch-7.12.0/logs/elasticsearch.log

4.第一个错误,意思大概是:elasticsearch进程的文件描述符[4096]过低,增加到至少[65535]

解决方案:
修改 vim /etc/security/limits.conf
在文件最下方加入如下配置:
注意:第一列是你刚才创建的用户名称,我刚才创建的用户是esuser

esuser soft nofile 65536
esuser hard nofile 65536
esuser soft nproc 4096
esuser hard nproc 4096

5.第二个错误,意思大概是:虚拟机最大的虚拟内存区域。max_map_count[65530]太低,增加到至少[262144]

解决办法:进入/etc 修改sysctl.conf文档,里面都是注释,添加如下面一行配置

1
vm.max_map_count=655360 推荐信息提示至少262144

保存信息wq
注意:配置如果想要生效,需要执行如下配置:

6.第三个错误:

解决办法:在elasticsearch的config目录下,修改elasticsearch.yml配置文件,将下面的配置加入到该配置文件中:
注意:这里的node-1为node-name配置的值

1
cluster.initial_master_nodes: ["node-1"] 

注意默认情况下:node-name的值是注释掉的。这里要放开

7. 重新启动ES 看看效果,启动完成后,还是提示这个错误

check failure [1] of [1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
这种情况,可能是配置没有生效,重启虚拟机再看看效果

重新启动完成后,再次启动提示start,表示已经正常启动了

8.windows电脑无法直接访问

开放9200端口

1
2
firewall-cmd --zone=public --add-port=9200/tcp --permanent
firewall-cmd --reload

使用windows客户端访问,能看到正常返回json了