Shell 脚本 一键部署tomcat8 + mysql80

一、部署前准备:

                1、web项目:HuluwaWeb.war

                2、数据库包: huluwa.sql 

                3、将两个包放入到根目录下

二、编写.sh脚本(这里需要修改对应部署项目的名称)

#!/bin/bash
function down_tom(){
#下载tomcat
wget -O /root/tomcat8.tar.gz https://archive.apache.org/dist/tomcat/tomcat-8/v8.0.47/bin/apache-tomcat-8.0.47.tar.gz

#进行md5校验查看是否是正版或是安装完成
tomcat_md5=$(md5sum /root/tonmat8.tar.gz &> /dev/null)
        if  [ $tomcat_md5="0fd249576c33fa71947bc7d296d5b0a9" ]
        then
          tar zxf /root/tomcat8.tar.gz -C /usr/local/ && break
        fi
mv /usr/local/apache-tomcat-8.0.47 /usr/local/tomcat8
}

#安装jdk

function down_jdk(){
        yum -y install java-1.8.0-openjdk &> /dev/null

        #将HuluwaWeb.war项目放到tomcat环境中
        mv /root/HuluwaWeb.war /usr/local/tomcat8/webapps/
}


#安装数据库
function down_mysql(){
        wget -O /root/mysql80-community-release-e17-7.noarch.rpm         https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
        rpm -ivh /root/mysql80-community-release-e17-7.noarch.rpm
        sed -i '5s/0/1/' /etc/yum.repos.d/mysql-community.repo
        sed -i '6s/1/0/' /etc/yum.repos.d/mysql-community.repo
        sed -i '13s/1/0/' /etc/yum.repos.d/mysql-community.repo
        yum -y install mysql-community-server &> /dev/null
        systemctl start mysqld

        #查询数据库默认密码,并且将密码修改为,JGHuluwa.123

        sleep 5
        sqlpass=$(grep "password" /var/log/mysqld.log | head -1 | awk '{print $(NF)}')
        newpass=JGHuluwa.123
        #修改密码
        mysql -uroot -p$sqlpass --connect-expired-password -e "alter user user() identified by 'JGHuluwa.123';"

        #这里是创建对应的数据库(huluwahoume)
        mysql -uroot -p$newpass -e "create database huluwahoume;"

        #导入对应的数据库文件(huluwa.sql)
        mysql -uroot -p$newpass woniusales < /root/huluwa.sql

}


function start_tom(){
        #启动tomcat
        /usr/local/tomcat8/bin/startup.sh
        sleep 10    
        #修改链接文件,这里是配置允许本地访问,所以没修改对应的IP

        #如果需要配置对应IP可以单独进入文件配置
        sed -i '3c db_password=JGHuluwa.123' /usr/local/tomcat8/webapps/HuluwaWeb/WEB-INF/classes/db.properties
}

#对每步的函数进行调用,可以查看返回结果,判断那一步骤出错。

down_tom
[ $? -eq 0 ] && echo "tom is ok"
down_jdk
[ $? -eq 0 ] && echo "jdk is ok"
down_mysql
[ $? -eq 0 ] && echo "mysql is ok"
start_tom
[ $? -eq 0 ] && echo "woniusales is ok"


 

你可能感兴趣的:(tomcat,java,apache)