shell脚本超详细安装elasticsearch
#!/bin/bash
[ $UID -ne 0 ] && echo "need to be root so that" && exit 1
echo "安装中, 请稍等 ... "
esVersion="7.13.2"
isInstallWget=`yum list installed | grep wget`
if [ $? -ne 0 ];then
yum -y install wget &>/dev/null
fi
if [ -d /usr/local/es ];then
echo "已存在es 目录,自行确认是否需要删除该目录";
exit 1
fi
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
tar --no-same-owner -xf "/opt/$dirName.tar.gz" -C /opt/
fi
mv /opt/$baseDirName /usr/local/es
cd /usr/local
if [ ! -d /usr/local/es ];then
echo "es目录不存在,请检查后重试...";
exit 1
fi
if [ ! grep "es" /etc/passwd ];then
useradd es
fi
chown es:es -R /usr/local/es
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
<<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
ES_HOME=/usr/local/es
case $1 in
start)
su es<<!
cd $ES_HOME
./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