ElasticSearch全文搜索引擎之Linux版安装教程

目录

一、简介

二、Linux下安装ElasticSearch


一、简介

前面我们已经在windows下搭建了es环境,但是在实际工作中,基本上都是部署在Linux系统中,本篇文章将介绍如何在Linux下搭建elasticsearch环境,并通过配置实现外网可以访问到es。

二、Linux下安装ElasticSearch

【a】下载linux版本的es压缩包

下载地址:https://www.elastic.co/cn/downloads/elasticsearch

elasticsearch官网下载非常慢,建议小伙伴们去找国内活雷锋分享的百度云盘下载,这里笔者以elasticsearch-7.6.2-linux-x86_64.tar.gz为例讲解linux下es的详细安装步骤。 

因为es7.6 已经内置了jdk,所以机器不需要有java环境。如果机器有java环境,务必保持和es版本匹配的java环境。

【b】将下载好的压缩包上传到linux服务器中

[root@localhost /]# mkdir es
[root@localhost /]# cd es
[root@localhost es]# ll
total 0
[root@localhost es]# rz

 【c】解压缩elasticsearch-7.6.2-linux-x86_64.tar.gz

[root@localhost es]# ll
total 289532
-rw-r--r--. 1 root root 296477546 Oct 10 14:42 elasticsearch-7.6.2-linux-x86_64.tar.gz
[root@localhost es]# tar -zxvf elasticsearch-7.6.2-linux-x86_64.tar.gz 

 

 【d】解压缩完成后,我们进入到bin目录

[root@localhost es]# cd elasticsearch-7.6.2/bin/
[root@localhost bin]# ll
total 19604
-rwxr-xr-x. 1 root root     1915 Mar 26  2020 elasticsearch
-rwxr-xr-x. 1 root root      491 Mar 26  2020 elasticsearch-certgen
-rwxr-xr-x. 1 root root      483 Mar 26  2020 elasticsearch-certutil
-rwxr-xr-x. 1 root root      982 Mar 26  2020 elasticsearch-cli
-rwxr-xr-x. 1 root root      433 Mar 26  2020 elasticsearch-croneval
-rwxr-xr-x. 1 root root     4313 Mar 26  2020 elasticsearch-env
-rwxr-xr-x. 1 root root     1828 Mar 26  2020 elasticsearch-env-from-file
-rwxr-xr-x. 1 root root      121 Mar 26  2020 elasticsearch-keystore
-rwxr-xr-x. 1 root root      440 Mar 26  2020 elasticsearch-migrate
-rwxr-xr-x. 1 root root      126 Mar 26  2020 elasticsearch-node
-rwxr-xr-x. 1 root root      172 Mar 26  2020 elasticsearch-plugin
-rwxr-xr-x. 1 root root      431 Mar 26  2020 elasticsearch-saml-metadata
-rwxr-xr-x. 1 root root      438 Mar 26  2020 elasticsearch-setup-passwords
-rwxr-xr-x. 1 root root      118 Mar 26  2020 elasticsearch-shard
-rwxr-xr-x. 1 root root      427 Mar 26  2020 elasticsearch-sql-cli
-rwxr-xr-x. 1 root root 19986912 Mar 26  2020 elasticsearch-sql-cli-7.6.2.jar
-rwxr-xr-x. 1 root root      426 Mar 26  2020 elasticsearch-syskeygen
-rwxr-xr-x. 1 root root      426 Mar 26  2020 elasticsearch-users
-rwxr-xr-x. 1 root root      346 Mar 26  2020 x-pack-env
-rwxr-xr-x. 1 root root      354 Mar 26  2020 x-pack-security-env
-rwxr-xr-x. 1 root root      353 Mar 26  2020 x-pack-watcher-env
[root@localhost bin]# ./elasticsearch

启动elasticsearch,我们发现启动报错:

OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
[2020-10-10T15:58:13,175][ERROR][o.e.b.ElasticsearchUncaughtExceptionHandler] [localhost.localdomain] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
	at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:174) ~[elasticsearch-7.6.2.jar:7.6.2]
	at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161) ~[elasticsearch-7.6.2.jar:7.6.2]
	at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-7.6.2.jar:7.6.2]
	at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:125) ~[elasticsearch-cli-7.6.2.jar:7.6.2]
	at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-7.6.2.jar:7.6.2]
	at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126) ~[elasticsearch-7.6.2.jar:7.6.2]
	at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-7.6.2.jar:7.6.2]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
	at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:105) ~[elasticsearch-7.6.2.jar:7.6.2]
	at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:172) ~[elasticsearch-7.6.2.jar:7.6.2]
	at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:349) ~[elasticsearch-7.6.2.jar:7.6.2]
	at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170) ~[elasticsearch-7.6.2.jar:7.6.2]
	... 6 more
uncaught exception in thread [main]
java.lang.RuntimeException: can not run elasticsearch as root
	at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:105)
	at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:172)
	at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:349)
	at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170)
	at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161)
	at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86)
	at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:125)
	at org.elasticsearch.cli.Command.main(Command.java:90)
	at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126)
	at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92)
For complete error details, refer to the log at /es/elasticsearch-7.6.2/logs/elasticsearch.log

上述错误是因为elasticsearch默认是不能以root 用户进行启动,所以我们需要自己创建一个用户用于启动elasticsearch。 

【e】创建用户组和用户es

[root@localhost bin]# groupadd es
groupadd: group 'es' already exists
[root@localhost bin]# useradd -g es es
useradd: user 'es' already exists
[root@localhost bin]# chown -R es:es /es/elasticsearch-7.6.2/
[root@localhost bin]# ll
total 19604
-rwxr-xr-x. 1 es es     1915 Mar 26  2020 elasticsearch
-rwxr-xr-x. 1 es es      491 Mar 26  2020 elasticsearch-certgen
-rwxr-xr-x. 1 es es      483 Mar 26  2020 elasticsearch-certutil
-rwxr-xr-x. 1 es es      982 Mar 26  2020 elasticsearch-cli
-rwxr-xr-x. 1 es es      433 Mar 26  2020 elasticsearch-croneval
-rwxr-xr-x. 1 es es     4313 Mar 26  2020 elasticsearch-env
-rwxr-xr-x. 1 es es     1828 Mar 26  2020 elasticsearch-env-from-file
-rwxr-xr-x. 1 es es      121 Mar 26  2020 elasticsearch-keystore
-rwxr-xr-x. 1 es es      440 Mar 26  2020 elasticsearch-migrate
-rwxr-xr-x. 1 es es      126 Mar 26  2020 elasticsearch-node
-rwxr-xr-x. 1 es es      172 Mar 26  2020 elasticsearch-plugin
-rwxr-xr-x. 1 es es      431 Mar 26  2020 elasticsearch-saml-metadata
-rwxr-xr-x. 1 es es      438 Mar 26  2020 elasticsearch-setup-passwords
-rwxr-xr-x. 1 es es      118 Mar 26  2020 elasticsearch-shard
-rwxr-xr-x. 1 es es      427 Mar 26  2020 elasticsearch-sql-cli
-rwxr-xr-x. 1 es es 19986912 Mar 26  2020 elasticsearch-sql-cli-7.6.2.jar
-rwxr-xr-x. 1 es es      426 Mar 26  2020 elasticsearch-syskeygen
-rwxr-xr-x. 1 es es      426 Mar 26  2020 elasticsearch-users
-rwxr-xr-x. 1 es es      346 Mar 26  2020 x-pack-env
-rwxr-xr-x. 1 es es      354 Mar 26  2020 x-pack-security-env
-rwxr-xr-x. 1 es es      353 Mar 26  2020 x-pack-watcher-env
[root@localhost bin]# 

注意:由于笔者这里之前已经创建过用户组和用户es,所以提示已存在,初次创建时是不会提示的。

创建完成后,我们切换用户再次启动es:

[root@localhost bin]# su es
[es@localhost bin]$ ./elasticsearch

 如下图,es启动成功,可以看到监听地址为:http://127.0.0.1:9200/

通过下面的命令查看es信息:

