淘先锋技术网

首页 1 2 3 4 5 6 7
#!/bin/bash
echo "初始化安装脚本"
<<COMMENT
  注:
   author:whj
   CREATED: 2020.04.24
   oracle版本:11.2.0.4.0
   CentOS Linux release 7.6.1810 (Core)  (已在centos7.6上完成测试,centos6尚未测试,但脚本里有做系统识别)
   使用方法:将完整版镜像包、oracle安装包以及此脚本上传到/opt/oracle目录下,执行 sh oracleInstall.sh 即可自动完成oracle安装
   注意:
   1、此脚本适用于无法联网的环境离线安装oracle,如服务器可联网,删掉脚本里的   #####创建yum源这一大步骤就可以####    ,这里就不做判别了,也无需上传完整版镜像包
   2、运行脚本后主机名将会修改,且在下一次登入终端时生效
   3、脚本是在win环境下编写,如果提示换行问题($'\r': command not found),则在linux终端执行   sed -i 's/\r//' oracleInstall.sh   即可
COMMENT
######################定义变量######################
setHostName=dbServer             #定义linux主机名
SIDNAME=itsmdb                   #定义实例名
passwordAll=Af_2020dd            #设置数据库密码
dbPort=1555                      #设置数据库连接端口号
file1_1=p13390677_112040_Linux-x86-64_1of7.zip
file1_2=p13390677_112040_Linux-x86-64_2of7.zip
##以下变量默认,无需修改
osVersion=$(cat /etc/redhat-release | tr -cd "[0-9]")
osNum7=${osVersion:0-4:4}
osNum6=${osVersion:0:2}
osNum=${osVersion:0:1}
isoNum=0
dir=$(pwd)
/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:" > ip.txt
theip=$(head -1 ip.txt)
#####################定义函数#######################
function exVar
{
     TMP=/tmp
  TMPDIR=$TMP   
  ORACLE_BASE=/home/oracle/app/oracle
  ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1
  ORACLE_SID=$SIDNAME
  ORACLE_UNQNAME=$SIDNAME
  ORACLE_TERM=xterm
  PATH=$ORACLE_HOME/bin:$PATH
  LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
}
function filetobak
{
  cp -f /etc/sysctl.conf.bak`date +%Y%m%d`  /etc/sysctl.conf
  cp -f /etc/security/limits.conf.bak`date +%Y%m%d`  /etc/security/limits.conf
  cp -f /etc/selinux/config.bak.`date +%Y%m%d`      /etc/selinux/config
  cp -f /etc/pam.d/login.bak.`date +%Y%m%d`  /etc/pam.d/login
  userdel oracle
  groupdel dba   
  groupdel oinstall
  groupdel asmdba
  groupdel asmadmin
  rm -rf /home/oracle/
  rm -f /etc/sysctl.conf.bak`date +%Y%m%d`  /etc/security/limits.conf.bak`date +%Y%m%d` /etc/selinux/config.bak.`date +%Y%m%d`  /etc/pam.d/login.bak.`date +%Y%m%d`
}
function stopInstall
{
  ID=`ps -ef | grep db_install.rsp | grep -v "grep" | awk '{print $2}'`   
  echo "---------------"  
  for id in $ID  
  do  
  kill -9 $id  
  done
  rm -rf /home/oracle/app/oracle/
  rm -f /etc/oraInst.loc
  rm -f /usr/local/bin/dbhome
  rm -f /usr/local/bin/oraenv
  rm -f /usr/local/bin/coraenv
  rm -f /etc/oratab
}
#####################确认是root用户安装###########
if [ $(id -u) != "0" ];then
   echo "Error: You must be root to run this script!"
   exit 1
fi
  
echo "========================================================================="
echo " install Oracle 11.2.0.4.0 on CentOS  $osNum"
echo "========================================================================="
#####################修改主机名###########
if [ "$osNum" == "6" ]
then 
cat>> /etc/sysconfig/network<<EOF
NETWORKING=yes
HOSTNAME=$setHostName
EOF
cat>> /etc/hosts<<EOF
$setHostName  $theip
EOF
 hostname $setHostName
# exec bash
fi
  
if [ "$osNum" == "7" ]
then 
hostnamectl set-hostname $setHostName
cat>> /etc/hosts<<EOF
 $theip    $setHostName
