1 Star 0 Fork 2

jerryharbin/install

forked from hzero-book/install 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
autoinstall-middleware.sh 16.96 KB
一键复制 编辑 原始数据 按行查看 历史
HAND 提交于 2021-04-30 00:11 . Initial Commit
#bin/bash
######### 调用示例 #################################
# sh autoinstall-middleware.sh
#
# 一键自动安装HZERO运行所需中间件服务,该脚本为CentOS服务器运行脚本
#
# @author nature.lei@hand-china.com 2021-04-20
####################################################
SUFFIX="openzero" #容器的后缀标签,如:mysql-后缀
if [ -n "$SUFFIX" ];then
SUFFIX="-$SUFFIX"
fi
if [ -z $1 ]; then
confirm="================================================================================
执行本安装脚本,可选择完成如下操作:
1.完成安装 Java, Git等工具
2.关闭服务器防火墙或已打开相应端口的网络访问权限:systemctl stop firewalld
3.完成安装 Docker 软件,可使用命令:curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun 一键安装
4.完成安装 Mysql、Redis、Minio 等中间件产品:参考https://open.hand-china.com/document-center/doc/product/10067/10032?doc_id=6296
4.1 创建hzero数据库用户:GRANT ALL PRIVILEGES ON *.* TO 'hzero'@'%' IDENTIFIED BY 'hzero' WITH GRANT OPTION;
4.2 配置数据库为大小写不敏感:lower_case_table_names=1
5.完成数据库初始化
注意:该脚本为CentOS服务器脚本,如需在其他服务器运行,请自行调整脚本。
请确认是否继续?[Y/n] "
read -p "$confirm" yn
if [ -z $yn ] || [ $yn == "Y" ] || [ $yn == "y" ];then
echo ">>>启动中间件安装及数据库初始化..."
else
exit 1
fi
fi
if ! [ -x "$(command -v git)" ]; then
echo ">>> 开始安装 Git 工具..."
yum install -y git
fi
if ! [ -x "$(command -v vim)" ]; then
echo ">>> 开始安装 vim 工具..."
yum install -y vim
fi
if ! [ -x "$(command -v tar)" ]; then
echo ">>> 开始安装 tar 工具..."
yum install -y tar
fi
if ! [ -x "$(command -v ifconfig)" ]; then
echo ">>> 开始安装 tar 工具..."
yum install -y net-tools
fi
# 检测网卡及IP地址
IPADDRESS="" #设置全局变量,用户函数返回值
function check_network(){
local ip=""
if [ -z "$IPADDRESS" ]; then #第一次调用时,需扫描服务器网卡
echo ">>>开始扫描服务器网卡及对应IP地址..."
local networknames=`ifconfig |grep -e "\beth[0-9]*:" -e "\bens[0-9]*:" -e "\benp0s[0-9]*:"|awk '{print $1}'`
local arr=($networknames)
for net in ${arr[@]}
do
ip=`ifconfig ${net%":"} |grep -v inet6|grep inet| awk '{print $2}'`
echo ">>> $net $ip" && sleep 1
done
if [ ${#arr[*]} == 1 ] && [ -n $ip ];then #仅检测到一个网卡,直接获取其IP地址
read -p ">>> 是否使用 $ip 作为$1服务的注册IP地址? [Y/n] " yn
if [ -z $yn ] || [ $yn == "Y" ] || [ $yn == "y" ];then
IPADDRESS=$ip
fi
fi
if [ -z "$IPADDRESS" ]; then
read -p ">>> 请指定$1服务的注册IP地址:[$ip] " IPADDRESS
if [ -z "$IPADDRESS" ] && [ -n "$ip" ];then
IPADDRESS=$ip
fi
fi
else
read -p ">>> 请指定$1服务的注册IP地址:[$2] " IPADDRESS
if [ -z "$IPADDRESS" ] && [ -n "$2" ];then
IPADDRESS=$2
fi
fi
while :;
do
if [[ $IPADDRESS =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
ip=(${IPADDRESS//\./ }) # 按.分割,转成数组,方便下面的判断
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
if [ $? -eq 1 ]; then
echo ">>> 值不正确,$IPADDRESS不是一个合法的IP地址!"
read -p ">>> 请指定$1服务的注册IP地址: " IPADDRESS
continue
fi
else
echo ">>> 值不正确,$IPADDRESS不是一个合法的IP地址!"
read -p ">>> 请指定$1服务的注册IP地址: " IPADDRESS
continue
fi
break
done
}
# 设置IP
check_network
# 检测端口是否被占用
function check_ports(){
local server_name=$1
local ports=($2)
local port=""
local rs=0
for port in ${ports[@]}
do
local grep_port=(`netstat -tlpn | grep ":\b$port\b" |awk '{print $7}'`)
if [ -n "$grep_port" ];then
local grep_server=(`docker ps --format '{{.Names}} {{.Ports}}' | grep ":\b$port\b" |awk '{print $1}'`)
local grep_server2=(`docker ps --format '{{.Names}} {{.Ports}}' | grep ":\b$(($port-1))\b" |awk '{print $1}'`)
#检测是否被自己的服务占用
if ( [ -z $grep_server ] && [ -z $grep_server2 ] ) || ( [ "$server_name" != "$grep_server" ] && [ "$server_name" != "$grep_server2" ] );then
echo ">>> 端口 $port 已被进程占用(PID/Program name): $grep_port"
rs=1
fi
fi
done
return $rs
}
# 设置服务安装端口
RETURN_PORT="" #设置全局变量,用户函数返回值
function reset_server_port(){
local server_name=$1
RETURN_PORT=$2
local port=""
while :;
do
read -p ">>> 请指定($1)服务运行端口号:[$2] " port
if [ -z "$port" ]; then
port=$2
fi
if [ -z "$port" ] || [[ ! $port =~ ^-?[0-9]+$ ]] || [ $port -le 0 ] || [ $port -gt 65535 ];then
echo ">>> 值不正确,端口号必须为不大于65535的正整数!"
continue
else
if [ -z $3 ] || [ $3 -ge 2 ];then
check_ports "$server_name" "$port $(($port+1))"
else
check_ports "$server_name" "$port"
fi
if [ $? == 1 ];then
continue
else
RETURN_PORT=$port
break
fi
fi
done
}
#检测防火墙
firewalld_status=(`service firewalld status 2>&1 | grep "\bActive\b:" | awk '{print $2}'`)
if [ "$firewalld_status" == "active" ];then
read -p ">>>当前防火墙(firewalld)为开启状态,服务端口可能无法直接访问,是否现在关闭? [Y/n] " yn
if [ -z $yn ] || [ $yn == "Y" ] || [ $yn == "y" ];then
service firewalld stop && systemctl disable firewalld
else
echo ">>> 请自行手动关闭防火墙或开放相应的端口访问..." && sleep 1
fi
fi
if [ -x "$(command -v iptables)" ]; then
iptables_status=(`service iptables status 2>&1 | grep "\bActive\b:" | awk '{print $2}'`)
if [ "$iptables_status" == "active" ];then
read -p ">>>当前防火墙(iptables)为开启状态,服务端口可能无法直接访问,是否现在关闭? [y/N] " yn
if [ "$yn" == "Y" ] || [ "$yn" == "y" ];then
service iptables stop && systemctl disable iptables
else
echo ">>> 请自行手动关闭防火墙或开放相应的端口访问..." && sleep 1
fi
fi
fi
#检测Docker安装
if ! [ -x "$(command -v docker)" ]; then
read -p ">>>未检测到Docker软件,是否现在安装Docker? [Y/n] " yn
if [ -z $yn ] || [ $yn == "Y" ] || [ $yn == "y" ];then
#curl -sSL https://get.daocloud.io/docker > install-docker.sh && sh install-docker.sh
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
systemctl enable docker
service docker restart
else
echo ">>> 请自行安装Docker软件后,再运行本脚本"
exit 1
fi
else
docker_version=(`docker version 2>&1 | grep "\bVersion\b" | awk '{print $2}'`)
echo ">>> 检测到已安装的Docker版本:$docker_version" && sleep 1
docker_status=(`service docker status 2>&1 | grep "\bActive\b:" | awk '{print $2}'`)
if [ "$docker_status" == "inactive" ];then
service docker start
systemctl enable docker
fi
fi
#检测Docker安装是否成功
if ! [ -x "$(command -v docker)" ]; then
echo ">>> Docker安装失败,请自行安装Docker软件后,再运行本脚本!"
exit 1
fi
#检测Java安装
source /etc/profile
if ! [ -x "$(command -v java)" ]; then
read -p ">>>未检测到已安装的Java,这将导致无法运行数据库初始化程序,是否现在安装Java? [Y/n] " yn
if [ -z $yn ] || [ $yn == "Y" ] || [ $yn == "y" ];then
if [ ! -f jdk-8u271-linux-x64.tar.gz ];then
echo ">>> 开始下载安装包,请等待..."
curl http://htdd.hand-china.com/jdk-8u271-linux-x64.tar.gz -o jdk-8u271-linux-x64.tar.gz
fi
if [ -d /usr/local/jdk1.8.0_271 ];then
read -p ">>> 检测到已安装的Java目录,是否删除后重新安装? [Y/n] " yn
if [ -z $yn ] || [ $yn == "Y" ] || [ $yn == "y" ];then
rm -rf /usr/local/jdk1.8.0_271
mkdir -p /usr/local
tar -zvxf jdk-8u271-linux-x64.tar.gz -C /usr/local/
fi
else
mkdir -p /usr/local
tar -zvxf jdk-8u271-linux-x64.tar.gz -C /usr/local/
fi
#检测java环境变量设置
profiles=(`cat /etc/profile | grep -e "\bJAVA_HOME\b" -e "\bJRE_HOME\b" -e "\bCLASSPATH\b"`)
if [ -z $profiles ];then
#添加环境变量
echo "export JAVA_HOME=/usr/local/jdk1.8.0_271" >> /etc/profile
echo "export JRE_HOME=/usr/local/jdk1.8.0_271/jre" >> /etc/profile
echo "export CLASSPATH=.:\$JAVA_HOME/lib/tools.jar:\$JRE_HOME/lib" >> /etc/profile
echo "export PATH=\$PATH:\$JAVA_HOME/bin" >> /etc/profile
fi
echo ">>> 已完成Java安装,且系统环境变量设置如下:" && sleep 1
cat /etc/profile | grep -e "\bJAVA_HOME\b" -e "\bJRE_HOME\b" -e "\bCLASSPATH\b"
read -p ">>> 是否修改profile环境变量设置(vi /etc/profile)? [y/N] " yn
if [ "$yn" == "Y" ] || [ "$yn" == "y" ];then
vim /etc/profile
fi
source /etc/profile
fi
fi
java_version=(`java -version 2>&1 | sed '1!d' | sed -e 's/"//g' | awk '{print $3}'`)
echo ">>> 检测到已安装的Java版本:$java_version" && sleep 1
# Step 1:
read -p ">>>是否安装Mysql数据库服务? [Y/n] " yn
if [ -z $yn ] || [ $yn == "Y" ] || [ $yn == "y" ];then
DOCKER_CONTAINER_NAME="mysql$SUFFIX"
reset_server_port $DOCKER_CONTAINER_NAME 3306 1
DB_INSTALL_PORT=$RETURN_PORT
read -p ">>> 请输入数据库root密码:[openzero] " ROOT_PASSWARD
if [ -z $ROOT_PASSWARD ];then
ROOT_PASSWARD="openzero" #默认的root密码
fi
if [ -d /u01/docker_container_config/$DOCKER_CONTAINER_NAME ];then
read -p ">>> 检测到已存在同名容器的数据文件目录,是否删除重新安装? [Y/n] " yn
if [ -z $yn ] || [ $yn == "Y" ] || [ $yn == "y" ];then
docker stop $DOCKER_CONTAINER_NAME #删除旧容器
rm -rf /u01/docker_container_config/$DOCKER_CONTAINER_NAME
fi
fi
mkdir -p /u01/docker_container_config/$DOCKER_CONTAINER_NAME/data \
/u01/docker_container_config/$DOCKER_CONTAINER_NAME/conf \
/u01/docker_container_config/$DOCKER_CONTAINER_NAME/entrypoint
## 创建mysql配置文件
echo "[mysqld]
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
secure-file-priv=/var/lib/mysql-files
user=mysql
symbolic-links=0
lower_case_table_names=1
max_connections=2000
max_allowed_packet=50M
innodb_file_per_table=1
character_set_server=utf8mb4
collation_server=utf8mb4_bin" > /u01/docker_container_config/$DOCKER_CONTAINER_NAME/conf/my.cnf
## 创建 hzero 用户初始化脚本
echo " update mysql.user set user.Host='%' where user.User='root';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'hzero'@'%' IDENTIFIED BY 'hzero' WITH GRANT OPTION;
FLUSH PRIVILEGES;" > /u01/docker_container_config/$DOCKER_CONTAINER_NAME/entrypoint/init-user.sql
chmod a+x /u01/docker_container_config/$DOCKER_CONTAINER_NAME/entrypoint/init-user.sql
## 创建Docker容器
docker rm -f $DOCKER_CONTAINER_NAME
echo ">>> 开始创建Mysql数据库服务($DOCKER_CONTAINER_NAME)..." && sleep 1
set -x
# docker run -d --net=host --name mysql$SUFFIX \
docker run -d -p $DB_INSTALL_PORT:3306 --name $DOCKER_CONTAINER_NAME \
-v /u01/docker_container_config/$DOCKER_CONTAINER_NAME/conf/my.cnf:/etc/my.cnf \
-v /u01/docker_container_config/$DOCKER_CONTAINER_NAME/data:/var/lib/mysql \
-v /u01/docker_container_config/$DOCKER_CONTAINER_NAME/entrypoint:/docker-entrypoint-initdb.d \
-e "MYSQL_ROOT_PASSWORD=$ROOT_PASSWARD" \
--privileged=true --restart=always \
registry.cn-hangzhou.aliyuncs.com/ntclub/mysql-server:5.7
set +x
sleep 10s
## 清除用户初始化脚本
#echo "" > /u01/docker_container_config/$DOCKER_CONTAINER_NAME/entrypoint/init-user.sql
#else
# DB_INSTALL_PORT=3306 #默认端口
# ROOT_PASSWARD="openzero" #默认root密码
fi
# Step 2:
read -p ">>>是否安装Redis缓存服务? [Y/n] " yn
if [ -z $yn ] || [ $yn == "Y" ] || [ $yn == "y" ];then
DOCKER_CONTAINER_NAME="redis$SUFFIX"
reset_server_port $DOCKER_CONTAINER_NAME 6379 1
REDIS_INSTALL_PORT=$RETURN_PORT
if [ -d /u01/docker_container_config/$DOCKER_CONTAINER_NAME ];then
read -p ">>> 检测到已存在同名容器的数据文件目录,是否删除重新安装? [Y/n] " yn
if [ -z $yn ] || [ $yn == "Y" ] || [ $yn == "y" ];then
docker stop $DOCKER_CONTAINER_NAME #删除旧容器
rm -rf /u01/docker_container_config/$DOCKER_CONTAINER_NAME
fi
fi
mkdir -p /u01/docker_container_config/$DOCKER_CONTAINER_NAME/data
## 创建Docker容器
docker rm -f $DOCKER_CONTAINER_NAME
echo ">>> 开始创建Redis缓存服务($DOCKER_CONTAINER_NAME)..."
set -x
docker run -d -p $REDIS_INSTALL_PORT:6379 --name $DOCKER_CONTAINER_NAME \
-v /u01/docker_container_config/$DOCKER_CONTAINER_NAME/data:/data \
--privileged=true --restart=always \
registry.cn-hangzhou.aliyuncs.com/ntclub/redis:4.0 redis-server --appendonly yes
set +x
sleep 5s
fi
# Step 3:
read -p ">>>是否安装Minio文件存储服务? [Y/n] " yn
if [ -z $yn ] || [ $yn == "Y" ] || [ $yn == "y" ];then
DOCKER_CONTAINER_NAME="minio$SUFFIX"
reset_server_port $DOCKER_CONTAINER_NAME 9000 1
MINIO_INSTALL_PORT=$RETURN_PORT
read -p ">>> 请输入minio访问密码:[minio123] " MINIO_PASSWARD
if [ -z $MINIO_PASSWARD ];then
MINIO_PASSWARD="minio123" #默认的root密码
fi
if [ -d /u01/docker_container_config/$DOCKER_CONTAINER_NAME ];then
read -p ">>> 检测到已存在同名容器的数据文件目录,是否删除重新安装? [Y/n] " yn
if [ -z $yn ] || [ $yn == "Y" ] || [ $yn == "y" ];then
docker stop $DOCKER_CONTAINER_NAME #删除旧容器
rm -rf /u01/docker_container_config/$DOCKER_CONTAINER_NAME
fi
fi
mkdir -p /u01/docker_container_config/$DOCKER_CONTAINER_NAME/data \
/u01/docker_container_config/$DOCKER_CONTAINER_NAME/config
## 创建Docker容器
docker rm -f $DOCKER_CONTAINER_NAME
echo ">>> 开始创建Minio文件存储服务($DOCKER_CONTAINER_NAME)..."
set -x
docker run -d -p $MINIO_INSTALL_PORT:9000 --name $DOCKER_CONTAINER_NAME \
-e "MINIO_ACCESS_KEY=minio" \
-e "MINIO_SECRET_KEY=$MINIO_PASSWARD" \
-v /u01/docker_container_config/$DOCKER_CONTAINER_NAME/data:/data \
-v /u01/docker_container_config/$DOCKER_CONTAINER_NAME/config:/root/.minio \
--privileged=true --restart=always \
hub.baidubce.com/yanqian-tools/minio:RELEASE.2019-10-12T01-39-57Z server /data
set +x
sleep 5s
fi
# Step 4:
read -p ">>>是否开始进行数据初始化安装? [Y/n] " yn
if [ -z $yn ] || [ $yn == "Y" ] || [ $yn == "y" ];then
if [ -z $DB_INSTALL_PORT ];then
read -p ">>> 请输入数据库运行端口号:[3306] " DB_INSTALL_PORT
if [ -z $DB_INSTALL_PORT ];then
DB_INSTALL_PORT=3306 #默认端口
fi
fi
if [ -z $ROOT_PASSWARD ];then
read -p ">>> 请输入数据库root密码:[openzero] " ROOT_PASSWARD
if [ -z $ROOT_PASSWARD ];then
ROOT_PASSWARD="openzero" #默认的root密码
fi
fi
if [ ! -d hzero-resource ];then
git clone https://gitee.com/hzero-book/hzero-resource.git
fi
if [ ! -f hzero-resource/application.yml.bak ];then
cp hzero-resource/application.yml hzero-resource/application.yml.bak
fi
/bin/cp -rf hzero-resource/application.yml.bak hzero-resource/application.yml
find hzero-resource -name 'application.yml' | xargs sed -i "s mysql://localhost:3306 mysql://$IPADDRESS:$DB_INSTALL_PORT g"
find hzero-resource -name 'application.yml' | xargs sed -i "s SPRING_DATASOURCE_USERNAME:hand SPRING_DATASOURCE_USERNAME:root g"
find hzero-resource -name 'application.yml' | xargs sed -i "s SPRING_DATASOURCE_PASSWORD:handhand SPRING_DATASOURCE_PASSWORD:$ROOT_PASSWARD g"
cd hzero-resource
echo ">>> 当前应用配置如下:" && cat application.yml | grep -v "#" && sleep 1s
read -p ">>> 是否修改配置(vi application.yml)? [y/N] " yn
if [ "$yn" == "Y" ] || [ "$yn" == "y" ];then
vim application.yml
echo ">>> 开始启动数据初始化程序..."
else
echo ">>> 使用默认配置启动数据初始化程序..."
sleep 2s
fi
## 启动数据初始化程序
echo ">>> 注意:1.启动成功后,请访问(http://$IPADDRESS:8099/install.html)完成操作"
echo ">>> (需开放8099端口访问或关闭服务器防火墙)"
echo ">>> 2.依次执行【更新数据库】--> 【导入数据】操作"
# echo ">>> 3.完成操作后,使用【Ctrl+C】快捷键退出程序"
sleep 1
read -p ">>> 按任意键开始启动初始化程序..." yn
sh database-init.sh $IPADDRESS
tail -n 7 data-init.log
echo ">>>数据库初始化工作已结束"
echo -e "================================================================================\n"
fi
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jerryharbin/install.git
git@gitee.com:jerryharbin/install.git
jerryharbin
install
install
master

搜索帮助