#!/bin/bash
ORACLE_BASE=/data/app/oracle
ORACLE_HOME=${ORACLE_BASE}/product/19.3.0/db_1
ORACLE_Inventory=${ORACLE_BASE}/oraInventory
ORACLE_DATA=${ORACLE_BASE}/oradata
ORACLE_RECOVERY=${ORACLE_BASE}/flash_recovery_area
ORACLE_SID=ora19c
GLOBAL_DBNAME=ora19c
pdbName=orapdb
pdbAdminPassword='orapdb@Admin123'
archivelog=${ORACLE_BASE}/archive
characterSet=AL32UTF8
password_ALL=A8c5sfh3j6bab11cc8d0e835c40#f
sysPassword='Dzda@123'
systemPassword='Dzda@123'
dbsnmpPassword='Dzda@123'
log=/tmp/oracle_install.log
Oracle_file='/tmp/LINUX.X64_193000_db_home.zip'
####安装Oracle所需依赖包
function yum_install(){
yum -y install bc binutils compat-libcap1 compat-libstdc++-33 elfutils-libelf elfutils-libelf-devel fontconfig-devel glibc glibc-devel ksh libaio libaio-devel libX11 libXau libXi libXtst libXrender libXrender-devel libgcc libstdc++ libstdc++-devel libxcb make gcc gcc-c++ net-tools nfs-utils python python-configshell python-rtslib python-six targetcli smartmontools sysstat epel-release
yum -y update
yum -y install rlwrap
}
####配置/etc/hosts
function get_host(){
ip_dir=`ip a|grep -w inet|grep -v '127.0.0.1'|awk '{print $2}'|sed -n '1p'`
hname=`hostname`
red=`cat /etc/hosts|grep "$hname"|wc -l`
if [ $red -eq 1 ]
then
get_log "info" "/etc/hosts is OK Nothing to do"
else
echo "$ip_dir" "$hname" >>/etc/hosts
get_log "info" "/etc/hosts is OK"
fi
}
####创建Oracle数据库安装所需多个目录与Oracle账户
function create_sys_user(){
groupadd -g 501 oinstall
groupadd -g 502 dba
groupadd -g 503 asmdba
groupadd -g 504 backupdba
groupadd -g 505 dgdba
groupadd -g 506 kmdba
groupadd -g 507 racdba
groupadd -g 508 oper
useradd -u 501 -g oinstall -G dba,asmdba,backupdba,dgdba,kmdba,racdba,oper oracle
id oracle
uid=`echo $?`
if [ ${uid} -eq 0 ]
then
get_log "info" "oracle用户及用户组创建完成"
else
get_log "error" "oracle用户及用户组创建发生错误${uid},请检查!"
exit
fi
}
#echo oracle:oracle | chpasswd
function sys_config(){
###环境变量
hostname=`hostname`
echo '
PATH=$PATH:$HOME/bin
umask 022
export ORACLE_BASE='"${ORACLE_BASE}"'
export ORACLE_SID='"${ORACLE_SID}"'
export ORACLE_HOME='"$ORACLE_HOME"'
export PATH=$HOME/bin:$ORACLE_HOME/bin:$PATH:$ORACLE_HOME/OPatch/
export TMP=/tmp
export TMPDIR=/tmp
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
export LC_TYPE=en_US.UTF-8
export PATH
alias sqlplus="rlwrap sqlplus"
alias rman="rlwrap rman"
'>>/home/oracle/.bash_profile
su - oracle <<EOF
source .bash_profile
EOF
get_log "info" "oracle用户oracle环境变量设置完成"
echo '
PATH=$PATH:$HOME/bin
umask 022
export ORACLE_BASE='"${ORACLE_BASE}"'
export ORACLE_SID='"${ORACLE_SID}"'
export ORACLE_HOME='"$ORACLE_HOME"'
export PATH=$HOME/bin:$ORACLE_HOME/bin:$PATH:$ORACLE_HOME/OPatch/
export TMP=/tmp
export TMPDIR=/tmp
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
export LC_TYPE=en_US.UTF-8
export PATH
alias sqlplus="rlwrap sqlplus"
alias rman="rlwrap rman"
'>>/etc/profile
get_log "info" "root用户oracle环境变量设置完成"
####配置系统内核
g=`free -b|grep Mem|awk '{print $2}'`
h=$(($g*4/5))
shmall=$(($h/4096))
echo "
#oracle
kernel.sysrq = 0
kernel.msgmnb = 65536
kernel.shmall = $shmall
fs.file-max = 6815744
kernel.msgmni = 2878
kernel.msgmax = 8192
kernel.msgmnb = 65536
kernel.sem = 250 32000 100 142
kernel.shmmni = 4096
kernel.shmmax = $h
kernel.sysrq = 0
net.core.wmem_default = 262144
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_max = 1048576
fs.aio-max-nr = 3145728
net.ipv4.ip_local_port_range = 9000 65500
vm.min_free_kbytes = 51200" >> /etc/sysctl.conf
/sbin/sysctl -p
get_log "info" "sysctl设置完成"
echo "session required pam_limits.so">> /etc/pam.d/login
get_log "info" "/etc/pam.d/login设置完成"
echo "
oracle soft nproc 2047
oracle hard nproc 16384
oracle soft nofile 1024
oracle hard nofile 65536" >>/etc/security/limits.conf
get_log "info" "limits.conf设置完成"
echo '
if [ $USER = "oracle" ]||[ $USER = "grid" ]; then
if [ $SHELL = "/bin/ksh" ]; then
ulimit -p 16384
ulimit -n 65536
else
ulimit -u 16384 -n 65536
fi
umask 022
fi'>>/etc/profile
source /etc/profile
get_log "info" "oracle用户ulimit设置完成"
}
# 定义颜色变量
GREEN='\033[0;32m'
RED='\033[0;31m'
BLUE='\033[0;36m'
YELLOW='\033[0;33m'
WHITE='\033[0;37m'
NC='\033[0m' # No Color
function get_log(){
v_mssg1=$1
v_mssg2=$2
v_logtime=`date "+%Y-%m-%d %H:%M:%S"`
if [[ "$v_mssg1" = "info" ]];then
echo -e "$v_logtime ${GREEN} [$v_mssg1] $2 ${NC}" |tee -a ${log}
elif [[ "$v_mssg1" = "title" ]];then
echo -e "$v_logtime ${BLUE} [$v_mssg1] $2 ${NC}"|tee -a ${log}
elif [[ "$v_mssg1" = "warning" ]];then
echo -e "$v_logtime ${YELLOW} [$v_mssg1] $2 ${NC}"|tee -a ${log}
elif [[ "$v_mssg1" = "error" ]];then
echo -e "$v_logtime ${RED} [$v_mssg1] $2 ${NC}"|tee -a ${log}
fi
}
function mkdir_oracle_dir(){
get_log "info" ">>>> 创建oracle目录并授权"
mkdir -p ${ORACLE_HOME}
mkdir -p ${archivelog}
chown -R oracle:oinstall ${ORACLE_BASE}
chmod -R 775 ${ORACLE_BASE}
chown -R oracle:oinstall ${log}
}
##解压缩Oracle安装软件
function unzip_file(){
get_log "info" ">>>> 开始解压oracle安装软件"
#udir=${ORACLE_HOME}
#if [ -e $udir ]
#then
# echo "Oracle soft has been created"
# exit
#else
unzip ${Oracle_file} -d ${ORACLE_HOME}
ox=`echo $?`
if [ ${ox} -eq 0 ]
then
get_log "info" "安装包解压完成"
chown -R oracle:oinstall ${ORACLE_BASE}
chmod -R 775 ${ORACLE_BASE}
else
get_log "error" "安装包解压发生错误${ox}"
exit
fi
#fi
}
##安装Oracle软件
function install_oracle(){
get_log "info" ">>>> 开始安装oracle软件"
install_rsp=$ORACLE_HOME/install/response/db_install.rsp
cd $ORACLE_HOME/install/response
cp ${install_rsp} ${install_rsp}_bk
get_log "info" "开始配置db_install.rsp参数"
sed -i 's/^oracle.install.option=/oracle.install.option=INSTALL_DB_SWONLY/g' ${install_rsp}
sed -i 's/^UNIX_GROUP_NAME=/UNIX_GROUP_NAME=oinstall/g' ${install_rsp}
sed -i 's#^INVENTORY_LOCATION=#INVENTORY_LOCATION='"${ORACLE_Inventory}"'#g' ${install_rsp}
sed -i 's#^ORACLE_HOME=#ORACLE_HOME='"${ORACLE_HOME}"'#g' ${install_rsp}
sed -i 's#^ORACLE_BASE=#ORACLE_BASE='"${ORACLE_BASE}"'#g' ${install_rsp}
sed -i 's/^oracle.install.db.InstallEdition=/oracle.install.db.InstallEdition=EE/g' ${install_rsp}
sed -i 's/^oracle.install.db.OSDBA_GROUP=/oracle.install.db.OSDBA_GROUP=dba/g' ${install_rsp}
sed -i 's/^oracle.install.db.OSOPER_GROUP=/oracle.install.db.OSOPER_GROUP=oper/g' ${install_rsp}
sed -i 's/^oracle.install.db.OSBACKUPDBA_GROUP=/oracle.install.db.OSBACKUPDBA_GROUP=backupdba/g' ${install_rsp}
sed -i 's/^oracle.install.db.OSDGDBA_GROUP=/oracle.install.db.OSDGDBA_GROUP=dgdba/g' ${install_rsp}
sed -i 's/^oracle.install.db.OSKMDBA_GROUP=/oracle.install.db.OSKMDBA_GROUP=kmdba/g' ${install_rsp}
sed -i 's/^oracle.install.db.OSRACDBA_GROUP=/oracle.install.db.OSRACDBA_GROUP=racdba/g' ${install_rsp}
sed -i 's/^oracle.install.db.rootconfig.executeRootScript=false/oracle.install.db.rootconfig.executeRootScript=true/g' ${install_rsp}
sed -i 's/^oracle.install.db.rootconfig.configMethod=/oracle.install.db.rootconfig.configMethod=ROOT/g' ${install_rsp}
sed -i 's/^oracle.install.db.config.starterdb.type=/oracle.install.db.config.starterdb.type=GENERAL_PURPOSE/g' ${install_rsp}
sed -i 's/^oracle.install.db.config.starterdb.globalDBName=/oracle.install.db.config.starterdb.globalDBName='"${GLOBAL_DBNAME}"'/g' ${install_rsp}
sed -i 's/^oracle.install.db.config.starterdb.SID=/oracle.install.db.config.starterdb.SID='"${ORACLE_SID}"'/g' ${install_rsp}
sed -i 's/^oracle.install.db.ConfigureAsContainerDB=/oracle.install.db.ConfigureAsContainerDB=true/g' ${install_rsp}
sed -i 's/^oracle.install.db.config.PDBName=/oracle.install.db.config.PDBName='"${pdbName}"'/g' ${install_rsp}
sed -i 's/^oracle.install.db.config.starterdb.characterSet=/oracle.install.db.config.starterdb.characterSet='"${characterSet}"'/g' ${install_rsp}
sed -i 's/^oracle.install.db.config.starterdb.memoryOption=/oracle.install.db.config.starterdb.memoryOption=false/g' ${install_rsp}
sed -i 's/^oracle.install.db.config.starterdb.memoryLimit=/oracle.install.db.config.starterdb.memoryLimit=8192/g' ${install_rsp}
sed -i 's/^oracle.install.db.config.starterdb.installExampleSchemas=/oracle.install.db.config.starterdb.installExampleSchemas=true/g' ${install_rsp}
sed -i 's/^oracle.install.db.config.starterdb.password.ALL=/oracle.install.db.config.starterdb.password.ALL='"${password_ALL}"'/g' ${install_rsp}
sed -i 's/^oracle.install.db.config.starterdb.managementOption=/oracle.install.db.config.starterdb.managementOption=DEFAULT/g' ${install_rsp}
sed -i 's/^oracle.install.db.config.starterdb.enableRecovery=/oracle.install.db.config.starterdb.enableRecovery=true/g' ${install_rsp}
sed -i 's/^oracle.install.db.config.starterdb.storageType=/oracle.install.db.config.starterdb.storageType=FILE_SYSTEM_STORAGE/g' ${install_rsp}
sed -i 's#^oracle.install.db.config.starterdb.fileSystemStorage.dataLocation=#oracle.install.db.config.starterdb.fileSystemStorage.dataLocation='"${ORACLE_DATA}"'#g' ${install_rsp}
sed -i 's#^oracle.install.db.config.starterdb.fileSystemStorage.recoveryLocation=#oracle.install.db.config.starterdb.fileSystemStorage.recoveryLocation='"${ORACLE_RECOVERY}"'#g' ${install_rsp}
sed -i 's/^oracle.install.db.rootconfig.executeRootScript=/oracle.install.db.rootconfig.executeRootScript=false/g' ${install_rsp}
get_log "info" "db_install.rsp参数配置完成,开始安装oracle软件"
install_log=${ORACLE_HOME}/install/response/ora_install.log
get_log "info" "安装日志文件 ${install_log}"
su - oracle >>${install_log} <<EOF
cd ${ORACLE_HOME}
./runInstaller -silent -responseFile ${install_rsp} >>${log}
EOF
sleep 30
get_log "info" "开始执行orainstRoot.sh和root.sh脚本"
sh ${ORACLE_Inventory}/orainstRoot.sh
sh ${ORACLE_HOME}/root.sh
p=`cat ${log} |grep -w 'Successfully Setup Software'|wc -l`
#r='Successfully Setup Software'
if [ ${p} -eq 1 ]
then
get_log "info" "oracle软件安装完成"
else
get_log "error" "oracle软件安装发生错误"
exit
fi
}
###oracle配置监听
function create_netca(){
get_log "info" ">>>> 开始配置oracle监听服务"
netca_rsp=`find ${ORACLE_HOME} -name netca.rsp`
netca_log=${ORACLE_HOME}/assistants/netca/net.log
get_log "info" "配置监听日志文件 ${netca_log}"
su - oracle >> ${netca_log} <<EOF
netca -silent -responseFile ${netca_rsp}
EOF
t=`echo $?`
if [ $t -eq 0 ]
then
get_log "info" "netca 配置完成"
else
get_log "error" "netca 配置发生错误"
exit
fi
get_log "info" "写入监听文件listener.ora"
echo '
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = '"${GLOBAL_DBNAME}"')
(ORACLE_HOME = '"${ORACLE_HOME}"')
(SID_NAME = '"${ORACLE_SID}"')
)
(SID_DESC =
(GLOBAL_DBNAME = '"${pdbName}"')
(SID_NAME = '"${ORACLE_SID}"')
)
)
'>> $ORACLE_HOME/network/admin/listener.ora
su - oracle <<EOF
lsnrctl stop
lsnrctl start
EOF
get_log "info" "oracle监听服务配置完成"
}
###oracle创建数据库
function create_db(){
get_log "info" ">>>> oracle开始创建数据库"
dbca_rsp=${ORACLE_HOME}/assistants/dbca/dbca.rsp
cd ${ORACLE_HOME}/assistants/dbca/
cp ${dbca_rsp} ${dbca_rsp}_bk
get_log "info" "开始配置dbca.rsp参数"
dbca_log=${ORACLE_HOME}/assistants/dbca/dbca.log
get_log "info" "创建数据库日志文件 ${dbca_log}"
# 这里两条配置的是数据库的名字和sid
sed -i 's/^gdbName=/gdbName='"${ORACLE_SID}"'/g' ${dbca_rsp}
sed -i 's/^sid=/sid='"${ORACLE_SID}"'/g' ${dbca_rsp}
sed -i 's/^databaseConfigType=/databaseConfigType=SI/g' ${dbca_rsp}
sed -i 's/^templateName=/templateName=General_Purpose.dbc/g' ${dbca_rsp}
# 这两条配置的是数据库的密码
sed -i 's/^sysPassword=/sysPassword='"${sysPassword}"'/g' ${dbca_rsp}
sed -i 's/^systemPassword=/systemPassword='"${systemPassword}"'/g' ${dbca_rsp}
sed -i 's/^pdbAdminPassword=/pdbAdminPassword='"${pdbAdminPassword}"'/g' ${dbca_rsp}
sed -i 's/^emConfiguration=/emConfiguration=DBEXPRESS/g' ${dbca_rsp}
sed -i 's/^dbsnmpPassword=/dbsnmpPassword='"${dbsnmpPassword}"'/g' ${dbca_rsp}
sed -i 's#^datafileDestination=#datafileDestination='"${ORACLE_DATA}"'#g' ${dbca_rsp}
sed -i 's/^characterSet=/characterSet='"${characterSet}"'/g' ${dbca_rsp}
sed -i 's/^createAsContainerDatabase=/createAsContainerDatabase=true/g' ${dbca_rsp}
sed -i 's/^numberOfPDBs=/numberOfPDBs=1/g' ${dbca_rsp}
sed -i 's/^pdbName=/pdbName='"${pdbName}"'/g' ${dbca_rsp}
#sed -i 's/^totalMemory=/totalMemory=15360/g' ${dbca_rsp}
get_log "info" "dbca.rsp参数配置完成,开始安装数据库"
su - oracle >>${dbca_log} <<EOF
dbca -silent -createDatabase -responseFile $dbca_rsp
EOF
x=$?
if [ $x -eq 0 ]
then
get_log "info" "dbca安装数据库完成"
else
get_log "error" "dbca安装数据库发生错误"
exit
fi
}
function config_update(){
####变更Oracle参数
su - oracle <<EOF
sqlplus / as sysdba << S
alter system set db_files=5000 scope=spfile;
alter system set memory_max_target=2320M scope=spfile;
alter system set memory_target=2320M scope=spfile;
alter system set processes=10000 scope=spfile;
alter system set session_cached_cursors=1000 scope=spfile;
alter system set open_cursors=1000 scope=spfile;
alter system set log_archive_dest_1='location=/data/app/oracle/archive/archivelog';
create pfile from spfile;
shutdown immediate;
exit;
S
sqlplus / as sysdba << Z
startup mount;
alter database archivelog;
alter database open;
create pfile from spfile;
alter pluggable database orapdb open;
alter session set container=orapdb;
create user dbadmin identified by "dbadmin@123A#";
grant dba to dbadmin;
exit;
Z
EOF
echo "Oracle install is OK!"
}
yum_install
get_host
create_sys_user
sys_config
mkdir_oracle_dir
unzip_file
install_oracle
create_netca
create_db
config_update