自動(dòng)化shell腳本
ps:此處安裝的jdk,maven,tomcat均在/opt目錄下,如需其他目錄或者其他版本,可根據(jù)需要改動(dòng)shell腳本即可。
ps:所有代碼全部手敲,已親測(cè)能夠使用,全部分享出來(lái)。
一:虛擬機(jī)初始化
創(chuàng)建shell腳本,把命令放進(jìn).sh腳本里,./xx.sh運(yùn)行此腳本可以完成虛擬機(jī)初始化
#!/bin/bash
#1.改變機(jī)器名
hostnamectl set-hostname $1
#2.打通ip地址
sed -i 's/dhcp/static/' /etc/sysconfig/network-scripts/ifcfgenp0s3
sed -i 's/ONBOOT=no/ONBOOT=yes/' /etc/sysconfig/network-scripts/ifcfgenp0s3
echo "IPADDR=$2" >> /etc/sysconfig/network-scripts/ifcfgenp0s3
systemctl restart network
#3.關(guān)閉防火墻
systemctl stop firewalld
systemctl disable firewalld
二:jdk自動(dòng)化安裝
創(chuàng)建shell腳本,把命令放進(jìn).sh腳本里,./xx.sh運(yùn)行此腳本可以完成jdk自動(dòng)化安裝
#!/bin/bash
jdkTar="/opt/jdk-8u111-linux-x64.tar.gz"
# 檢查原先是否已安裝
checkExist(){
jdkline1=$(grep -n "export JAVA_HOME=.*" /etc/profile | cut -f1 -d':')
if [ -n "$jdkline1" ];then
echo "存在JAVA_HOME路徑,將其刪除"
sed -i "${jdkline1}d" /etc/profile
fi
jdkline2=$(grep -n "export CLASSPATH=.*\$JAVA_HOME.*" /etc/profile | cut -f1 -d':')
if [ -n "$jdkline2" ];then
echo "存在CLASSPATH路徑,將其刪除"
sed -i "${jdkline2}d" /etc/profile
fi
jdkline3=$(grep -n "export PATH=.*\$JAVA_HOME.*" /etc/profile | cut -f1 -d':')
if [ -n "$jdkline3" ];then
echo "存在PATH-JAVA路徑,將其刪除"
sed -i "${jdkline3}d" /etc/profile
fi
}
# 查詢是否有jdk壓縮包
if [ -e $jdkTar ];then
echo "--------存在jdk安裝包-------"
echo "正在解壓jdk安裝包。。。"
tar -zxvf /opt/jdk-8u111-linux-x64.tar.gz
if [ -e "/opt/soft2" ];then
echo "存在該文件夾,刪除。。"
rm -rf /opt/soft2
fi
echo "---------------------------------"
echo "---------------------------------"
echo "正在建立jdk安裝路徑。。"
echo "---------------------------------"
echo "---------------------------------"
mkdir -p /opt/soft2/jdk180
mv -f jdk1.8.0_111 /opt/soft2/jdk180
# 檢查配置信息
checkExist
echo "---------------------------------"
echo "---------------------------------"
echo "正在配置jdk環(huán)境。。。"
sed -i '$a export JAVA_HOME=/opt/soft2/jdk180/jdk1.8.0_111' /etc/profile
sed -i '$a export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar' /etc/profile
sed -i '$a export PATH=$PATH:$JAVA_HOME/bin' /etc/profile
echo "---------------------------------"
echo "---------------------------------"
echo "配置環(huán)境已完成。。。"
echo 'this is the end'
echo "---------------------------------"
echo "---------------------------------"
echo "配置版本信息如下:"
source /etc/profile
java -version
else
echo "未檢測(cè)到安裝包,請(qǐng)將安裝包放到opt目錄下"
fi
三:maven自動(dòng)化安裝
創(chuàng)建shell腳本,把命令放進(jìn).sh腳本里,./xx.sh運(yùn)行此腳本可以完成maven自動(dòng)化安裝
#!/bin/bash
mavenTar="/opt/apache-maven-3.6.1-bin.tar.gz"
mavenMirror=" mirror>
id>alimaven/id>
mirrorOf>central/mirrorOf>
name>aliyun maven/name>
url>https://maven.aliyun.com/repository/central/url>
/mirror>
mirror>
id>alimaven/id>
name>aliyun maven/name>
url>http://maven.aliyun.com/nexus/content/groups/public//url>
mirrorOf>central/mirrorOf>
/mirror>
"
mavenrespo="localRepository>/opt/soft2/maven361/apache-maven-3.6.1/jarstore/localRepository>"
# 檢查原先是否已安裝
checkExist(){
mavenline1=$(grep -n "export MAVEN_HOME=.*" /etc/profile | cut -f1 -d':')
if [ -n "$mavenline1" ];then
echo "存在MAVEN_HOME路徑,將其刪除"
sed -i "${mavenline1}d" /etc/profile
fi
mavenline3=$(grep -n "export PATH=.*\$MAVEN_HOME.*" /etc/profile | cut -f1 -d':')
if [ -n "$mavenline3" ];then
echo "存在PATH-MAVEN路徑,將其刪除"
sed -i "${mavenline3}d" /etc/profile
fi
}
# 查詢是否有maven壓縮包
if [ -e $mavenTar ];then
echo "--------存在maven安裝包-------"
echo "正在解壓maven安裝包。。。"
tar -zxvf /opt/apache-maven-3.6.1-bin.tar.gz
if [ -e "/opt/soft2/maven361" ];then
echo "存在該文件夾,刪除。。"
rm -rf /opt/soft2/maven361
fi
echo "---------------------------------"
echo "---------------------------------"
echo "正在建立maven安裝路徑。。"
echo "---------------------------------"
echo "---------------------------------"
mkdir -p /opt/soft2/maven361
mv -f apache-maven-3.6.1 /opt/soft2/maven361
# 檢查配置信息
checkExist
echo "---------------------------------"
echo "---------------------------------"
echo "正在配置maven環(huán)境。。。"
sed -i '$a export MAVEN_HOME=/opt/soft2/maven361/apache-maven-3.6.1' /etc/profile
sed -i '$a export PATH=$PATH:$MAVEN_HOME/bin' /etc/profile
echo "---------------------------------"
echo "---------------------------------"
echo "----正在配置maven鏡像源和mirror----"
sed -i '/\/mirror>/i ${mavenMirror}' /opt/soft2/maven361/apache-maven-3.6.1/conf/settings.xml
sed -i '/\/settings>/i ${mavenrespo}' /opt/soft2/maven361/apache-maven-3.6.1/conf/settings.xml
echo "---------------------------------"
echo "---------------------------------"
echo "配置環(huán)境已完成。。。"
echo 'this is the end'
echo "---------------------------------"
echo "---------------------------------"
echo "配置版本信息如下:"
source /etc/profile
mvn -version
else
echo "未檢測(cè)到安裝包,請(qǐng)將安裝包放到opt目錄下"
fi
四:tomcat自動(dòng)化安裝
創(chuàng)建shell腳本,把命令放進(jìn).sh腳本里,./xx.sh運(yùn)行此腳本可以完成tomcat自動(dòng)化安裝
#!/bin/bash
tomcatTar="/opt/apache-tomcat-8.5.54.tar.gz"
# 查詢是否有tomcat壓縮包
if [ -e $tomcatTar ];then
echo "--------存在tomcat安裝包-------"
echo "正在解壓maven安裝包。。。"
tar -zxvf /opt/apache-tomcat-8.5.54.tar.gz
if [ -e "/opt/soft2/tomcat85" ];then
echo "存在該文件夾,刪除。。"
rm -rf /opt/soft2/tomcat85
fi
echo "---------------------------------"
echo "---------------------------------"
echo "正在建立tomcat安裝路徑。。"
echo "---------------------------------"
echo "---------------------------------"
mkdir -p /opt/soft2/tomcat85
mv -f apache-tomcat-8.5.54 /opt/soft2/tomcat85
echo "---------------------------------"
echo "---------------------------------"
echo "配置tomcat環(huán)境已完成。。。"
echo 'this is the end'
echo "---------------------------------"
echo "---------------------------------"
else
echo "未檢測(cè)到安裝包,請(qǐng)將安裝包放到opt目錄下"
fi
五:mysql自動(dòng)化安裝
創(chuàng)建shell腳本,把命令放進(jìn).sh腳本里,./xx.sh運(yùn)行此腳本可以完成mysql自動(dòng)化安裝
#!/bin/bash
mysqlInstall=$(rpm -qa | grep mysql)
mysqlRepoInstall1=$(find /etc/yum.repos.d/ -name "mysql-community.repo" | sed 's#.*/##')
mysqlRepoInstall2=$(find /etc/yum.repos.d/ -name "mysql-community-source.repo" | sed 's#.*/##')
wgetInstall=$(rpm -qa | grep wget)
mariadbInstall=$(rpm -qa | grep mariadb)
# 檢查是否已安裝了mysql,未安裝再安裝
if [ ! -n "${mysqlInstall}" ];then
# 檢查centOS是否自帶了mariadb,若有則刪除
if [ -n "${mariadbInstall}" ];then
echo "自帶mariadb---刪除mariadb中----"
rpm -e --nodeps mariadb-libs-5.5.64-1.el7.x86_64
echo "--------------------------------"
echo "--------------------------------"
echo "--------------------------------"
else
echo "centOS里沒(méi)有mariadb軟件"
echo "-----------------------"
echo "-----------------------"
fi
if [ -n "${wgetInstall}" ];then
echo "-------存在wget下載工具-------"
echo "------------------------------"
echo "------------------------------"
else
echo "-------安裝下載工具wget-------"
echo "------------------------------"
echo "------------------------------"
yum install -y wget
fi
echo "-------在線下載5.7mysql并安裝-------"
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
echo "-----------下載mysql成功-------------"
echo "-------------------------------------"
echo "-----------正在安裝mysql中----------"
echo "-------------------------------------"
rpm -ivh mysql-community-release-el7-5.noarch.rpm
echo "-----正在檢測(cè)mysql安裝環(huán)境是否成功----"
else
echo "-------------已安裝過(guò)mysql--------------"
fi
installmysqlServer(){
if [ "${mysqlRepoInstall1}" = "mysql-community.repo" -a "${mysqlRepoInstall2}" = "mysql-community-source.repo" ];then
echo "存在2個(gè)mysql安裝環(huán)境,下載安裝完成。。。"
echo "----------------------------------------"
echo "----------------------------------------"
echo "-----------安裝mysql-server中-----------"
echo "----------------------------------------"
echo "----------------------------------------"
if [ ! -e "/var/lib/mysql" ];then
yum install -y mysql-server
else
echo "-----已安裝過(guò)mysql-server-----"
fi
echo "修改訪問(wèn)權(quán)限為當(dāng)前用戶。。。"
chown -R root:root /var/lib/mysql
ls -l /var/lib
echo "重啟mysql服務(wù)中"
service mysqld restart
echo "安裝完成---請(qǐng)登錄mysql修改密碼和字符集"
else
echo "${mysqlRepoInstall1}"
echo "${mysqlRepoInstall2}"
echo "----沒(méi)有安裝成功----"
fi
}
installmysqlServer
總結(jié)
到此這篇關(guān)于shell腳本自動(dòng)化創(chuàng)建虛擬機(jī)的基本配置--tomcat--mysql--jdk--maven的文章就介紹到這了,更多相關(guān)shell腳本自動(dòng)化創(chuàng)建虛擬機(jī)的基本配置--tomcat--mysql--jdk--maven內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 監(jiān)控MySQL主從狀態(tài)的shell腳本
- shell腳本一鍵安裝MySQL5.7.29的方法
- mysql常用備份命令和shell備份腳本分享
- shell腳本定時(shí)備份MySQL數(shù)據(jù)庫(kù)數(shù)據(jù)并保留指定時(shí)間
- shell腳本實(shí)現(xiàn)mysql定時(shí)備份、刪除、恢復(fù)功能
- 一個(gè)Shell小腳本精準(zhǔn)統(tǒng)計(jì)Mysql每張表的行數(shù)實(shí)現(xiàn)
- 通過(guò)Shell腳本批量創(chuàng)建服務(wù)器上的MySQL數(shù)據(jù)庫(kù)賬號(hào)
- 使用shell腳本來(lái)給mysql加索引的方法
- 干掉一堆mysql數(shù)據(jù)庫(kù),僅需這樣一個(gè)shell腳本(推薦)
- 使用shell腳本每天對(duì)MySQL多個(gè)數(shù)據(jù)庫(kù)自動(dòng)備份的講解
- MySQL Shell的介紹以及安裝