Mac/Centos安装elasticsearch7.14.0

一、安装包安装

1.下载

访问 ElasticSearch 官网 进行下载
image

2.解压

image

3.配置

进入 ElasticSearch 安装目录的 config 目录在 elasticsearch.yml 文件中添加如下信息。

action.auto_create_index: .security,.monitoring*,.watches,.triggered_watches,.watcher-history*
xpack.security.enabled: false
xpack.monitoring.enabled: true
xpack.graph.enabled: false
xpack.watcher.enabled: false
xpack.ml.enabled: false

4.启动

进入到 /bin目录,执行sh elasticsearch看到如下日志代表启动成功
image

5.错误

如果出现如下错误

[2024-01-21T10:52:54,283][INFO ][o.e.i.g.GeoIpDownloader  ] [localhost] updating geoip database [GeoLite2-ASN.mmdb]
[2024-01-21T10:52:54,377][ERROR][o.e.i.g.GeoIpDownloader  ] [localhost] error updating geoip database [GeoLite2-ASN.mmdb]
java.net.ConnectException: Connection refused

进入 ElasticSearch 安装目录的 config 目录在 elasticsearch.yml 文件中添加如下信息。

ingest.geoip.downloader.enabled: false

重新启动sh elasticsearch

如果在linux中报错

java.lang.RuntimeException: can not run elasticsearch as root

# 添加用户
adduser es
# 设置密码
passwd es
# 赋予权限
chown -R es:es /usr/local/elasticsearch-7.14.0
chmod 777 /usr/local/elasticsearch-7.14.0

完整命令

[root@lee bin]# adduser es
[root@lee bin]# passwd es
Changing password for user es.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@lee bin]# chown -R es:es /usr/local/elasticsearch-7.14.0
[root@lee bin]# chmod 777 /usr/local/elasticsearch-7.14.0
[root@lee bin]# su es
[root@lee bin]# cd /usr/local/elasticsearch-7.14.0/bin
[es@lee bin]$ sh elasticsearch

6.验证

浏览器输入 localhost:9200 如下图所示表示启动成功!
image

二、Docker安装

1.搜索镜像

镜像仓库
image

2.拉取镜像

拉取镜像文件

docker pull elasticsearch

说明:此处也可在Tags中选择指定版本的elasticsearch
image

我们这里安装的版本是elasticsearch:7.14.0

docker pull elasticsearch:7.14.0

3.查看镜像

docker images

4.创建所需文件夹

#1、创建Elasticsearch配置文件夹
mkdir -p /usr/local/data-docker/elasticsearch/config
​
#2、创建Elasticsearch数据文件夹
mkdir -p /usr/local/data-docker/elasticsearch/data
​
#3、创建Elasticsearch插件文件夹(如:ik)
mkdir -p /usr/local/data-docker/elasticsearch/plugins
​
#说明:目的将CentOS本地的文件夹映射到Elasticsearch容器,以实现容器数据的持久化到CentOS本地,以及通过CentOS本地文件夹内容的修改同步到容器

#4、创建并写入elasticsearch.yml配置,注意:http.host: 0.0.0.0 冒号后有一空格
echo "http.host: 0.0.0.0">>/usr/local/data-docker/elasticsearch/config/elasticsearch.yml

5.文件夹赋权

chmod -R 777 elasticsearch/

6.命令启动

docker run --name elasticsearch -p 9200:9200 \
 -p 9300:9300 \
 -e "discovery.type=single-node" \
 -e ES_JAVA_OPTS="-Xms64m -Xmx128m" \
  -v /usr/local/data-docker/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
 -v /usr/local/data-docker/elasticsearch/data:/usr/share/elasticsearch/data \
 -v /usr/local/data-docker/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
 -d elasticsearch

参数说明

--name elasticsearch:将Elasticsearch容器命名为 elasticsearch
-p 9200:9200:将容器的9200端口映射到宿主机9200端口
-p 9300:9300:将容器的9300端口映射到宿主机9300端口,目的是集群互相通信
-e "discovery.type=single-node":单例模式
-e ES_JAVA_OPTS="-Xms64m -Xmx128m":配置内存大小
-v /usr/local/data-docker/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml:将配置文件挂载到宿主机
-v /usr/local/data-docker/elasticsearch/data:/usr/share/elasticsearch/data:将数据文件夹挂载到宿主机
-v /usr/local/data-docker/elasticsearch/plugins:/usr/share/elasticsearch/plugins:将插件目录挂载到宿主机(需重启)
-d elasticsearch:后台运行容器,并返回容器ID

7.docker-compose启动

version: '2'
services:
  elasticsearch:
    container_name: elasticsearch
    image: elasticsearch
    ports:
      - "9200:9200"
    volumes:
      - /usr/local/data-docker/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
      - /usr/local/data-docker/elasticsearch/data:/usr/share/elasticsearch/data
      - /usr/local/data-docker/elasticsearch/plugins:/usr/share/elasticsearch/plugins
    environment:
      - "ES_JAVA_OPTS=-Xms64m -Xmx128m"
      - "discovery.type=single-node"
      - "COMPOSE_PROJECT_NAME=elasticsearch-server"
    restart: always

8.查看容器

docker ps

9.验证

#说明:安装完后,通过如下指令查看elasticsearch是否安装成功
curl http://localhost:9200

展示结果如下:

{
  "name" : "root",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "6kEOxLwQSkG3gFX7AGz_ww",
  "version" : {
    "number" : "7.14.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1",
    "build_date" : "2021-07-29T20:49:32.864135063Z",
    "build_snapshot" : false,
    "lucene_version" : "8.9.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}
  "tagline" : "You Know, for Search"
}

注意:如展示上边内容,说明docker安装elasticsearch成功,如远程访问无法展示上述内容,请关防火墙或防火墙开放9200、9300端口

10.Docker启动容器自启

docker update elasticsearch --restart=always

后台启动服务

1.创建es 的系统启动服务文件

进入到 cd /etc/init.d 目录

cd /etc/init.d

创建es系统启动服务文件

vi elasticsearch

2.编写启动脚本

#!/bin/bash
#chkconfig: 345 63 37
#description: elasticsearch
#processname: elasticsearch-7.14.0
export ES_HOME=/usr/local/elasticsearch-7.14.0
export ES_HOME_BIN=/usr/local/elasticsearch-7.14.0/bin
case $1 in
        start)
                su es<<!
                cd $ES_HOME_BIN
                ./elasticsearch -d
                exit
!
                echo "elasticsearch is started"
                ;;
        stop)
                pid=`cat $ES_HOME/pid`
                kill -9 $pid
                echo "elasticsearch is stopped"
                ;;
        restart)
                pid=`cat $ES_HOME/pid`
                kill -9 $pid
                echo "elasticsearch is stopped"
                sleep 1
                su es<<!
                cd $ES_HOME_BIN
                ./elasticsearch -d
                exit
!
                echo "elasticsearch is started"
        ;;
    *)
        echo "start|stop|restart"
        ;;
esac
exit 0

3.修改文件权限

chmod 777 elasticsearch

4.添加和删除服务并设置启动方式

  • 添加系统服务
chkconfig --add elasticsearch
  • 删除系统服务
chkconfig --del elasticsearch

5.关闭和启动服务

  • 启动
service elasticsearch start
  • 停止
service elasticsearch stop
  • 重启
service elasticsearch restart

6.设置服务是否开机启动

  • 开启
chkconfig elasticsearch on
  • 关闭
chkconfig elasticsearch off
posted @ 2024-01-21 13:10  leepandar  阅读(55)  评论(0编辑  收藏  举报