#!/bin/bash
#Create by:ember.zhang
#oracle一键安装脚本
#############################
#变量初始化
oracle_version=19.0.0
oracle_base=/data/oracle
oracle_instance_name=orcl
oracle_file=""
oracle_unzip=${oracle_base}/product/${oracle_version}/dbhome_1
oracle_log_file=`pwd`/oracle_install.log
opatch_zip=""
oracle_update=""
oracle_password="sysadmin"
help_str="参数说明:
--oracleversion, -v: oracle版本(支持19.0.0)
--oraclebase, -b : oracle_base 选填 默认 /data/oracle
--oraclesid, -s : oracle_sid 选填 默认 orcl
--installfile, -f : 安装文件(绝对路径) 必填
--installdir, -d : 安装文件存放目录(绝对路径)选填 19c默认 \${oracle_base}/product/\${oracle_version}/dbhome_1
--installlog, -l : 安装日志(绝对路径) 选填 默认当前目录
--opatchzip, -o : opatch升级包(绝对路径) 选填 默认空
--oracleupdate, -u : oracle补丁包(绝对路径) 选填 默认空
--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:o:u:p:h --long oracleversion:,oraclebase::,oraclesid::,installfile:,installdir:,installlog::,opatchzip::,oracleupdate::,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 ;;
-o|--opatchzip)
opatch_zip="$2"
shift ;;
-u|--oracleupdate)
oracle_update="$2"
shift ;;
-p|--syspasswd)
oracle_password="$2"
shift ;;
--)
shift
break ;;
*)
echo "$1 is not an option"
exit 1 ;; # 发现未知参数,直接退出
esac
shift
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=$1
var_color=$2
var_curr_timestamp=`date "+%Y-%m-%d %H:%M:%S.%N" | cut -b 1-23`
content_echo_str=""
## 判断参数1 是否是空字符串
if [ "x${content_str}" == "x" ];then
return
else
content_str="[${var_curr_timestamp}] ${content_str}"
fi
content_echo_str="${content_str}"
## 判断颜色
if [ "${var_color}" == "green" ];then
content_echo_str="\033[32m${content_str}\033[0m"
elif [ "${var_color}" == "yellow" ];then
content_echo_str="\033[33m${content_str}\033[0m"
elif [ "${var_color}" == "red" ];then
content_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(){
shm_size=`df | grep /dev/shm | awk '{print $2 / 1024 / 1024}'`
shm_size=${shm_size%.*}
if [ ${shm_size} -lt 2 ]
then
wlog '/dev/shm空间小于2G,正在为您扩充/dev/shm空间...'
mount -o size=2000M -o nr_inodes=1000000 -o noatime,nodiratime -o remount /dev/shm
echo 'tmpfs /dev/shm tmpfs defaults,size=1200M 0 0' >> /etc/fstab
mount -o remount /dev/shm
fi
}
#判断交换空间是否足够,不够予以添加
addSwap(){
swap_size=`free | grep Swap | awk '{print $2 / 1024 / 1024}'`
swap_size=${swap_size%.*}
if [ ${swap_size} -lt 16 ]
then
needed_swap=$((16 - swap_size))
new_swap_file="/swapfile_$(date +%s)"
wlog '交换空间小于16G,正在为您扩充交换空间...'
dd if=/dev/zero of="$new_swap_file" bs=1024k count=$((needed_swap*1024)) >> ${oracle_log_file} 2>&1
mkswap "$new_swap_file" >> ${oracle_log_file} 2>&1
swapon "$new_swap_file" >> ${oracle_log_file} 2>&1
echo "$new_swap_file none swap defaults 0 0" >> /etc/fstab
fi
}
#添加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>&1
wlog "数据库依赖环境已完成"
}
#添加oracle用户组以及用户
addOracleUserAndGroup(){
groupadd oinstall
groupadd dba
groupadd oper
useradd -g oinstall -G dba,oper oracle
wlog "oracle用户以及用户组已创建,使用oracle用户时自行修改密码"
}
#修改环境变量
modifyEnvironment(){
echo "${host_ip} ${host_name}" >> /etc/hosts
sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
setenforce 0
wlog "已关闭selinux"
cp /etc/sysctl.conf /etc/sysctl.conf.bak_`date "+%Y-%m-%d_%H:%M:%S"`
echo "fs.aio-max-nr = 1048576" >> /etc/sysctl.conf
echo "fs.file-max = 6815744" >> /etc/sysctl.conf
echo "kernel.shmall = 1073741824" >> /etc/sysctl.conf
echo "kernel.shmmax = 4398046511104" >> /etc/sysctl.conf
echo "kernel.shmmni = 4096" >> /etc/sysctl.conf
echo "kernel.sem = 250 32000 100 128" >> /etc/sysctl.conf
echo "net.ipv4.ip_local_port_range = 9000 65500" >> /etc/sysctl.conf
echo "net.core.rmem_default = 262144" >> /etc/sysctl.conf
echo "net.core.rmem_max = 4194304" >> /etc/sysctl.conf
echo "net.core.wmem_default = 262144" >> /etc/sysctl.conf
echo "net.core.wmem_max = 1048576" >> /etc/sysctl.conf
sysctl -p >> ${oracle_log_file} 2>&1
wlog "/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 16384" >> /etc/security/limits.conf
echo "oracle hard nproc 16384" >> /etc/security/limits.conf
echo "oracle soft nofile 65536" >> /etc/security/limits.conf
echo "oracle hard nofile 65536" >> /etc/security/limits.conf
wlog "/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/login
wlog "/etc/pam.d/login已修改,添加session required pam_limits.so"
}
showBar(){
i=$1
jj=0
((jj=i+3))
jj=`echo "$i $jj"|awk '{printf "%d\n",$1/$2*100}'`
msg=$2
b=`echo "" | sed ":a; s/^/-/; /-\{${jj}\}/b; ta"`
b="${b}>"
if [ $msg == "执行中" ]
then
printf "[%-101s] %d%% %3s \r" "$b" "$jj" "$msg";
else
jj=100
b=`echo "" | sed ":a; s/^/-/; /-\{${jj}\}/b; ta"`
b="${b}>"
printf "[%-101s] %d%% %3s \n" "$b" "$jj" "$msg";
fi
}
function installOracle(){
wlog "#########开始安装oracle数据库#########" "green"
wlog "创建文件夹${oracle_home}oracle_home..."
mkdir -p ${oracle_home} >> ${oracle_log_file} 2>&1
chown -R oracle:oinstall ${oracle_home} >> ${oracle_log_file} 2>&1
chmod -R 775 ${oracle_home} >> ${oracle_log_file} 2>&1
wlog "开始解压oracle数据库安装包..."
unzip -q ${oracle_file} -d ${oracle_unzip}/
chown -R oracle:oinstall ${oracle_base}
wlog "oracle数据库文件解压完毕"
echo "export TMP=/tmp" >> /home/oracle/.bash_profile
echo "export TMPDIR=\$TMP" >> /home/oracle/.bash_profile
echo "export ORACLE_HOSTNAME=${host_name}" >> /home/oracle/.bash_profile
echo "export ORACLE_UNQNAME=${oracle_instance_name}" >> /home/oracle/.bash_profile
echo "export ORACLE_BASE=${oracle_base}" >> /home/oracle/.bash_profile
echo "export ORACLE_HOME=${oracle_home}" >> /home/oracle/.bash_profile
echo "export ORACLE_SID=${oracle_instance_name}" >> /home/oracle/.bash_profile
echo "export PATH=/usr/sbin:$PATH" >> /home/oracle/.bash_profile
echo "export PATH=\${ORACLE_HOME}/bin:\$PATH" >> /home/oracle/.bash_profile
echo "export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib" >> /home/oracle/.bash_profile
echo "export CLASSPATH=$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib" >> /home/oracle/.bash_profile
echo "export NLS_LANG=\"AMERICAN_AMERICA.AL32UTF8\"" >> /home/oracle/.bash_profile
source /home/oracle/.bash_profile
wlog "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.OSDBA_GROUP=dba"
a[7]="oracle.install.db.OSOPER_GROUP=oper"
a[8]="oracle.install.db.OSBACKUPDBA_GROUP=dba"
a[9]="oracle.install.db.OSDGDBA_GROUP=dba"
a[10]="oracle.install.db.OSKMDBA_GROUP=dba"
a[11]="oracle.install.db.OSRACDBA_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=AL32UTF8"
a[16]="oracle.install.db.config.starterdb.memoryOption=false"
a[17]="oracle.install.db.config.starterdb.installExampleSchemas=false"
a[18]="oracle.install.db.config.starterdb.managementOption=DEFAULT"
a[19]="oracle.install.db.rootconfig.method=sudo"
a[20]="oracle.install.db.config.starterdb.enableRecovery=false"
#a[21]="oracle.install.db.config.starterdb.enableRecovery=true"
#a[22]="oracle.install.db.config.starterdb.recoveryAreaSize=4096"
#a[23]="oracle.install.db.config.starterdb.recoveryAreaDestination=${oracle_base}/fast_recovery_area"
for i in ${a[*]}; do
sed -i "s#^${i%%=*}=.*#${i}#" "${oracle_unzip}/install/response/db_install.rsp"
done
wlog "修改安装文文件完毕......"
#rm -rf ${oracle_file}
wlog "开始安装数据库..."
su - oracle <<EOF
cd ${oracle_unzip}
if [ -f oracle_install_log.out ]
then
rm oracle_install_log.out
fi
nohup ${oracle_unzip}/runInstaller -force -silent -noconfig -responseFile ${oracle_unzip}/install/response/db_install.rsp -ignorePrereq >> ${oracle_unzip}/install/oracle_install_log.out &
EOF
ii=0
while true
do
sleep 1
egrep -e "FATAL|Failed" ${oracle_unzip}/install/oracle_install_log.out
msg=$?
if [ "$msg" == '0' ]
then
showBar ${ii} "失败"
exit
fi
grep "Successfully Setup Software." ${oracle_unzip}/install/oracle_install_log.out >> ${oracle_log_file} 2>&1
msg=$?
if [ "$msg" == '0' ]
then
showBar 100 "成功"
wlog "oracle数据库软件安装成功" "green"
break
fi
showBar ${ii} "执行中"
((ii=ii+2))
done
`grep "orainstRoot.sh" ${oracle_unzip}/install/oracle_install_log.out >> ${oracle_log_file} 2>&1`
`grep "root.sh" ${oracle_unzip}/install/oracle_install_log.out >> ${oracle_log_file} 2>&1`
wlog "oracle数据库初始化完成" "green"
wlog "开始创建监听(netca)" "green"
su - oracle -c "${oracle_unzip}/bin/netca /silent /responsefile ${oracle_unzip}/assistants/netca/netca.rsp" >> ${oracle_log_file} 2>&1
wlog "oracle监听创建完成" "green"
}
function backupSoftware() {
wlog "开始备份Oracle软件" "green"
tar -czf oraclebak.tar.gz ${oracle_base} >> ${oracle_log_file} 2>&1
wlog "oracle软件备份完成oraclebak.tar.gz" "green"
}
function updateOpatch(){
wlog "#########开始升级OPatch#########" "green"
if [ ! -f "$opatch_zip" ]; then
wlog "未指定OPatch包,跳过OPatch升级" "green"
else
rm -rf ${oracle_home}/OPatch >> ${oracle_log_file} 2>&1
unzip -q ${opatch_zip} -d ${oracle_home}/
chown -R oracle:oinstall ${oracle_home}/OPatch >> ${oracle_log_file} 2>&1
wlog "OPatch升级完成" "green"
fi
}
function updateOracle(){
wlog "#########开始为oracle安装补丁#########" "green"
if [ ! -f "$oracle_update" ]; then
wlog "未指定oracle补丁包,跳过补丁安装" "green"
else
wlog "关闭监听" "green"
su - oracle -c "lsnrctl stop" >> ${oracle_log_file} 2>&1
wlog "关闭数据库" "green"
su - oracle -c " sqlplus / as sysdba <<EOF
shutdown immediate;
exit;
EOF" >> ${oracle_log_file} 2>&1
unzip -q ${oracle_update} -d ${oracle_base}/ou >> ${oracle_log_file} 2>&1
chown -R oracle:oinstall ${oracle_base}/ou >> ${oracle_log_file} 2>&1
patch_dirs=$(find ${oracle_base}/ou/ -type d -name etc -exec dirname {} \; | sort -u)
if [ -z "$patch_dirs" ]; then
wlog "未找到补丁目录(包含etc文件夹的目录),补丁安装失败" "red"
else
wlog "找到以下补丁目录:" "green"
echo "$patch_dirs" | while read -r dir; do
wlog " - $dir" "green"
done
echo "$patch_dirs" | while read -r patch_dir; do
wlog "===== 开始应用补丁: $patch_dir =====" "green"
wlog "检查补丁先决条件..." "green"
su - oracle -c "${oracle_home}/OPatch/opatch prereq CheckConflictAgainstOHWithDetail -ph ${patch_dir}" >> ${oracle_log_file} 2>&1
prereq_status=$?
if [ $prereq_status -ne 0 ]; then
wlog "警告: 补丁先决条件检查失败,继续执行..." "green"
else
wlog "补丁先决条件检查通过" "green"
fi
wlog "开始应用补丁..." "green"
su - oracle -c "${oracle_home}/OPatch/opatch apply -silent -local ${patch_dir}" >> ${oracle_log_file} 2>&1
apply_status=$?
if [ $apply_status -eq 0 ]; then
wlog "补丁 ${patch_dir} 应用成功" "green"
else
wlog "补丁 ${patch_dir} 应用失败" "green"
fi
wlog "===== 补丁 $patch_dir 处理完成 =====" "green"
done
fi
wlog "oracle 补丁安装完成" "green"
wlog "开启数据库" "green"
su - oracle -c " sqlplus / as sysdba <<EOF
startup;
exit;
EOF" >> ${oracle_log_file} 2>&1
wlog "开启监听" "green"
su - oracle -c "lsnrctl start" >> ${oracle_log_file} 2>&1
fi
}
function installIntance(){
wlog "#########开始安装oracle数据库单实例#########" "green"
wlog "正在修改数据库实例配置文件......"
a[1]="gdbName=${oracle_instance_name}"
a[2]="sid=${oracle_instance_name}"
a[3]="databaseConfigType=SI"
a[4]="policyManaged=false"
a[5]="createServerPool=false"
a[6]="force=false"
a[7]="templateName=${oracle_home}/assistants/dbca/templates/General_Purpose.dbc" #可以通过修改此文件开启归档<archiveLogMode>true</archiveLogMode>
a[8]="sysPassword=${oracle_password}"
a[9]="systemPassword=${oracle_password}"
a[10]="runCVUChecks=false"
a[11]="datafileJarLocation={ORACLE_HOME}/assistants/dbca/templates/"
a[12]="dvConfiguration=false"
a[13]="olsConfiguration=false"
a[14]="datafileDestination=${ORACLE_BASE}/oradata/"
a[15]="storageType=FS"
a[16]="characterSet=AL32UTF8"
a[17]="nationalCharacterSet=AL16UTF16"
a[18]="registerWithDirService=false"
a[19]="listeners=LISTENER"
a[20]="recoveryAreaDestination=${ORACLE_BASE}/flash_recovery_area"
a[21]="sampleSchema=false"
a[22]="databaseType=MULTIPURPOSE"
a[23]="memoryPercentage=70"
a[24]="automaticMemoryManagement=false"
a[25]="totalMemory=10240"
escape_sed() {
echo "$1" | sed 's/[\/&]/\\&/g'
}
for i in "${a[@]}"; do
key="${i%%=*}"
value="${i#*=}"
escaped_value=$(escape_sed "$value")
sed -i "s#^${key}=.*#${key}=${escaped_value}#" "${oracle_unzip}/assistants/dbca/dbca.rsp"
done
# for i in ${a[*]}; do
# sed -i "s#^${i%%=*}=.*#${i}#" "${oracle_unzip}/assistants/dbca/dbca.rsp"
# done
wlog "修改数据库实例配置文件完毕......"
su - oracle -c "dbca -silent -createDatabase -responseFile ${oracle_unzip}/assistants/dbca/dbca.rsp" >> ${oracle_log_file} 2>&1
su - oracle -c "mkdir -p ${oracle_home}/oradata/${oracle_instance_name}/" >> ${oracle_log_file} 2>&1
wlog "oracle数据库实例安装完成!" "green"
}
addBootstrap(){
sed -i "s/^ORACLE_HOME_LISTNER=.*/ORACLE_HOME_LISTNER=\$ORACLE_HOME/" ${oracle_home}/bin/dbstart
sed -i "s/^ORACLE_HOME_LISTNER=.*/ORACLE_HOME_LISTNER=\$ORACLE_HOME/" ${oracle_home}/bin/dbshut
rm -rf /etc/oratab
echo "${oracle_instance_name}:${oracle_home}:Y" >> /etc/oratab
cat >>/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
EOF
chmod 755 /etc/rc.d/init.d/oracle
chkconfig --add oracle
}
main(){
clear
cat 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_str
if [ "$confirm_str" != 'y' ] && [ "$confirm_str" != 'Y' ];
then
echo ""
wlog "一键安装oracle数据库已停止,您可以再修改完参数后再次执行。" "red"
exit
fi
if [ $(id -u) != "0" ];
then
wlog "Oracle数据库安装需要以root用户执行!" "red"
exit
fi
if [ ! -f ${oracle_file} ];
then
wlog "Oracle数据库文件不存在!" "red"
exit
fi
wlog "#########开始初始化安装环境#########" "green"
installDependence
modifyEnvironment
addSwap
addShm
addOracleUserAndGroup
installOracle
backupSoftware
updateOpatch
updateOracle
installIntance
addBootstrap
wlog "已为您添加自启动脚本..." "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
# ./zyzoracleinstall19c.sh -s nubtest -f /data/LINUX.X64_193000_db_home.zip -o /data/p6880880_19120144_Linux-x86-64.zip -u /data/p36522439_19240716_grid_Linux-x86-64.zip
Oracle database 静默安装 oracle19c 一键安装
于 2025-07-17 13:52:07 首次发布