EOF
 hostname 
 #exec bash
fi
######################创建yum源######################
cd /mnt
if [ -d cdrom ];then
    echo "cdrom目录已创建"
    else
    mkdir -p /mnt/cdrom
fi
cd -
ls CentOS-*.iso >ls.txt 2>&1
if (( $?==0 ))
then
  isoVersion=$(cat ls.txt | tr -cd "[0-9]")
else
  echo "Error:当前目录下未找到镜像,请检查镜像格式"
  exit 1
fi
if (( $osNum==7 ))
then
  isoNum=${isoVersion:0-4:4}
else
  isoNum=${isoVersion:0:2}
fi
if [[ "$isoNum" == "$osNum7" || "$isoNum" == "$osNum6" ]]
  then
    echo "已检测到镜像"
else
    echo "Error:镜像与系统不匹配,请检上传的镜像版本"
    exit 1
fi
mount $(cat ls.txt) -o loop /mnt/cdrom
if [[ $?==32 || $?==0 ]]
then
  echo "镜像已挂载"
  continue
else
  echo "Error:挂载出错,请检查镜像完整性"
  exit 1
fi
rm -f ls.txt ip.txt
cd  /etc/yum.repos.d
# 判断备份目录是否创建
if [ -d repobak`date +%Y%m%d` ];then
    echo "yum备份目录已创建"
    else
    mkdir -p repobak`date +%Y%m%d`
fi
mv *.repo repobak`date +%Y%m%d`/ >/dev/null  2>&1
cat >>local.repo<<EOF
[local_server]
name=This is a local repo
baseurl=file:///mnt/cdrom
enabled=1
gpgcheck=1
gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-CentOS-$osNum
EOF
echo "正在创建yum源"
yum  clean all;yum makecache
######################安装环境依赖###########################
yum install -y binutils compat-libstdc++-33 elfutils-libelf elfutils-libelf-devel expat gcc gcc-c++ glibc glibc-common glibc-devel glibc-headers libaio libaio-devel libgcc libstdc++ libstdc++-devel make unixODBC unixODBC-devel zip unzip lm_sensors-libs sysstat libtool-ltdl
rpm -q binutils compat-libstdc++-33 elfutils-libelf elfutils-libelf-devel expat gcc gcc-c++ glibc glibc-common glibc-devel glibc-headers libaio libaio-devel libgcc libstdc++ libstdc++-devel make unixODBC unixODBC-devel zip unzip lm_sensors-libs sysstat libtool-ltdl
if (( $?==0 ))
then
  echo "相关依赖包已安装完毕"
else
  echo "Error:当前镜像缺少相关依赖,请检查镜像是否为全量版(Everything iso)"
  exit 1
fi
######################修改linux参数######################
cd /etc
cp sysctl.conf sysctl.conf.bak`date +%Y%m%d`
cat>>sysctl.conf<<EOF
kernel.shmall = 2097152
kernel.shmmax = 536870912
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128
net.ipv4.ip_local_port_range = 9000 65500
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576
EOF
cd /sbin/;sysctl -p
cd /etc/security/
cp limits.conf limits.conf.bak`date +%Y%m%d`
cat>>limits.conf<<EOF
oracle soft nproc 10240
oracle hard nproc 10240
oracle soft nofile 65536
oracle hard nofile 65536
EOF
cd /etc/pam.d/
cp login login.bak.`date +%Y%m%d`
cat>>login<<EOF
session    required     pam_limits.so
EOF
###关闭防火墙##
cd /etc/selinux
cp config config.bak.`date +%Y%m%d`
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
if (( $osNum==6 ))
then
  service iptables stop
  chkconfig iptables off
else
  systemctl stop firewalld.service && systemctl disable firewalld.service
fi
######################创建用户和用户组######################
for GROUP_NAME in dba oinstall asmdba asmadmin
do
if [ -z $(cat /etc/group|awk -F: '{print $1}'| grep -w "$GROUP_NAME") ]
then
  groupadd  $GROUP_NAME
    if(($? == 0 ))
         then
         echo "group $GROUP_NAME add sucessfully!"
    fi
else
  echo "$GROUP_NAME is exsits"
fi
done
useradd -g oinstall -G dba,asmdba,asmadmin oracle
if(($? == 0 ))
then
  echo " oracle 用户创建成功!"
