Elasticsearch 安装

本文详细介绍了如何在 CentOS 7.6 系统上安装 Elasticsearch 7.9.3,包括下载、创建新用户、设置权限、解决启动异常问题,如文件描述符、线程数和虚拟内存限制。此外,还讲解了配置网络主机为0.0.0.0以允许外部访问,以及启动和停止ES的步骤。文章中还提到了配置文件`elasticsearch.yml`的重要参数,并展示了分词器的测试结果,指出默认分词器与预期不符。
摘要由CSDN通过智能技术生成

Elasticsearch 安装

linux:centos7.6
es版本:elasticsearch-7.9.3
jdk:openJDK

1 ES下载

官网下载:https://www.elastic.co/cn/downloads/elasticsearch
中文社区下载:https://elasticsearch.cn/download/
百度网盘下载:https://pan.baidu.com/s/1Qv5PZW0hny_Ahp7DRMXd3w 提取码:o3vf

2 安装配置

博主将包上传到 /home/es 目录下

2.1 解压

# 解压到当前目录下
[root@localhost es]# tar -zxvf elasticsearch-7.9.3-linux-x86_64.tar.gz
# 解压成功目录
[root@localhost es]# ls
elasticsearch-7.9.3  elasticsearch-7.9.3-linux-x86_64.tar.gz
2.1.1 es目录结构介绍

在这里插入图片描述

注意:es不能在root用户下启动,必须创建新的用户,用来启动es

2.2 创建新用户

# 1. 创建新用户
useradd es
# 2. 修改密码
passwd es
# 3. 切换用户
su - es # 密码登录
su es # 无需密码登录

2.2 为用户设置权限,否则启动会报错

chown -R es:es 当前es的安装目录(博主目录/home/es/elasticsearch-7.9.3)

2.2 es授权

为使浏览器允许访问,需要做如下配置:
配置文件为es解压目录下:/config/elasticsearch.yml

# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 0.0.0.0

2.2 启动过程可能引起的异常

  • max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
  • max number of threads [3818] for user [es] is too low, increase to at least [4096]
  • max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

原因:系统配置参数与es启动配置参数不符,需要配置

  1. 修改/etc/security/limits.conf文件,在底部新增,属于*也要新增
*               soft    nofile          65536
*               hard    nofile          65536
*               soft    nproc           4096
*               hard    nproc           4096

服务器重启生效,查看

[root@localhost config]# ulimit -Hn
65536
[root@localhost config]# ulimit -Sn
65536
[root@localhost config]# ulimit -Hu
4096
[root@localhost config]# ulimit -Su
4096
  1. 最大线程个数太低。修改配置文件/etc/security/limits.conf,增加配置
# /etc/security/limits.conf增加
vm.max_map_count=262144
#执行命令sysctl -p生效
[root@localhost config]# sysctl -p
vm.max_map_count = 262144

3 启动停止

3.1 启动

# 守护进程启动,es解压目录下/bin
./elasticsearch -d

3.2 关闭

es只能强制关闭:kill -9 pid

