参考文章
linux安装Oracle11G
Oracle 设置开机自启动
oracle 内核参数设置
下载地址
脚本
# vim:sw=4:ts=4:et
<<INFO
AUTHOR:运维@小兵
DATE:2021-08-28
DESCRIBE:一键安装/卸载oracle(rpm版)
SYSTEM:CentOS 7.6.1810
WARNING:警告信息
MODIFY:
INFO
set -e
[[ $# -ne 1 ]] && echo "ERROR:Invalid Param!!!,Please Excute:bash $0 (install|uninstall)" && exit 1
source /etc/profile
WORKDIR=$(cd `dirname $0`;pwd)
LOG_NAME=${WORKDIR}/install_oracle.log
ORACLE_VERSION="11.2.0.4" #oracle版本
DATA_DIR="/deploy/oracle" #数据目录
ORACLE_BASE=${DATA_DIR}
ORACLE_HOME="${DATA_DIR}/product/${ORACLE_VERSION}"
ORACLE_USER="oracle"
ORACLE_GROUP="oinstall"
ORACLE_SID="orcl"
ORACLE_PORT=1521
SYSPASSWORD="123456" #SYS管理员密码
SYSTEMPASSWORD="123456" #SYSTEM管理员密码
SYSMANPASSWORD="123456" #SYSMAN管理员密码
DBSNMPPASSWORD="123456" #DBSNMP管理员密码
RC_LOCAL_CONF="/etc/rc.d/rc.local" #开机自启文件
ORACLE_KERNEL_CONFIG="/etc/sysctl.d/99-oracle-database-preinstall-11c-sysctl.conf" #内核文件
SYSTEM_MEM=$(free | grep -w '^Mem:' | awk '{print $2}') #物理内存大小(KB)
#${FUNCNAME[1]代表调用该函数的函数,$LINENO代表当前代码行号
Log(){
local log_level=$1
local log_info=$2
local line=$3
local script_name=$(basename $0)
case ${log_level} in
"INFO")
echo -e "\033[32m$(date "+%Y-%m-%d %T.%N") [INFO]: ${log_info}\033[0m";;
"WARN")
echo -e "\033[33m$(date "+%Y-%m-%d %T.%N") [WARN]: ${log_info}\033[0m";;
"ERROR")
echo -e "\033[31m$(date "+%Y-%m-%d %T.%N") [ERROR ${script_name} ${FUNCNAME[1]}:$line]: ${log_info}\033[0m";;
*)
echo -e "${@}"
;;
esac
}
Check_Env(){
Log INFO "Begin Check_Env..."
Log INFO "log file path:${LOG_NAME}"
[[ $UID -ne 0 ]] && Log ERROR "Please Use Admin(root) Excute......" && exit 1
! command -v rpm &> /dev/null && Log ERROR "rpm Command Not Exist" && exit 1
[[ ! -d ${WORKDIR}/rpms ]] && Log ERROR "${WORKDIR}/rpms Not Found" && exit 1
! command -v ss &> /dev/null && rpm -ivh ${WORKDIR}/rpms/iproute-4.11.0-30.el7.x86_64.rpm --nodeps
! command -v unzip &> /dev/null && rpm -ivh ${WORKDIR}/rpms/unzip-6.0-22.el7_9.x86_64.rpm --nodeps
if [ -f /etc/redhat-release ]; then
local os_version=$(cat /etc/redhat-release)
Log INFO "操作系统为: ${os_version}"
else
Log ERROR "Only Support CentOS Or RedHat" $LINENO && exit 1
fi
}
#关闭防火墙,禁用selinux
Config_Security(){
Log INFO "Begin Config Security..."
systemctl disable firewalld &>/dev/null || echo "skip"
systemctl stop firewalld &>/dev/null || echo "skip"
selinux_mode=$(grep '^SELINUX=' /etc/selinux/config |awk -F'=' '{print $2}')
if [ ${selinux_mode} != "disabled" ];then
setenforce 0
sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config
Log WARN "selinux需重启系统才能生效"
fi
}
#安装依赖包
Install_Depend(){
Log INFO "Begin Install Depend Rpms..."
rpm -ivh ${WORKDIR}/rpms/*.rpm --nodeps || echo "skip"
Log INFO "Install Depend Rpms Success"
}
#创建用户和组
Create_User(){
Log INFO "Begin Create User..."
if ! grep "^${ORACLE_GROUP}:" /etc/group &> /dev/null;then
groupadd -g 500 ${ORACLE_GROUP}
fi
if ! id ${ORACLE_USER} &> /dev/null;then
useradd -s /bin/bash -u 500 -g ${ORACLE_GROUP} -m ${ORACLE_USER}
echo "${ORACLE_USER}:1q2w3e4r" | chpasswd
fi
}
#修改内核参数
Config_Kernel(){
Log INFO "Begin Config Kernel..."
local config_name="${ORACLE_KERNEL_CONFIG}"
if [[ -f ${config_name} ]];then
[[ ! -f ${config_name}.bak ]] && cp ${config_name}{,.bak}
fi
tee ${config_name} << EOF
fs.aio-max-nr = 1048576
fs.file-max = 65536
kernel.shmall = $(echo $[SYSTEM_MEM / 4])
kernel.shmmax = $(echo $[SYSTEM_MEM / 2 * 1024])
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
sysctl -p
}
#修改用户限制文件
Config_User_Limit(){
Log INFO "Begin Config /etc/security/limits.conf..."
local config_name="/etc/security/limits.conf"
[[ ! -f ${config_name}.bak ]] && cp ${config_name}{,.bak}
sed -i "/^${ORACLE_USER} /d" ${config_name}
tee -a ${config_name} << EOF
${ORACLE_USER} soft nproc 2047
${ORACLE_USER} hard nproc 16384
${ORACLE_USER} soft nofile 1024
${ORACLE_USER} hard nofile 65536
${ORACLE_USER} soft stack 10240
EOF
}
#创建swap空间
Create_Swap(){
Log INFO "Begin Create Swap ..."
local swap_info=$(swapon -s)
if [[ -z ${swap_info} ]];then
mkdir -p /swap
dd if=/dev/zero of=/swap/swap.swp bs=1M count=4000
mkswap -f /swap/swap.swp
[[ ! -f /etc/fstab.bak ]] && cp /etc/fstab{,.bak}
sed -i '/swap.swp/d' /etc/fstab
echo '/swap/swap.swp swap swap 0 0' >>/etc/fstab
chmod 600 /swap/swap.swp
swapon -a
swapon -s
else
echo ${swap_info}
fi
}
#创建数据库目录
Create_Database_Dir(){
Log INFO "Begin Create Oracle Dir..."
mkdir -p ${DATA_DIR}/{oradata,inventory,fast_recovery_area,software}
mkdir -p ${ORACLE_HOME}
chown -R ${ORACLE_USER}:${ORACLE_GROUP} ${DATA_DIR}
chmod -R 775 ${DATA_DIR}
}
#配置Oracle环境变量
Config_Oracle_Env(){
Log INFO "Begin Config Oracle Environment..."
local config_name="/home/${ORACLE_USER}/.bash_profile"
[[ ! -f ${config_name}.bak ]] && cp ${config_name}{,.bak}
sed -i '/ORACLE/d' ${config_name}
tee -a ${config_name} << EOF
ORACLE_BASE=${ORACLE_BASE}
ORACLE_HOME=${ORACLE_HOME}
ORACLE_SID=${ORACLE_SID}
PATH=\$PATH:\$ORACLE_HOME/bin
export ORACLE_BASE ORACLE_HOME ORACLE_SID PATH
EOF
}
#准备安装包
Prepare_Install_Pkg(){
Log INFO "Begin Prepare Install Pkg..."
cd ${WORKDIR}/packages
[[ ! -f "linux.x64_11gR2_database_1of2.zip" ]] && Log ERROR "linux.x64_11gR2_database_1of2.zip" $LINENO && exit 1
[[ ! -f "linux.x64_11gR2_database_2of2.zip" ]] && Log ERROR "linux.x64_11gR2_database_2of2.zip" $LINENO && exit 1
\cp *.zip ${DATA_DIR}/software/
cd ${DATA_DIR}/software/
Log INFO "Begin Uncompress Pkg..."
unzip -qo linux.x64_11gR2_database_1of2.zip
unzip -qo linux.x64_11gR2_database_2of2.zip
chown -R ${ORACLE_USER}:${ORACLE_GROUP} ${DATA_DIR}
Log INFO "Uncompress Pkg Success..."
}
#创建静默安装响应文件
Create_Install_Rsp(){
Log INFO "Begin Create Install Rsp File..."
local config_name="/home/${ORACLE_USER}/response/db_install.rsp"
local hostname=$(cat /etc/hostname)
[[ -z ${hostname} ]] && Log ERROR "hostname Is Null" $LINENO && exit 1
if ! ping -c3 -i0.2 -W1 ${hostname} &> /dev/null;then
Log ERROR "${hostname} Unreachable,Please Check" $LINENO && exit 1
fi
\cp -pr ${DATA_DIR}/software/database/response /home/${ORACLE_USER}
sed -i "s/^oracle.install.option=.*/oracle.install.option=INSTALL_DB_SWONLY/g" ${config_name} #仅安装软件不创建数据库
sed -i "s/^ORACLE_HOSTNAME=.*/ORACLE_HOSTNAME=${hostname}/g" ${config_name} #服务器主机名
sed -i "s/^UNIX_GROUP_NAME=.*/UNIX_GROUP_NAME=${ORACLE_GROUP}/g" ${config_name} #软件安装所需群组
sed -i "s#^INVENTORY_LOCATION=.*#INVENTORY_LOCATION=${DATA_DIR}/inventory#g" ${config_name} #指定资产清单目录
sed -i "s/^SELECTED_LANGUAGES=.*/SELECTED_LANGUAGES=en,zh_CN/g" ${config_name} #软件语言
sed -i "s#^ORACLE_HOME=.*#ORACLE_HOME=${ORACLE_HOME}#g" ${config_name} #ORACLE_HOME
sed -i "s#^ORACLE_BASE=.*#ORACLE_BASE=${ORACLE_BASE}#g" ${config_name} #ORACLE_BASE
sed -i "s/^oracle.install.db.InstallEdition=.*/oracle.install.db.InstallEdition=EE/g" ${config_name} #安装版本,本例选择企业版
sed -i "s/^oracle.install.db.DBA_GROUP=.*/oracle.install.db.DBA_GROUP=${ORACLE_GROUP}/g" ${config_name} #安装软件时的指定用户组
sed -i "s/^oracle.install.db.OPER_GROUP=.*/oracle.install.db.OPER_GROUP=${ORACLE_GROUP}/g" ${config_name}
sed -i "s/^DECLINE_SECURITY_UPDATES=.*/DECLINE_SECURITY_UPDATES=true/g" ${config_name}
}
#安装前准备
Pre_Install(){
Config_Security
Install_Depend
Create_User
Config_Kernel
Config_User_Limit
Create_Swap
Create_Database_Dir
Config_Oracle_Env
Prepare_Install_Pkg
}
#创建库和实例
Create_Db_Instance(){
Log INFO "Begin Create Db And Instance..."
local config_name="/home/${ORACLE_USER}/response/dbca.rsp"
local total_memory=$(echo $[SYSTEM_MEM / 1024 * 8 / 10]) #MB
[[ ! -f ${config_name}.bak ]] && cp ${config_name}{,.bak}
sed -i "s/^GDBNAME = .*/GDBNAME = ${ORACLE_SID}/g" ${config_name} #全局数据库的名字
sed -i "s/^SID = .*/SID = ${ORACLE_SID}/g" ${config_name} #对应的实例名字
sed -i "s/#SYSPASSWORD = .*/SYSPASSWORD = ${SYSPASSWORD}/g" ${config_name}
sed -i "s/#SYSTEMPASSWORD = .*/SYSTEMPASSWORD = ${SYSTEMPASSWORD}/g" ${config_name}
sed -i "s/#SYSMANPASSWORD = .*/SYSMANPASSWORD = ${SYSMANPASSWORD}/g" ${config_name}
sed -i "s/#DBSNMPPASSWORD = .*/DBSNMPPASSWORD = ${DBSNMPPASSWORD}/g" ${config_name}
sed -i "/#DATAFILEDESTINATION =/c DATAFILEDESTINATION = ${DATA_DIR}/oradata" ${config_name} #数据文件存放目录
sed -i "/#RECOVERYAREADESTINATION=/c RECOVERYAREADESTINATION = ${DATA_DIR}/fast_recovery_area" ${config_name} #恢复数据存放目录
sed -i "s/^#CHARACTERSET = .*/CHARACTERSET = AL32UTF8/g" ${config_name} #字符集,重要
sed -i "s/^#TOTALMEMORY = .*/TOTALMEMORY = ${total_memory}/g" ${config_name} # oracle内存=物理内存*80%
chown -R ${ORACLE_USER}:${ORACLE_GROUP} /home/${ORACLE_USER}/
su - ${ORACLE_USER} -c "dbca -silent -responseFile /home/${ORACLE_USER}/response/dbca.rsp" #进行静默配置
}
#配置开机自启
Config_Auto_Start(){
Log INFO "Begin Config Auto Start Oracle..."
if [[ -f /etc/oratab ]];then
[[ ! -f /etc/oratab.bak ]] && cp /etc/oratab{,.bak}
sed -i "/${ORACLE_VERSION}/c ${ORACLE_SID}:${ORACLE_HOME}:Y" /etc/oratab
fi
[[ ! -f ${RC_LOCAL_CONF}.bak ]] && cp ${RC_LOCAL_CONF}{,.bak}
sed -i "/su - ${ORACLE_USER} -c 'lsnrctl start'/d" ${RC_LOCAL_CONF}
sed -i "/su - ${ORACLE_USER} -c 'dbstart \${ORACLE_HOME}'/d" ${RC_LOCAL_CONF}
echo "su - ${ORACLE_USER} -c 'lsnrctl start'" >> ${RC_LOCAL_CONF}
echo "su - ${ORACLE_USER} -c 'dbstart \${ORACLE_HOME}'" >> ${RC_LOCAL_CONF}
}
#打印安装信息
Print_Info(){
local host_ip=$(ip addr |awk '/inet /' |sed -n '2p' |awk -F' ' '{print $2}' |awk -F'/' '{print $1}')
su - ${ORACLE_USER} -c "echo 'select * from global_name;' | sqlplus system/123456@${host_ip}/${ORACLE_SID}"
if [[ $? -eq 0 ]];then
echo
echo
echo
Log INFO "Install Oracle Success"
echo "#####################Oracle Install Info#####################"
echo "Oracle Version:${ORACLE_VERSION}"
echo "Oracle_Instance:${ORACLE_SID}"
echo "Oracle_Home:${ORACLE_HOME}"
echo "Table_Space_File_Path:${DATA_DIR}/oradata/${ORACLE_SID}"
echo "DATA_PUMP_DIR:${DATA_DIR}/admin/${ORACLE_SID}/dpdump"
else
Log ERROR "Install Oracle Fail" $LINENO && exit 1
fi
}
#安装Oracle
Install_Oracle(){
if ss -nutlp | egrep ":${ORACLE_PORT}\b" &> /dev/null;then
Log WARN "Oracle Is Installed" && exit 0
fi
Pre_Install
Create_Install_Rsp
Log INFO "Begin Init Oracle..."
su - ${ORACLE_USER} -c "cd ${DATA_DIR}/software/database && ./runInstaller -silent -responseFile /home/${ORACLE_USER}/response/db_install.rsp -ignorePrereq" #静默安装
rm -f ${DATA_DIR}/inventory/logs/*.log
local num=0
while [[ ${num} -le 60 ]]
do
sleep 5
if grep '^INFO: Exit Status is 0$' ${DATA_DIR}/inventory/logs/*.log;then
Log INFO "Init Oracle Success" && break
else
num=$(echo $[num+1])
fi
done
[[ ${num} -gt 60 ]] && Log ERROR "Init Oracle Fail" $LINENO && exit 1
[[ -f ${DATA_DIR}/inventory/orainstRoot.sh ]] && bash ${DATA_DIR}/inventory/orainstRoot.sh
bash ${ORACLE_HOME}/root.sh
su - ${ORACLE_USER} -c "netca /silent /responseFile /home/oracle/response/netca.rsp" #以静默方式配置监听
if ss -nutlp | egrep ":${ORACLE_PORT}\b" &> /dev/null;then
Log INFO "Listen Oracle Success"
else
Log ERROR "Listen Oracle Fail" $LINENO && exit 1
fi
Create_Db_Instance
Config_Auto_Start
Print_Info
}
#卸载Oracle
Uninstall_Oracle(){
if [[ ! -d ${DATA_DIR} ]];then
Log WARN "Oracle Is Uninstalled" && exit 0
fi
Log INFO "Begin Uninstall Oracle..."
su - ${ORACLE_USER} -c "echo 'SHUTDOWN IMMEDIATE;' | sqlplus / as sysdba" || echo "skip 001"
su - ${ORACLE_USER} -c "lsnrctl stop" || echo "skip 002"
if ! ss -nutlp | egrep ":${ORACLE_PORT}\b" &> /dev/null;then
if [[ $(ps -ef | grep -v grep | grep -w "^oralce") -eq 0 ]];then
rm -f /etc/oratab
rm -f oraInst.loc
[[ ! -f ${RC_LOCAL_CONF}.bak ]] && cp ${RC_LOCAL_CONF}{,.bak}
sed -i "/su - ${ORACLE_USER} -c 'lsnrctl start'/d" ${RC_LOCAL_CONF}
sed -i "/su - ${ORACLE_USER} -c 'dbstart \${ORACLE_HOME}'/d" ${RC_LOCAL_CONF}
userdel -r ${ORACLE_USER}
groupdel ${ORACLE_GROUP}
rm -f ${ORACLE_KERNEL_CONFIG}
sysctl -p
sed -i "/^${ORACLE_USER} /d" /etc/security/limits.conf
rm -rf ${DATA_DIR}
Log INFO "Uninstal Oracle Success" && exit 0
fi
else
Log ERROR "Stop Oracle Fail" && exit 1
fi
}
######################主程序######################
set -o pipefail
Check_Env | tee -a ${LOG_NAME}
case $1 in
install)
Install_Oracle | tee -a ${LOG_NAME};;
uninstall)
Uninstall_Oracle | tee -a ${LOG_NAME};;
*)
Log ERROR "Invalid Param!!!,Please Excute:bash $0 (install|uninstall)" $LINENO && exit 1
esac
执行效果
bash install_oracle.sh install