fi
######################设置oracle用户环境变量######################
su - oracle<<"EOF"
if [ -d app ];then
    echo "app目录已创建"
    else
    mkdir -p app
fi
chmod 777 -R app
EOF
cat>>/home/oracle/.bash_profile<<profile
export TMP=/tmp
export TMPDIR=\$TMP   
export ORACLE_BASE=\$HOME/app/oracle
export ORACLE_HOME=\$ORACLE_BASE/product/11.2.0/db_1
export ORACLE_SID=$SIDNAME
export ORACLE_UNQNAME=$SIDNAME
export ORACLE_TERM=xterm
export PATH=\$ORACLE_HOME/bin:\$PATH
export LD_LIBRARY_PATH=\$ORACLE_HOME/lib:\$LD_LIBRARY_PATH
profile
 
source /home/oracle/.bash_profile
######################解压安装包######################
cd $dir
for oraFile in $file1_1 $file1_2
do
if [ -f $oraFile ]
then
  chown -R oracle:oinstall $oraFile
  sysctl -w kernel.watchdog_thresh=30
  echo "正在复制安装包......"
  cp $oraFile /home/oracle
else
  filetobak
  echo "ERROR:未在当前目录下找到安装包"
  exit 1
fi
done
su - oracle<<EOF
unzip $file1_1
if(( $? != 0 ))
then
  echo " 解压失败,请检查文件完整性"
  filetobak
  exit 1
fi
unzip $file1_2
if(( $? != 0 ))
then
  echo " 解压失败,请检查文件完整性"
  filetobak
  exit 1
else
  echo "解压安装文件成功"
fi
rm -f $file1_1 $file1_2
EOF
######################安装数据库######################
####修改安装响应文件####
echo "正在修改安装文件......"
sed -i '29s#oracle.install.option=#oracle.install.option=INSTALL_DB_SWONLY#'  /home/oracle/database/response/db_install.rsp
sed -i '42s#UNIX_GROUP_NAME=#UNIX_GROUP_NAME=oinstall#'  /home/oracle/database/response/db_install.rsp
sed -i '49s#INVENTORY_LOCATION=#INVENTORY_LOCATION=/home/oracle/app/oracle/oraInventory#'  /home/oracle/database/response/db_install.rsp
sed -i '86s#SELECTED_LANGUAGES=en#SELECTED_LANGUAGES=en,zh_CN#'  /home/oracle/database/response/db_install.rsp
sed -i '91s#ORACLE_HOME=#ORACLE_HOME=/home/oracle/app/oracle/product/11.2.0/db_1#'  /home/oracle/database/response/db_install.rsp
sed -i '96s#ORACLE_BASE=#ORACLE_BASE=/home/oracle/app/oracle#'  /home/oracle/database/response/db_install.rsp
sed -i '107s#oracle.install.db.InstallEdition=#oracle.install.db.InstallEdition=EE#'  /home/oracle/database/response/db_install.rsp
sed -i '117s#oracle.install.db.EEOptionsSelection=false#oracle.install.db.EEOptionsSelection=true#'  /home/oracle/database/response/db_install.rsp
sed -i '154s#oracle.install.db.DBA_GROUP=#oracle.install.db.DBA_GROUP=dba#'  /home/oracle/database/response/db_install.rsp
sed -i '160s#oracle.install.db.OPER_GROUP=#oracle.install.db.OPER_GROUP=oinstall#'  /home/oracle/database/response/db_install.rsp
sed -i '176s#oracle.install.db.isRACOneInstall=#oracle.install.db.isRACOneInstall=false#'  /home/oracle/database/response/db_install.rsp
sed -i '189s#oracle.install.db.config.starterdb.type=#oracle.install.db.config.starterdb.type=GENERAL_PURPOSE#'  /home/oracle/database/response/db_install.rsp
sed -i '194s#oracle.install.db.config.starterdb.globalDBName=#oracle.install.db.config.starterdb.globalDBName='"$SIDNAME"'#'  /home/oracle/database/response/db_install.rsp
sed -i '199s#oracle.install.db.config.starterdb.SID=#oracle.install.db.config.starterdb.SID='"$SIDNAME"'#'  /home/oracle/database/response/db_install.rsp
sed -i '229s#oracle.install.db.config.starterdb.memoryLimit=#oracle.install.db.config.starterdb.memoryLimit=1024#'  /home/oracle/database/response/db_install.rsp
sed -i '262s#oracle.install.db.config.starterdb.password.ALL=#oracle.install.db.config.starterdb.password.ALL='"$passwordAll"'#'  /home/oracle/database/response/db_install.rsp
sed -i '400s#DECLINE_SECURITY_UPDATES=#DECLINE_SECURITY_UPDATES=true#'  /home/oracle/database/response/db_install.rsp
echo "修改安装文文件完毕......"
####开始安装oracle####
exVar
su - oracle<<EOF
cd  /home/oracle/database
if [ -f nohup.out ]
then
    rm nohup.out