[root@localhost bin]# ps -ef|grep elasticsearch
es         8946      1  0 11:21 pts/0    00:00:56 /usr/lib/jvm/java-1.8.0/bin/java -Xshare:auto -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=10 -XX:+AlwaysPreTouch -Xss1m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -XX:-OmitStackTraceInFastThrow -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dio.netty.allocator.numDirectArenas=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Djava.locale.providers=SPI,JRE -Xms1g -Xmx1g -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -Djava.io.tmpdir=/tmp/elasticsearch-4938857371574732973 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=data -XX:ErrorFile=logs/hs_err_pid%p.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintTenuringDistribution -XX:+PrintGCApplicationStoppedTime -Xloggc:logs/gc.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=32 -XX:GCLogFileSize=64m -XX:MaxDirectMemorySize=536870912 -Des.path.home=/home/es/elasticsearch-7.9.3 -Des.path.conf=/home/es/elasticsearch-7.9.3/config -Des.distribution.flavor=default -Des.distribution.type=tar -Des.bundled_jdk=true -cp /home/es/elasticsearch-7.9.3/lib/* org.elasticsearch.bootstrap.Elasticsearch -d
es         8960   8946  0 11:21 pts/0    00:00:00 /home/es/elasticsearch-7.9.3/modules/x-pack-ml/platform/linux-x86_64/bin/controller
root       9143   7086  0 13:35 pts/0    00:00:00 grep --color=auto elasticsearch
[root@localhost bin]# kill -9 8946

3.3 启动验证

浏览器访问:xxx.xxx.xxx.xxx:9200

{
  "name" : "localhost.localdomain",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "qO9-FxOAQkarL_ieSn_R7A",
  "version" : {
    "number" : "7.9.3",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "c4138e51121ef06a6404866cddc601906fe5c868",
    "build_date" : "2020-10-16T10:36:16.141335Z",
    "build_snapshot" : false,
    "lucene_version" : "8.6.2",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

4 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 see the documentation for further information on configuration options:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.html>
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
# 集群名称,默认是elasticsearch
# cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
# 节点名称,默认从elasticsearch-2.4.3/lib/elasticsearch-2.4.3.jar!config/names.txt中随机选择一个名称
# node.name: node-1
#
# Add custom attributes to the node:
#
# node.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
# 可以指定es的数据存储目录,默认存储在es_home/data目录下
# path.data: /path/to/data
#
# Path to log files:
# 可以指定es的日志存储目录,默认存储在es_home/logs目录下
# path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
# 锁定物理内存地址,防止elasticsearch内存被交换出去,也就是避免es使用swap交换分区
# bootstrap.memory_lock: true
#
#
#
# 确保ES_HEAP_SIZE参数设置为系统可用内存的一半左右
# Make sure that the `ES_HEAP_SIZE` environment variable is set to about half the memory
# available on the system and that the owner of the process is allowed to use this limit.
#
# 当系统进行内存交换的时候,es的性能很差
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
#
# 为es设置ip绑定,默认是127.0.0.1,也就是默认只能通过127.0.0.1 或者localhost才能访问
# es1.x版本默认绑定的是0.0.0.0 所以不需要配置,但是es2.x版本默认绑定的是127.0.0.1,需要配置
# Set the bind address to a specific IP (IPv4 or IPv6):
#
# network.host: 192.168.0.1
#
#
# 为es设置自定义端口,默认是9200
# 注意:在同一个服务器中启动多个es节点的话,默认监听的端口号会自动加1:例如:9200,9201,9202...
# Set a custom port for HTTP:
#
# http.port: 9200
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html>
#
# --------------------------------- Discovery ----------------------------------
#
# 当启动新节点时,通过这个ip列表进行节点发现,组建集群
# 默认节点列表:
# 127.0.0.1,表示ipv4的回环地址。
# [::1],表示ipv6的回环地址
#
# 在es1.x中默认使用的是组播(multicast)协议,默认会自动发现同一网段的es节点组建集群,
# 在es2.x中默认使用的是单播(unicast)协议,想要组建集群的话就需要在这指定要发现的节点信息了。
# 注意:如果是发现其他服务器中的es服务,可以不指定端口[默认9300],如果是发现同一个服务器中的es服务,就需要指定端口了。
# 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"]
#
#
#
#
# 通过配置这个参数来防止集群脑裂现象 (集群总节点数量/2)+1
# Prevent the "split brain" by configuring the majority of nodes (total number of nodes / 2 + 1):
#
# discovery.zen.minimum_master_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-discovery.html>
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
# 一个集群中的N个节点启动后,才允许进行数据恢复处理,默认是1
# gateway.recover_after_nodes: 3
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-gateway.html>
#
# ---------------------------------- Various -----------------------------------
# 在一台服务器上禁止启动多个es服务
# Disable starting multiple nodes on a single system:
#
# node.max_local_storage_nodes: 1
#
# 设置是否可以通过正则或者_all删除或者关闭索引库,默认true表示必须需要显式指定索引库名称
# 生产环境建议设置为true,删除索引库的时候必须显式指定,否则可能会误删索引库中的索引库。
# Require explicit names when deleting indices:
#
# action.destructive_requires_name: true

5 其他异常

5.1 启动异常

  1. 异常信息
ERROR: [1] bootstrap checks failed
[1]: 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
  1. 修改elasticsearch.yml配置文件
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.seed_hosts: ["host1", "host2"]
cluster.initial_master_nodes: ["node-1"]

6 分析器

6.1 默认分词器测试

请求工具:postman
请求uri:192.168.163.104:9200/_analyze
body:{“analyzer”: “chinese”,“text”:“tom今天天气真好”}

在这里插入图片描述
分词结果:中文分词与实际预期分词不符,差异太大

{
    "tokens": [
        {
            "token": "tom",
            "start_offset": 0,
            "end_offset": 3,
            "type": "<ALPHANUM>",
            "position": 0
        },
        {
            "token": "今",
            "start_offset": 3,
            "end_offset": 4,
            "type": "<IDEOGRAPHIC>",
            "position": 1
        },
        {
            "token": "天",
            "start_offset": 4,
            "end_offset": 5,
            "type": "<IDEOGRAPHIC>",
            "position": 2
        },
        {
            "token": "天",
            "start_offset": 5,
            "end_offset": 6,
            "type": "<IDEOGRAPHIC>",
            "position": 3
        },
        {
            "token": "气",
            "start_offset": 6,
            "end_offset": 7,
            "type": "<IDEOGRAPHIC>",
            "position": 4
        },
        {
            "token": "真",
            "start_offset": 7,
            "end_offset": 8,
            "type": "<IDEOGRAPHIC>",
            "position": 5
        },
        {
            "token": "好",
            "start_offset": 8,
            "end_offset": 9,
            "type": "<IDEOGRAPHIC>",
            "position": 6
        }
    ]
}

6.2 IK分词器介绍

https://www.sojson.com/blog/82.html

安装Elasticsearch,你可以按照以下步骤进行操作: 1. 首先,你可以从官方网站下载Elasticsearch安装包。官网下载地址为。 2. 下载完成后,解压缩安装包。你可以使用以下命令解压缩tar.gz文件: ```shell tar -zxvf elasticsearch-7.8.1-linux-x86_64.tar.gz ``` 3. 解压缩完成后,进入解压缩后的目录,找到配置文件elasticsearch.yml。你可以使用以下命令来编辑该文件并进行配置: ```shell vim /usr/local/elasticsearch-7.13.2/config/elasticsearch.yml ``` 4. 在编辑配置文件之前,你可能需要了解一些配置选项以满足你的需求。根据你的环境和需求,修改elasticsearch.yml文件中的配置选项。 5. 当你完成了配置文件的修改后,保存并退出编辑器。 6. 现在,可以启动Elasticsearch了。你可以使用以下命令启动Elasticsearch: ```shell ./bin/elasticsearch ``` 这是一个基本的安装和启动Elasticsearch的过程。请注意,具体的步骤可能因操作系统和版本而略有不同。确保你仔细阅读官方文档以获取更详细的安装指南。 关于卸载Elasticsearch,你可以按照以下步骤进行操作: 1. 首先,你可以通过以下命令来查看已安装Elasticsearch软件包: ```shell dpkg -l | grep elastic ``` 2. 接下来,你可以使用以下命令查找Elasticsearch安装关联: ```shell dpkg -L elasticsearch ``` 3. 然后,使用以下命令来移除已安装Elasticsearch软件包: ```shell dpkg -P elasticsearch ``` 4. 继续使用以下命令查找未卸载的目录和文件: ```shell find ./elasticsearch | grep elasticsearch ``` 注意:根据你的环境和安装方式,可能会有其他目录和文件与Elasticsearch相关。 5. 最后,根据你的环境,使用以下命令来移除相关的目录和文件: ```shell rm -r /var/lib/elasticsearch rm /var/lib/dpkg/info/elasticsearch.* rm /etc/default/elasticsearch rm /etc/init.d/elasticsearch ``` 请根据自己的具体环境和需求来执行移除目录和文件的操作,确保操作正确无误。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [一、ElasticSearch安装](https://blog.csdn.net/m0_51295655/article/details/123193074)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值