[root@localhost /]# curl -X GET 127.0.0.1:9200
{
  "name" : "localhost.localdomain",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "bGQWfqOfQwimmU5iES61jA",
  "version" : {
    "number" : "7.6.2",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
    "build_date" : "2020-03-26T06:34:37.794943Z",
    "build_snapshot" : false,
    "lucene_version" : "8.4.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

 可以看到,跟之前windows版本的提示信息一致,输出出默认的集群名称、elasticsearch版本信息等,说明我们linux下的es安装并且启动成功。

【f】配置外网访问

可以看到,前面我们使用的是http://127.0.0.1:9200/本机进行访问,但是实际工作中,我们更多时候需要支持外网能直接访问es,所以需要我们做一些配置,以支持es外网访问。

[root@localhost /]# cd es/elasticsearch-7.6.2/
[root@localhost elasticsearch-7.6.2]# ll
total 552
drwxr-xr-x.  2 es es   4096 Mar 26  2020 bin
drwxr-xr-x.  2 es es    178 Oct 10 15:58 config
drwxrwxr-x.  3 es es     19 Oct 10 16:03 data
drwxr-xr-x.  9 es es    107 Mar 26  2020 jdk
drwxr-xr-x.  3 es es   4096 Mar 26  2020 lib
-rw-r--r--.  1 es es  13675 Mar 26  2020 LICENSE.txt
drwxr-xr-x.  2 es es   4096 Oct 10 16:03 logs
drwxr-xr-x. 38 es es   4096 Mar 26  2020 modules
-rw-r--r--.  1 es es 523209 Mar 26  2020 NOTICE.txt
drwxr-xr-x.  2 es es      6 Mar 26  2020 plugins
-rw-r--r--.  1 es es   8164 Mar 26  2020 README.asciidoc
[root@localhost elasticsearch-7.6.2]# cd config/
[root@localhost config]# ll
total 40
-rw-rw----. 1 es es   199 Oct 10 15:58 elasticsearch.keystore
-rw-rw----. 1 es es  2831 Mar 26  2020 elasticsearch.yml
-rw-rw----. 1 es es  2301 Mar 26  2020 jvm.options
-rw-rw----. 1 es es 17545 Mar 26  2020 log4j2.properties
-rw-rw----. 1 es es   473 Mar 26  2020 role_mapping.yml
-rw-rw----. 1 es es   197 Mar 26  2020 roles.yml
-rw-rw----. 1 es es     0 Mar 26  2020 users
-rw-rw----. 1 es es     0 Mar 26  2020 users_roles
[root@localhost config]# vim elasticsearch.yml 

修改network.host对应的值:指定Linux服务器IP地址。

network.host: 192.168.179.133

修改完成保存退出,我们重启es:

如上图,启动失败,报了三个错误,下面我们一个一个解决:

(1)、max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]

解决方法:编辑 /etc/security/limits.conf,追加一些配置信息。

vim /etc/security/limits.conf

在文件最后面添加如下内容:

* soft nofile 65536
* hard nofile 65536

(2)、max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

解决方法:往/etc/sysctl.conf文件中追加一些配置,具体命令如下:

su root
echo "vm.max_map_count=262144" > /etc/sysctl.conf
sysctl -p

 (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

解决方法:指定集群名称、当前节点名称以及初始化主节点配置信息。

cluster.name: my-application
node.name: node-1
cluster.initial_master_nodes: ["node-1"]

 以上三个地方修改完成之后,我们再次启动elasticsearch:

我们发现,es成功启动,如上图可以看到,es已经成功绑定到我们指定的外网IP:

[root@localhost config]# curl -X GET 192.168.179.133:9200
{
  "name" : "node-1",
  "cluster_name" : "my-application",
  "cluster_uuid" : "bGQWfqOfQwimmU5iES61jA",
  "version" : {
    "number" : "7.6.2",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
    "build_date" : "2020-03-26T06:34:37.794943Z",
    "build_snapshot" : false,
    "lucene_version" : "8.4.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}
[root@localhost config]# 

下面我们通过浏览器访问:http://192.168.179.133:9200/

可以看到,外网也可以访问到elasticsearch服务了。

注意:

如果外网还是不能访问的话,可以尝试下面的方法处理一下,说不定就是下面的原因导致不能外网访问。

  • 禁用防火墙: systemctl disable firewalld
  • iptables对外暴露端口:vi /etc/sysconfig/iptables,添加如下两行:

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 9200 -j ACCEPT

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 9300 -j ACCEPT

至此,我们实现了Linux环境下ElasticSearch的安装、启动以及配置外网可访问,更多的配置可以参考elasticsearch官方文档,希望对小伙伴们的学习有所帮助。

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页
实付 19.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值