fi
nohup ./runInstaller -silent -noconfig -ignorePrereq -responseFile /home/oracle/database/response/db_install.rsp  &
EOF
echo "正在安装中,请不要退出"
cd /home/oracle/database
while true 
do
grep "FATAL" /home/oracle/database/nohup.out
grp0=$?
grep "Failed" /home/oracle/database/nohup.out
grp3=$?
if [[ "$grp0" == "0" || "$grp3" == "0" ]]
then
  cp  /home/oracle/database/nohup.out  $dir/oraInstall.log
  echo "安装失败,请查看$dir下的oraInstall.log日志"
  stopInstall
  filetobak
  exit 1 
fi
grep "/home/oracle/app/oracle/oraInventory/orainstRoot.sh" nohup.out
grp1=$?
grep "/home/oracle/app/oracle/product/11.2.0/db_1/root.sh" nohup.out
grp2=$?
if [[ "$grp1" == "0" && "$grp2" == "0" ]]
then 
  echo "检查到相关配置脚本,请稍等" 
  sleep 10 
  break
else
sleep 10
fi
done
echo "让我休息15秒吧......"
sleep 15
grep "Successfully Setup Software."   nohup.out
grp4=$?
if [ "$grp4" == '0' ]
  then
 echo "install success,进行下一步配置"
else 
   echo "还未检测到成功信息,去看日志叭"
   echo "------退出本次安装------" 
   stopInstall
   filetobak
   exit 1   
fi
####执行脚本####
sh /home/oracle/app/oracle/oraInventory/orainstRoot.sh
sh /home/oracle/app/oracle/product/11.2.0/db_1/root.sh
######################创建itsm数据库实例######################
####修改建库响应文件####
echo "正在修改建库文件......"
 sed -i '78s#GDBNAME = "orcl11g.us.oracle.com"#GDBNAME ="'"$SIDNAME"'"#'        /home/oracle/database/response/dbca.rsp
 sed -i '170s#SID = "orcl11g"#SID ="'"$SIDNAME"'"#'        /home/oracle/database/response/dbca.rsp
 sed -i '418s#\#CHARACTERSET = "US7ASCII"#CHARACTERSET = "ZHS16GBK"#'        /home/oracle/database/response/dbca.rsp
 sed -i '428s#\#NATIONALCHARACTERSET= "UTF8"#NATIONALCHARACTERSET= "AL16UTF16"#'        /home/oracle/database/response/dbca.rsp
 sed -i '523s#\#MEMORYPERCENTAGE = "40"#MEMORYPERCENTAGE = "50"#'        /home/oracle/database/response/dbca.rsp
 sed -i '533s#\#DATABASETYPE = "MULTIPURPOSE"#DATABASETYPE = "MULTIPURPOSE"#'        /home/oracle/database/response/dbca.rsp
 sed -i '543s#\#AUTOMATICMEMORYMANAGEMENT = "TRUE"#AUTOMATICMEMORYMANAGEMENT = "TRUE"#'        /home/oracle/database/response/dbca.rsp
 sed -i '553s#\#TOTALMEMORY = "800"#TOTALMEMORY = "1024"#'        /home/oracle/database/response/dbca.rsp
 sed -i '475s#\#LISTENERS = "listener1 listener2"#LISTENERS = "LISTENER"#'        /home/oracle/database/response/dbca.rsp
 sed -i '211s#\#SYSPASSWORD = "password"#SYSPASSWORD = "'"$passwordAll"'"#'        /home/oracle/database/response/dbca.rsp
 sed -i '221s#\#SYSTEMPASSWORD = "password"#SYSTEMPASSWORD = "'"$passwordAll"'"#'        /home/oracle/database/response/dbca.rsp
