基于oracle安装包中应答文件实现一键安装
支持环境:
Linux :centerOS 7
oracle :11.2.0
Oracle应答文件
runInstaller应答文件
/database/response/db_install.rsp
netca应答文件
/database/response/netca.rsp
dbca应答文件
/database/response/dbca.rsp
解压oracle安装包后在生成database目录
静默安装需先编辑对应应答文件
编辑完成后执行安装程序指定对应应答文件
一键安装脚本
基于oracle可以实现静默安装的前提、
我们可以将安装oracle的步骤整合为shell脚本、
同时增加命令行控制、
增加可自定义项、
#!/bin/bash
#Create by:zhangyongze
#为什么不问问神奇的海螺呢丶
#oracle一键安装脚本
#############################
#变量初始化
oracle_version=11.2.0
oracle_base=/oracledata/app/oracle
oracle_instance_name=orcl
oracle_file=""
oracle_unzip=/oracledata
oracle_log_file=`pwd`/oracle_install.log
oracle_password="sysadmin"
help_str="参数说明:--oracleversion, -v: oracle版本(支持11.2.0)--oraclebase, -b : oracle_base 选填 默认 /oracledata/app/oracle--oraclesid, -s : oracle_sid 选填 默认 orcl--installfile, -f : 安装文件(绝对路径) 必填--installdir, -d : 安装文件存放目录(绝对路径)选填 默认 /oracledata--installlog, -l : 安装日志(绝对路径) 选填 默认当前目录--syspasswd, -p : sys用户密码 选填 默认 sysadmin--help, -h : 帮助oracle_home =\${oracle_base}/product/\${oracle_version}/dbhome_1"# 定义命令执行选项
getopt_cmd=$(getopt -o v:b:s:f:d:l:p:h --long oracleversion:,oraclebase::,oraclesid::,installfile:,installdir:,installlog::,syspasswd::,help -n $(basename $0) -- "$@")
[ $? -ne 0 ] && exit 1
# 将规范化后的命令行参数分配至位置参数($1,$2,...)
eval set -- "$getopt_cmd"# 接受执行选项;赋值给变量
while [ -n "$1" ]
do
case "$1" in
-h|--help)echo -e "$help_str"exit ;;
-v|--oracleversion)oracle_version="$2"shift ;;
-b|--oraclebase)oracle_base="$2"shift ;;
-s|--oraclesid)oracle_instance_name="$2"shift ;;
-f|--installfile)oracle_file="$2"shift ;;
-d|--installdir)oracle_unzip="$2"shift ;;
-l|--installlog)oracle_log_file="$2"shift ;;
-p|--syspasswd)oracle_password="$2"shift ;;
--) shiftbreak ;;
*) echo "$1 is not an option"exit 1 ;; # 发现未知参数,直接退出esacshift
done##########变量################
oracle_home=${oracle_base}/product/${oracle_version}/dbhome_1
host_name=`hostname`
host_ip=`ip addr | grep inet | grep -v 127 | grep -v inet6 |awk '{print $2}'| grep -Eo "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}"|head -n 1`###########函数区#############
#打印输出
function wlog()
{content_str=$1var_color=$2var_curr_timestamp=`date "+%Y-%m-%d %H:%M:%S.%N" | cut -b 1-23`content_echo_str=""## 判断参数1 是否是空字符串if [ "x${content_str}" == "x" ];thenreturnelsecontent_str="[${var_curr_timestamp}] ${content_str}"ficontent_echo_str="${content_str}"## 判断颜色if [ "${var_color}" == "green" ];thencontent_echo_str="\033[32m${content_str}\033[0m"elif [ "${var_color}" == "yellow" ];thencontent_echo_str="\033[33m${content_str}\033[0m"elif [ "${var_color}" == "red" ];thencontent_echo_str="\033[1;41;33m${content_str}\033[0m"fi## 打印输出echo -e "${content_echo_str}"echo "${content_str}" >> ${oracle_log_file}
}
#判断/dev/shm是否足够2G,不够予以添加
addShm(){swap_zize=`df | grep /dev/shm | awk '{print $2 / 1024 / 1024}'`swap_zize=${swap_zize%.*}if [ ${swap_zize} -lt 2 ]then wlog '/dev/shm空间小于2G,正在为您扩充/dev/shm空间...'mount -o size=2000M -o nr_inodes=1000000 -o noatime,nodiratime -o remount /dev/shmecho 'tmpfs /dev/shm tmpfs defaults,size=1200M 0 0' >> /etc/fstabmount -o remount /dev/shmfi
}#判断交换空间是否足够,不够予以添加
addSwap(){swap_zize=`free | grep Swap | awk '{print $2 / 1024 / 1024}'`swap_zize=${swap_zize%.*}if [ ${swap_zize} -lt 1 ]then wlog '交换空间小于1G,正在为您扩充交换空间...'dd if=/dev/zero of=/root/swap bs=1024k count=1024 >> ${oracle_log_file} 2>&1mkswap /root/swap >> ${oracle_log_file} 2>&1swapon /root/swap >> ${oracle_log_file} 2>&1echo "/sbin/swapon /root/swap" >> /etc/rc.d/rc.localfi
}#添加oracle12c需要的依赖
installDependence(){wlog "正在安装数据库依赖环境..."yum -y install libnsl.x86_64 binutils compat-libcap1 compat-libstdc++-33 compat-libstdc++-33* elfutilslibelf-devel gcc gcc-c++ glibc* glibc glibc-devel glibc-devel* ksh libgcc* libgcc libstdc++ libstdc++ libstdc++-devel libstdc++-devel* libaio libaio* libaio-devel libaio-devel*.i686 make sysstat unixODBC unixODBC* unixODBC-devel unixODBCdevel* libXp unzip wget vim epel-release >> ${oracle_log_file} 2>&1wlog "数据库依赖环境已完成"
}#添加oracle用户组以及用户
addOracleUserAndGroup(){groupadd oinstallgroupadd dbagroupadd operuseradd -g oinstall -G dba,oper oraclewlog "oracle用户以及用户组已创建,使用oracle用户时自行修改密码"
}#修改环境变量
modifyEnvironment(){echo "${host_ip} ${host_name}" >> /etc/hostssed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config setenforce 0wlog "已关闭selinux"cp /etc/sysctl.conf /etc/sysctl.conf.bak_`date "+%Y-%m-%d_%H:%M:%S"`echo "fs.aio-max-nr = 1048576" >> /etc/sysctl.confecho "fs.file-max = 6815744" >> /etc/sysctl.confecho "kernel.shmall = 1073741824" >> /etc/sysctl.confecho "kernel.shmmax = 4398046511104" >> /etc/sysctl.confecho "kernel.shmmni = 4096" >> /etc/sysctl.confecho "kernel.sem = 250 32000 100 128" >> /etc/sysctl.confecho "net.ipv4.ip_local_port_range = 9000 65500" >> /etc/sysctl.confecho "net.core.rmem_default = 262144" >> /etc/sysctl.confecho "net.core.rmem_max = 4194304" >> /etc/sysctl.confecho "net.core.wmem_default = 262144" >> /etc/sysctl.confecho "net.core.wmem_max = 1048576" >> /etc/sysctl.confsysctl -p >> ${oracle_log_file} 2>&1wlog "/etc/sysctl.conf文件已经备份且修改为oracle官方要求的最小值"cp /etc/security/limits.conf /etc/security/limits.conf.bak_`date "+%Y-%m-%d_%H:%M:%S"`echo "oracle soft nproc 10240" >> /etc/security/limits.confecho "oracle hard nproc 10240" >> /etc/security/limits.confecho "oracle soft nofile 65536" >> /etc/security/limits.confecho "oracle hard nofile 65536" >> /etc/security/limits.confwlog "/etc/security/limits.conf文件已经备份且修改为oracle官方要求的最小值"cp /etc/pam.d/login /etc/pam.d/login_`date "+%Y-%m-%d_%H:%M:%S"`echo "session required pam_limits.so" >> /etc/pam.d/loginwlog "/etc/pam.d/login已修改,添加session required pam_limits.so"# echo "if [ $USER = ""oracle"" ]; then" >> /etc/profile# echo " if [ $SHELL = ""/bin/ksh"" ]; then" >> /etc/profile# echo " ulimit -p 16384" >> /etc/profile# echo " ulimit -n 65536" >> /etc/profile# echo " else" >> /etc/profile# echo " ulimit -u 16384 -n 65536" >> /etc/profile# echo " fi" >> /etc/profile# echo "fi" >> /etc/profile# source /etc/profile# wlog "/etc/profile已修改,限制其他用户使用"
}showBar(){i=$1jj=0((jj=i+3))jj=`echo "$i $jj"|awk '{printf "%d\n",$1/$2*100}'`msg=$2b=`echo "" | sed ":a; s/^/-/; /-\{${jj}\}/b; ta"`b="${b}>"if [ $msg == "执行中" ]then printf "[%-101s] %d%% %3s \r" "$b" "$jj" "$msg";elsejj=100b=`echo "" | sed ":a; s/^/-/; /-\{${jj}\}/b; ta"`b="${b}>"printf "[%-101s] %d%% %3s \n" "$b" "$jj" "$msg";fi
}function installOracle(){wlog "#########开始安装oracle数据库#########" "green"rm -rf ${oracle_unzip}/database >> ${oracle_log_file} 2>&1wlog "开始解压oracle数据库文件..."unzip ${oracle_file} -d ${oracle_unzip}/ >> ${oracle_log_file} 2>&1chown oracle:oinstall -R ${oracle_unzip}# mkdir -p ${oracle_home}# chown -R oracle:oinstall ${oracle_home}# chmod -R 775 ${oracle_home}wlog "oracle数据库文件解压完毕"echo "export TMP=/tmp" >> /home/oracle/.bash_profileecho "export TMPDIR=\$TMP" >> /home/oracle/.bash_profileecho "export ORACLE_HOSTNAME=${host_name}" >> /home/oracle/.bash_profileecho "export ORACLE_UNQNAME=${oracle_instance_name}" >> /home/oracle/.bash_profileecho "export ORACLE_BASE=${oracle_base}" >> /home/oracle/.bash_profileecho "export ORACLE_HOME=${oracle_home}" >> /home/oracle/.bash_profileecho "export ORACLE_SID=${oracle_instance_name}" >> /home/oracle/.bash_profileecho "export PATH=/usr/sbin:$PATH" >> /home/oracle/.bash_profileecho "export PATH=\${ORACLE_HOME}/bin:\$PATH" >> /home/oracle/.bash_profileecho "export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib" >> /home/oracle/.bash_profileecho "export CLASSPATH=$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib" >> /home/oracle/.bash_profileecho "export NLS_LANG=\"SIMPLIFIED CHINESE_CHINA.ZHS16GBK\"" >> /home/oracle/.bash_profilesource /home/oracle/.bash_profilewlog "oracle环境变量已经配置" wlog "正在修改安装文件......"a[0]="oracle.install.option=INSTALL_DB_SWONLY"a[1]="UNIX_GROUP_NAME=oinstall"a[2]="INVENTORY_LOCATION=${oracle_base}/oraInventory"a[3]="ORACLE_HOME=${oracle_home}"a[4]="ORACLE_BASE=${oracle_base}"a[5]="oracle.install.db.InstallEdition=EE"a[6]="oracle.install.db.DBA_GROUP=dba"a[7]="oracle.install.db.OPER_GROUP=oper"a[8]="oracle.install.db.BACKUPDBA_GROUP=dba"a[9]="oracle.install.db.DGDBA_GROUP=dba"a[10]="oracle.install.db.KMDBA_GROUP=dba"a[11]="oracle.install.db.RACDBA_GROUP=dba"a[12]="oracle.install.db.config.starterdb.type=GENERAL_PURPOSE"a[13]="oracle.install.db.config.starterdb.globalDBName=${oracle_instance_name}"a[14]="oracle.install.db.config.starterdb.SID=${oracle_instance_name}"a[15]="oracle.install.db.config.starterdb.characterSet=ZHS16GBK"a[16]="SECURITY_UPDATES_VIA_MYORACLESUPPORT=false"a[17]="DECLINE_SECURITY_UPDATES=true"for i in ${a[*]}; dosed -i "s#^${i%%=*}=.*#${i}#" "${oracle_unzip}/database/response/db_install.rsp"donewlog "修改安装文文件完毕......"#rm -rf ${oracle_file}wlog "开始安装数据库..."su - oracle <<EOF cd ${oracle_unzip}/databaseif [ -f oracle_install_log.out ]thenrm oracle_install_log.outfinohup ./runInstaller -force -silent -noconfig -responseFile ${oracle_unzip}/database/response/db_install.rsp -ignorePrereq >> oracle_install_log.out &
EOFii=0while truedosleep 1egrep -e "FATAL|Failed" ${oracle_unzip}/database/oracle_install_log.outmsg=$?if [ "$msg" == '0' ]thenshowBar ${ii} "失败"exitfigrep "Successfully Setup Software." ${oracle_unzip}/database/oracle_install_log.out >> ${oracle_log_file} 2>&1msg=$? if [ "$msg" == '0' ]thenshowBar 100 "成功"wlog "oracle数据库安装成功" "green"breakfishowBar ${ii} "执行中"((ii=ii+2))done`grep "orainstRoot.sh" ${oracle_unzip}/database/oracle_install_log.out >> ${oracle_log_file} 2>&1``grep "root.sh" ${oracle_unzip}/database/oracle_install_log.out >> ${oracle_log_file} 2>&1`su - oracle -c "netca /silent /responsefile ${oracle_unzip}/database/response/netca.rsp" >> ${oracle_log_file} 2>&1wlog "oracle数据库初始化完成" "green"}function installIntance(){wlog "#########开始安装oracle数据库单实例#########" "green"wlog "正在修改数据库实例配置文件......"rm -rf ${oracle_unzip}/database/response/dbca.rspecho "[GENERAL]" >> ${oracle_unzip}/database/response/dbca.rspecho "RESPONSEFILE_VERSION = "11.2.0" " >> ${oracle_unzip}/database/response/dbca.rspecho "OPERATION_TYPE = "createDatabase"" >> ${oracle_unzip}/database/response/dbca.rspecho "[CREATEDATABASE]" >> ${oracle_unzip}/database/response/dbca.rspecho "GDBNAME=${oracle_instance_name}" >> ${oracle_unzip}/database/response/dbca.rspecho "SID=${oracle_instance_name}" >> ${oracle_unzip}/database/response/dbca.rspecho "TEMPLATENAME = "General_Purpose.dbc"" >> ${oracle_unzip}/database/response/dbca.rspecho "SYSPASSWORD=${oracle_password}" >> ${oracle_unzip}/database/response/dbca.rspecho "SYSTEMPASSWORD=${oracle_password}" >> ${oracle_unzip}/database/response/dbca.rspecho "DATAFILEJARLOCATION=${ORACLE_HOME}/assistants/dbca/templates/" >> ${oracle_unzip}/database/response/dbca.rspecho "AUTOMATICMEMORYMANAGEMENT=TRUE" >> ${oracle_unzip}/database/response/dbca.rspecho "CHARACTERSET=ZHS16GBK" >> ${oracle_unzip}/database/response/dbca.rspecho "NATIONALCHARACTERSET=AL16UTF16" >> ${oracle_unzip}/database/response/dbca.rspecho "TOTALMEMORY=30000" >> ${oracle_unzip}/database/response/dbca.rspecho "RECOVERYAREADESTINATION=${ORACLE_BASE}/flash_recovery_area" >> ${oracle_unzip}/database/response/dbca.rspecho "STORAGETYPE="FS"" >> ${oracle_unzip}/database/response/dbca.rspecho "LISTENERS=LISTENER" >> ${oracle_unzip}/database/response/dbca.rsp# a[1]="GDBNAME=${oracle_instance_name}"# a[2]="SID=${oracle_instance_name}"# a[3]="RESPONSEFILE_VERSION=11.2.0"# a[4]="OPERATION_TYPE=createDatabase"# a[5]="TEMPLATENAME=General_Purpose.dbc"# a[6]="SYSPASSWORD=${oracle_password}"# a[7]="SYSTEMPASSWORD=${oracle_password}"# a[8]="DATAFILEJARLOCATION=${ORACLE_HOME}/assistants/dbca/templates/"# a[9]="DATAFILEDESTINATION=${ORACLE_BASE}/oradata/"# a[10]="CHARACTERSET=ZHS16GBK"# a[11]="NATIONALCHARACTERSET=AL16UTF16"# a[12]="MEMORYPERCENTAGE=40"# a[13]="AUTOMATICMEMORYMANAGEMENT=FALSE"# a[14]="TOTALMEMORY=30000"# a[15]="RECOVERYAREADESTINATION=${ORACLE_BASE}/flash_recovery_area"# a[16]="LISTENERS=LISTENER"# a[17]="STORAGETYPE="FS""# for i in ${a[*]}; do# sed -i "s#^${i%%=*}=.*#${i}#" "${oracle_unzip}/database/response/dbca.rsp"# donewlog "修改数据库实例配置文件完毕......"su - oracle -c "dbca -silent -responseFile ${oracle_unzip}/database/response/dbca.rsp" >> ${oracle_log_file} 2>&1su - oracle -c "mkdir -p ${oracle_home}/oradata/${oracle_instance_name}/" >> ${oracle_log_file} 2>&1wlog "oracle数据库实例安装完成!" "green"
}
addBootstrap(){sed -i "s/^ORACLE_HOME_LISTNER=.*/ORACLE_HOME_LISTNER=\$ORACLE_HOME/" ${oracle_home}/bin/dbstartsed -i "s/^ORACLE_HOME_LISTNER=.*/ORACLE_HOME_LISTNER=\$ORACLE_HOME/" ${oracle_home}/bin/dbshutrm -rf /etc/oratab echo "${oracle_instance_name}:${oracle_home}:Y" >> /etc/oratabcat >>/etc/rc.d/init.d/oracle<<EOF
#!/bin/bash
# whoami # root
# chkconfig: 345 51 49
# description: starts the oracle dabasedeamons
#
ORACLE_HOME=${oracle_home}
ORACLE_OWNER=oracle
ORACLE_DESC=oracledatabase
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
EOFchmod 755 /etc/rc.d/init.d/oraclechkconfig --add oracle
}main(){clearcat null > ${oracle_log_file}chmod 777 ${oracle_log_file}wlog "开始为您一键安装oracle数据库,请确认一下信息" "green"wlog "Oracle版本号:${oracle_version}"wlog "Oracle主目录:${oracle_base}"wlog "Oracle安装目录:${oracle_home}"wlog "Oracle数据库实例名称:${oracle_instance_name}"wlog "Oracle数据库系统用户初始密码:${oracle_password}"wlog "Oracle数据库解压目录:${oracle_unzip}"wlog "Oracle数据库安装文件:${oracle_file}"wlog "安装详细日志文件:${oracle_log_file}"wlog "确认以此参数安装,请输入Y,开始安装,修改参数请按其他任意键结束安装:" "red"read -t 10 confirm_strif [ "$confirm_str" != 'y' ] && [ "$confirm_str" != 'Y' ];thenecho ""wlog "一键安装oracle数据库已停止,您可以再修改完参数后再次执行。" "red"exitfiif [ $(id -u) != "0" ];then wlog "Oracle数据库安装需要以root用户执行!" "red"exitfiif [ ! -f ${oracle_file} ];then wlog "Oracle数据库文件不存在!" "red"exit fiwlog "#########开始初始化安装环境#########" "green" installDependencemodifyEnvironmentaddSwapaddShmaddOracleUserAndGroupinstallOracleinstallIntanceaddBootstrapwlog "已为您添加自启动脚本..." "green"wlog "安装程序已全部结束,请留意防火墙状态并尝试连接数据库进行测试" "red"wlog "关闭防火墙命令: systemctl stop firewalld" "green"wlog "关闭数据库命令: service oracle stop" "green"wlog "开启数据库命令: service oracle start" "green"wlog "重启数据库命令: service oracle restart" "green"wlog "您的数据库连接地址:${host_ip}:1521/${oracle_instance_name}" "green"wlog "管理员用户名: system" "green"wlog "初始密码: ${oracle_password}" "green"
}main
执行脚本 -h 查看帮助文档
oracle11g一键安装脚本
寻找缺失的依赖包
解决思路
找同系统版本未安装Oracle的数据库
配置网络yum源
使用yum downloadonly 命令 下载包
yum install --downloadonly binutils compat-libcap1 compat-libstdc++-33 compat-libstdc++-33.i686 glibc glibc.i686 glibc-devel glibc-devel.i686 ksh libaio libaio.i686 libaio-devel libaio-devel.i686 libX11 libX11.i686 libXau libXau.i686 libXi libXi.i686 libXtst libXtst.i686 libgcc libgcc.i686 libstdc++ libstdc++.i686 libstdc++-devel libstdc++-devel.i686 libxcb libxcb.i686 make nfs-utils net-tools smartmontools sysstat unixODBC unixODBC-devel gcc gcc-c++ libXext libXext.i686 zlib-devel zlib-devel.i686 unzip wget vim epel-release --downloaddir=/oralcepkg
打包传输到需要安装Oracle的服务器
解压后 yum -y install *