一文教懂你关于Elasticsearch的安装配置

请一定要更新JDK到最新版本

我们去官网https://www.elastic.co/cn/elasticsearch/下载最新版本的软件包并解压软件包。
然后启动运行elasticsearch

./bin/elasticsearch

首先会报一个错误:

[ERROR][o.e.b.Bootstrap] [01] Exception
java.lang.RuntimeException: can not run elasticsearch as root

这是因为我们不能以root用户进行运行导致的。我们只需新增一个用户赋予新用户启动elasticsearch的权限即可。

useradd esuser
passwd esuser
//先移动elasticsearch到usr/local/elasticsearch下
mv elasticsearch usr/local/elasticsearch
//然后进行赋权
chown -R esuser:esuser usr/local/elasticsearch
//之后切换到新建用户进行启动即可
su esuser
./usr/local/elasticsearch/bin/elasticsearch

启动报错,内容如下:

  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 /opt/elasticsearch/logs/elasticsearch.log

大致是说我们3个配置没有做,也对,刚下载解压后直接运行,肯定会有很多问题:

  1. bootstrap check failure 1 of [3]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]

这个是说ElasticSearch进程的最大文件描述大小需要65535,而当前是4096,解决办法是修改 /etc/security/limits.conf 文件,在末尾加上(存在则修改,数值不能比要求的小):

    * soft nofile 65535
    * hard nofile 65535
    * soft nproc 65535
    * hard nproc 65535

如果limits.conf文件无法修改,可尝试修改文件权限之后再次修改,如还是不行,则可进行临时修改。命令如下:

ulimit -n 65535 //临时修改最大打开文件数
ulimit -Hn 4096 //临时修改最大进程数
  1. bootstrap check failure [2] of [3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

这是说最大虚拟内存太小(vm.max_map_count配置),至少需要262144,当前为65530,解决办法是修改 /etc/sysctl.conf 文件,在末尾加上(存在则修改,数值不能比要求的小):

vm.max_map_count=262144

//随后执行以下命令,立即生效
/sbin/sysctl -p
  1. 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

这是说我们没有对ElasticSearch发现进行配置,至少需要配置discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes中的一个:

  • discovery.seed_hosts:集群节点列表,每个值应采用host:port或host的形式(其中port默认为设置transport.profiles.default.port,如果未设置则返回transport.port)
  • discovery.seed_providers:集群节点列表的提供者,作用就是获取discovery.seed_hosts,比如使用文件指定节点列表
  • cluster.initial_master_nodes:初始化时master节点的选举列表,一般使用node.name(节点名称)配置指定,配置旨在第一次启动有效,启动之后会保存,下次启动会读取保存的数据

比如这里我全部的配置如下(config/elasticsearch.yml),更多配置参考官网:https://www.elastic.co/guide/en/elasticsearch/reference/current/settings.html

    # 启动地址,如果不配置,只能本地访问
    network.host: 0.0.0.0
    # 节点名称
    node.name: node-name
    # 节点列表
    discovery.seed_hosts: ["你的子服务器ip"]
    # 初始化时master节点的选举列表
    cluster.initial_master_nodes: [ "node-name" ]
    # 集群名称
    cluster.name: cluster-name
    # 对外提供服务的端口
    http.port: 9200
    # 内部服务端口
    transport.port: 9300    
    # 跨域支持
    http.cors.enabled: true
    # 跨域访问允许的域名地址(正则)
    http.cors.allow-origin: /.*/
    #设置索引数据的存储路径
    path.data: /usr/local/elasticsearch/data
    #设置日志的存储路径
    path.logs: /usr/local/elasticsearch/logs
    #设置集群中master节点的初始列表,可以通过这些节点来自动发现新加入集群的节点
    discovery.zen.ping.unicast.hosts: ["127.0.0.1","你的ip:9200"]
注:如果还报上面1、2的异常,那可能需要重启一下系统了。

接着启动,结果提示jdk版本不对,原来我自己配置了环境变量JAVA_HOME,而ElasticSearch就是用了这个环境变量对应的java来运行,即发现jdk版本不对,另外还warning,JAVA_HOME环境变量已经弃用了,使用ES_JAVA_HOME代替:

  warning: usage of JAVA_HOME is deprecated, use ES_JAVA_HOME
  Future versions of Elasticsearch will require Java 11; your Java version from [/opt/jdk1.8.0_202/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.

因为ElasticSearch包中包含了JDK,所以我们可以直接使用,有两种使用办法:

1、添加环境变量ES_JAVA_HOME指向ElasticSearch包中包含了JDK目录

2、修改bin/elasticsearch-env中代码,我们注释掉JAVA_HOME部分的判断:
1033563-20210421144144373-2108640179.png

一切配置完成之后,重启服务器并启动elasticsearch。访问你的elasticsearch地址,如http://192.168.0.1:9200,如果出现以下json串就表示安装成功啦!

{
  "name": "node-1",
  "cluster_name": "linlianlai",
  "cluster_uuid": "YBKSt_D3SGywU7YyJ3Xeog",
  "version": {
    "number": "7.16.0",
    "build_flavor": "default",
    "build_type": "tar",
    "build_hash": "6fc81662312141fe7691d7c1c91b8658ac17aa0d",
    "build_date": "2021-12-02T15:46:35.697268109Z",
    "build_snapshot": false,
    "lucene_version": "8.10.1",
    "minimum_wire_compatibility_version": "6.8.0",
    "minimum_index_compatibility_version": "6.0.0-beta1"
  },
  "tagline": "You Know, for Search"
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

就叫我菜菜吧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值