echo "修改建库响应文件完毕......"
####创建数据库、配置oracle监听####
su - oracle<<EOF
cd $ORACLE_HOME/bin
echo "正在创建数据库中,请不要退出......"
dbca -silent -responsefile  /home/oracle/database/response/dbca.rsp
netca -silent -responsefile /home/oracle/database/response/netca.rsp
EOF
######################配置开机自启动######################
####修改oracle启停脚本####
echo "正在修改oracle启停脚本"
   sed -i '80s#ORACLE_HOME_LISTNER=\$1#ORACLE_HOME_LISTNER=\$ORACLE_HOME#' $ORACLE_HOME/bin/dbstart
   sed -i '50s#ORACLE_HOME_LISTNER=\$1#ORACLE_HOME_LISTNER=\$ORACLE_HOME#' $ORACLE_HOME/bin/dbshut
   sed -i '23s#itsmdb:/home/oracle/app/oracle/product/11.2.0/db_1:N#itsmdb:/home/oracle/app/oracle/product/11.2.0/db_1:Y#'    /etc/oratab
echo "修改oracle启停脚本成功"
####创建自动执行文件####
echo "正在创建自动执行文件......"
cat >>/etc/rc.d/init.d/oracle<<EOF
#!/bin/bash
# whoami # root 
# chkconfig: 345 51 49  
# description: starts the oracle dabasedeamons 
# 
ORACLE_HOME=/home/oracle/app/oracle/product/11.2.0/db_1
ORACLE_OWNER=oracle 
ORACLE_DESC="Oracle 11g" 
case "\$1" in 
'start') 
echo -n \"Starting \${ORACLE_DESC}:\" 
runuser - \$ORACLE_OWNER -c '\$ORACLE_HOME/bin/lsnrctl start' 
runuser - \$ORACLE_OWNER -c '\$ORACLE_HOME/bin/dbstart' 
runuser - \$ORACLE_OWNER -c '\$ORACLE_HOME/bin/emctl start dbconsole' 
touch \${ORACLE_LOCK} 
echo 
;; 
'stop') 
echo -n "shutting down \${ORACLE_DESC}: " 
runuser - \$ORACLE_OWNER -c '\$ORACLE_HOME/bin/lsnrctl stop' 
runuser - \$ORACLE_OWNER -c '\$ORACLE_HOME/bin/dbshut' 
rm -f \${ORACLE_LOCK} 
echo  
;; 
'restart') 
echo -n "restarting \${ORACLE_DESC}:" 
\$0 stop 
\$0 start 
echo 
;; 
*)  
echo "usage: \$0 { start | stop | restart }" 
exit 1 
esac 
exit 0
EOF
echo "创建自动执行文件成功......"
cd /etc/rc.d/init.d
chmod 755 oracle
chkconfig --add oracle
chk1=$?
chkconfig --level 345 oracle on
chk2=$?
if [[ "$chk1" == "0" && "$chk2" == "0" ]]
then
  echo  "配置开机自启动成功"
else
  echo "配置开机自启动失败,请检查......"
  exit 1
fi
service oracle restart
if [ "$?" == "0" ]
then 
  echo "oracle服务重启成功"
else
  echo "oracle服务重启失败,请检查......"
  exit1
fi
######################修改数据库连接端口号######################
su - oracle<<EOF
  lsnrctl stop
  sed -i 's/1521/$dbPort/g'  $ORACLE_HOME/network/admin/listener.ora
  
  sqlplus / as sysdba <<sqlcom
  show parameter local_listener;
  alter system set local_listener="(address = (protocol = tcp)(host = $theip)(port = $dbPort))";
  show parameter local_listener;
  quit
  sqlcom
EOF
echo "至此,安装完成"
######################尝试验证登入oracle######################
echo "--------------------查看oracle监听--------------------"
su - oracle -c "lsnrctl start"
su - oracle -c "lsnrctl status"
echo "--------------------尝试登入数据库--------------------"
su - oracle -c "sqlplus / as sysdba" 

附:脚本使用的oracle11G下载地址
链接:https://pan.baidu.com/s/1Z5e8FRggzLeFpKI4TN81wg
提取码:379y