Linux shell脚本超详细安装elasticsearch

shell脚本超详细安装elasticsearch

#!/bin/bash

# 本文shell脚本自动化安装es

# 本文使用的是wget 下载elasticsearch包,所以需要你安装wget  ,命令:yum -y install wget

# elasticsearch下载地址:https://artifacts.elastic.co/downloads/elasticsearch/

# 安装包: elasticsearch-7.13.2-linux-x86_64.tar.gz

# 使用root权限安装
[ $UID -ne 0 ] && echo "need to be root so that" && exit 1

echo  "安装中, 请稍等 ... "
# es版本
esVersion="7.13.2"

# 下载wget
isInstallWget=`yum list installed | grep wget`

if [ $? -ne 0 ];then
   yum -y install wget &>/dev/null
fi

# 准备创建 es 安装目录
if [ -d /usr/local/es ];then
   echo "已存在es 目录,自行确认是否需要删除该目录";
   exit 1
fi

# 下载es
#nginx解压后的目录
dirName=elasticsearch-$esVersion-linux-x86_64
baseDirName=elasticsearch-$esVersion
[ ! -e /opt ] && mkdir /opt
isExistEsPackage=`ls /opt/$dirName.tar.gz`;
if [ $? -ne 0 ];then
  wget -P /opt/ "https://artifacts.elastic.co/downloads/elasticsearch/$dirName.tar.gz"
  if [ $? -ne 0 ];then
    echo "下载失败,该版本: $esVersion 不存在"
    exit 1;
  fi
fi
echo "es 下载已完成"
sleep 1

# 解压压缩包
if [ ! -e $baseDirName ];then
  # --no-same-owner 新增此参数 将解压出来的包 归属于当前用户
  tar --no-same-owner -xf "/opt/$dirName.tar.gz"  -C /opt/
fi

# 将解压下来的压缩包移到/usr/local/es
mv /opt/$baseDirName /usr/local/es
# 切换
cd /usr/local

if [ ! -d /usr/local/es ];then
   echo "es目录不存在,请检查后重试...";
   exit 1
fi
# 此处需要注意的是,es相关操作必须非root用户
# 所以需要创建es 用户
# 创建用户
if [ ! grep "es" /etc/passwd ];then
 useradd es
fi
# 创建所属组和所属用户
chown es:es -R /usr/local/es

# 由于es 需要java 环境
# 这里设置使用es 自带jdk
# 如下需要配置java环境,我们这里使用 es中的jdk
# 此处使用export 是不生效的,需要使用vi /etc/profile
# 所以需要使用sed 修改文本内容
if [ ! grep 'JAVA_PATH' /etc/profile] ;then
  sed -i '$aJAVA_PATH=\/usr\/local\/es\/jdk\/bin \nPATH=\$JAVA_PATH:$PATH \n$aexport $PATH' /etc/profile
  source /etc/profile
fi



#配置相关修改
<<ESCONFIG
   #修改文件 /usr/local/es/config/elasticsearch.yml
   1. network.host: 0.0.0.0
   2. http.port: 9200
   3. path.data: /usr/local/es/data
   4. path.logs: /usr/local/es/logs  #elasticsearch.log 需要加入执行权限
   5. cluster.name: my-application
   6. node.name: lunan
   7. cluster.initial_master_nodes: ["lunan"]
   # 配置修改
   vim /etc/sysctl.conf
   新增: vm.max_map_count=262144
   sysctl -p
   #修改/usr/local/es/config/jvm.options 下面两个值调小一些 如下:
   -Xms256m
   -Xmx256m
   
ESCONFIG

# 如需启动的话,需要切换到 es用户
# su - es
<<startEs
   # 启动es 需要切换到es 用户
   su - es
   # 浏览器访问 
   http://172.16.185.11:9200/
   #当如果访问不了的话,可以关闭一下防火墙
   # 查看防火墙状态 firewall-cmd --state
   firewall-cmd --state
   # 关闭防火墙
   service firewalld stop
   当然网上还有很多种关闭防火墙
   也可以不关闭防火墙
   添加开放9200端口
   firewall-cmd --zone=public --add-port=9200/tcp --permanent
   firewall-cmd --reload 
   当看到如下信息时就说明已经启动成功了
   {
     name: "lunan",
     cluster_name: "my-application",
     cluster_uuid: "n4A84SULSnCfD7HaEzRnVw",
     version: {
      number: "7.13.2",
      build_flavor: "default",
      build_type: "tar",
      build_hash: "4d960a0733be83dd2543ca018aa4ddc42e956800",
      build_date: "2021-06-10T21:01:55.251515791Z",
      build_snapshot: false,
      lucene_version: "8.8.2",
      minimum_wire_compatibility_version: "6.8.0",
      minimum_index_compatibility_version: "6.0.0-beta1"
     },
     tagline: "You Know, for Search"
   }  
startEs


<<autoStart
  #新建自动重启脚本
  # 创建 vim /etc/init.d/elasticsearch
  #新增权限
  chmod +x /etc/init.d/elasticsearch
  chkconfig --add elasticsearch
  chkconfig elasticsearch on
  service elastisearch stop|start|restart
autoStart

开机自动启动脚本

#!/bin/bash
#chkconfig: 345 63 37
#description: elasticsearch
#processname: elasticsearch

# es开机自动启动脚本

# 这个目录是你ES 所在的文件夹的目录

ES_HOME=/usr/local/es

case $1 in
start)
   su es<<!
   cd $ES_HOME
   # -d 指的是 后台进程运行 -p 指的是pid
   ./bin/elasticsearch -d -p pid
   exit
!
   echo "elasticsearch is started"
   ;;
stop)
   pid=`cat $ES_HOME/pid`
   kill -9 $pid
   echo "elasticsearch is stoped"
   ;;
restart)
   pid=`cat $ES_HOME/pid`
   kill -9 $pid
   echo "elasticsearch is stoped"
   sleep 1
   su es<<!
   cd $ES_HOME
   ./bin/elasticsearch -d -p pid
   exit
!
   echo "elasticsearch is started"
   ;;
*)
   echo "start|stop|restart"
   ;;
esac
exit 0
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

蓝颜~岁月

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

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

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

打赏作者

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

抵扣说明:

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

余额充值