注:(仅供学习参考)本脚本经过本人多次测试成功,但由于oracle 11g安装复杂,需要定义各种参数、变量,小小错误就会导致安装失败。请各位在测试机上测试完成后,方可用于正式环境。(QQ:755136755)
#!/bin/bash
#Global environment variable
HostIP="192.168.2.57"
HostName="centos7_57"
echo "${HostIP} ${HostName}" >> /etc/hosts
OracleUserPasswd="ABC123@" #oraclet用户的密码
ORACLE_DB_PASSWD="ABC123@" #数据库的密码
ORACLE_BASE="/data/soft/oracle"
ORACLE_HOME="${ORACLE_BASE}/product/11.2.0"
ORACLE_SID="orcl"
NLS_LANG="AMERICAN_AMERICA.ZHS16GBK" #系统变量
CHARACTERSET="ZHS16GBK" #dbca_rsp数据库编码
GDBNAME="ORCL.ABC.COM" #数据库格式:
#域名由各国文字的特定字符集、英文字母、数字及“-”(即连字符或减号)任意组合而成, 但开头及结尾均不能含有“-”。
#内存计算,#物理内存不少于1G,swap分区空间不少于2G kernel.shmmax
MemTotle_BYTE=`awk '($1 == "MemTotal:"){print $2}' /proc/meminfo` #内存(byte)
MemTotle_BYTE_KERNEL=`echo $MemTotle_BYTE | awk '{print $1-1}'` #除以2
MemTotle_BYTE_KERNEL=`echo $MemTotle_BYTE_KERNEL | awk -F"." '{print $1}'` #去除小数点
if [[ $MemTotle_BYTE_KERNEL -lt 1073741824 ]];then
MemTotle_BYTE_KERNEL="1073741824"
echo "内存少于1G手动设置为1073741824(1G)"
else
echo "内存大于于1G:${MemTotle_BYTE_KERNEL}"
fi
MemTotle_MB=`awk '($1 == "MemTotal:"){print $2/1024}' /proc/meminfo` #内存(MB)
MemTotle_MB_80=`echo $MemTotle_MB | awk -F"." '{print $1*0.8}'` #内存的80%
MemTotle_MB_80=`echo $MemTotle_MB_80 | awk -F"." '{print $1}'` #去除小数点
#安装文件参数配置的临时文件
rm -f /tmp/oracle_env_tmp.txt
rm -f /tmp/kernel_config_tmp.txt
rm -f /tmp/oracle_install_rsp.txt
rm -f /tmp/oracle_dbca_rsp.txt
#oracle 用户变量设置
cat >> /tmp/oracle_env_tmp.txt <
NLS_LANG=${NLS_LANG}
ORACLE_BASE=${ORACLE_BASE}
ORACLE_HOME=\${ORACLE_BASE}/product/11.2.0
ORACLE_SID=${ORACLE_SID}
PATH=\$PATH:\${ORACLE_HOME}/bin
export NLS_LANG ORACLE_BASE ORACLE_HOME ORACLE_SID PATH
echo "oracle .bash_profile exec!!!"
EOF
cat >> /tmp/kernel_config_tmp.txt <
fs.aio-max-nr = 1048576
fs.file-max = 6815744
kernel.shmall = 2097152
kernel.shmmax = ${MemTotle_BYTE_KERNEL}
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
cat >> /tmp/oracle_install_rsp.txt <
ORACLE_HOSTNAME=${HostName}
UNIX_GROUP_NAME=oinstall
INVENTORY_LOCATION=${ORACLE_BASE}/inventory
SELECTED_LANGUAGES=en,zh_CN
ORACLE_HOME=${ORACLE_BASE}/product/11.2.0
oracle.install.db.config.starterdb.SID=${ORACLE_SID}
ORACLE_BASE=${ORACLE_BASE}
oracle.install.db.InstallEdition=EE
oracle.install.db.DBA_GROUP=dba
oracle.install.db.OPER_GROUP=dba
DECLINE_SECURITY_UPDATES=true
EOF
#/data/oracle/oradata
#/data/oracle/fast_recovery_area
#GDBNAME 全局数据库名
cat >> /tmp/oracle_dbca_rsp.txt <
SID="${ORACLE_SID}"
SYSPASSWORD="${ORACLE_DB_PASSWD}"
SYSTEMPASSWORD="${ORACLE_DB_PASSWD}"
SYSMANPASSWORD="${ORACLE_DB_PASSWD}"
DBSNMPPASSWORD="${ORACLE_DB_PASSWD}"
DATAFILEDESTINATION=${ORACLE_BASE}/oradata
RECOVERYAREADESTINATION=${ORACLE_BASE}/fast_recovery_area
CHARACTERSET="${CHARACTERSET}"
TOTALMEMORY="${MemTotle_MB_80}"
EOF
#其中TOTALMEMORY = "1638" 为1638MB,物理内存2G*百分之80"。
#---------------------------------------------------------------------------------#
#Judgment parameter
function print_color () #颜色输出函数
{
case $1 in
red)
echo -e "\033[31m$2 \033[0m"
;;
green)
echo -e "\033[32m$2 \033[0m"
;;
yellow)
echo -e "\033[33m$2 \033[0m"
;;
blue)
echo -e "\033[34m$2 \033[0m"
;;
*)
echo -e "\033[31m[颜色错误]$2 \033[0m"
;;
esac
}
function j_para() {
print_color "green" "检查参数!"
#判断必要参数是否存在
if [ ${HostIP} == '' -a ${ORACLE_SID} == '' ];then
echo -e "\033[34mInstallNotice >>\033[0m \033[05;31mPlease config HostIP&ORACLE_SID\033[0m"
exit
fi
print_color "green" "HOSTNAME:${HostName},IP:${HostIP},SID:${ORACLE_SID}"
#判断数据库包文件是否存在
if [ ! -f "/tmp/linux.x64_11gR2_database_1of2.zip" ]; then
print_color "red" "/tmp/linux.x64_11gR2_database_1of2.zip not found"
exit
elif [ ! -f "/tmp/linux.x64_11gR2_database_2of2.zip" ]; then
print_color "red" "/tmp/linux.x64_11gR2_database_2of2.zip not found"
exit
fi
if [ -d "/tmp/database/" ];then
print_color "red" "/tmp/database/ 已存在,目录冲突,请删除,再执行脚本!"
exit 1
fi
user_i=`cat /etc/passwd | cut -f1 -d':' | grep -w "oracle" -c`
if [ $user_i -gt 0 ]; then
#显示用户存在
print_color "red" "oracle用户已存在!请备份home目录,并执行userdel -r oracle,删除用户!"
exit 1
fi
}
#install package
function install_package() {
print_color "green" "正在yum安装必要的包..."
yum install -y -q 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
}
#base_config
function base_config() {
# echo "${HostIP} ${HostName}" >> /etc/hosts
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
systemctl stop firewalld && systemctl disable firewalld
print_color "green" "添加oracle用户和组"
#添加用户和组
groupadd oinstall && groupadd dba && groupadd oper
useradd -g oinstall -G dba,oper oracle && echo "$OracleUserPasswd" | passwd oracle --stdin
#mkdir oracle need directory
#ORACLE_BASE=/u01/app/oracle
#ORACLE_HOME=$ORACLE_BASE/product/11.2.0
print_color "green" "创建基本文件夹"
mkdir -p ${ORACLE_HOME}
mkdir ${ORACLE_BASE}/oradata #存放数据库的数据目录
mkdir ${ORACLE_BASE}/oinventory #oracle install 安装日志存放目录
mkdir ${ORACLE_BASE}/fast_recovery_area #数据库归档日志放置在fast_recovery_area
chmod -R 775 ${ORACLE_BASE}
chown -R oracle:oinstall ${ORACLE_BASE}
print_color "green" "修改用户的限制文件"
echo "
oracle soft nproc 2047
oracle hard nproc 16384
oracle soft nofile 1024
oracle hard nofile 65536
oracle soft stack 10240
" >> /etc/security/limits.conf
print_color "green" "修改/etc/pam.d/login文件"
echo "
session required /lib64/security/pam_limits.so
session required pam_limits.so
" >> /etc/pam.d/login
print_color "green" "修改/etc/profile文件"
echo "
if [ $USER = "oracle" ]; then
if [ $SHELL = "/bin/ksh" ]; then
ulimit -p 16384
ulimit -n 65536
else
ulimit -u 16384 -n 65536
fi
fi
" >> /etc/profile
print_color "green" "设置oracle用户变量"
#导入oracle变量。add oracle environmental variable
cat /tmp/oracle_env_tmp.txt >> /home/oracle/.bash_profile
cat /home/oracle/.bash_profile
bash /home/oracle/.bash_profile
chmod 755 /home/oracle/.bash_profile
rm -f /tmp/oracle_env_tmp.txt
}
function kernel_config() { #内核优化
#内核参数配置 kernel.shmmax = 1073741824 为本机物理内存(2G)的一半,单位为byte。
print_color "green" "修改内核参数配置文件"
cat /tmp/kernel_config_tmp.txt >> /etc/sysctl.conf && sysctl -p -q
tail -20 /tmp/kernel_config_tmp.txt
}
#Decompression oracle file解压
function oracle_file() {
print_color "green" "解压oracle安装文件/tmp/linux.x64_11gR2_database_*of2.zip"
unzip -qa linux.x64_11gR2_database_1of2.zip && echo "解压成功!!!"
if [ "$?" -ne 0 ]; then echo "解压失败, 程序退出!"; exit 1; fi
unzip -qa linux.x64_11gR2_database_2of2.zip && echo "解压成功!!!"
if [ "$?" -ne 0 ]; then echo "解压失败, 程序退出!"; exit 1; fi
mkdir /home/oracle/response
chown -R oracle:oinstall /tmp/database
chown -R oracle:oinstall /home/oracle/response
#get install config file
cd /home/oracle/response
\cp -a /tmp/database/response/* /home/oracle/response/
if [ "$?" -ne 0 ]; then echo "执行结果: exit $?, 程序退出!"; exit 1; fi
}
function db_install_rsp() { #替换安装文件参数/home/oracle/response/db_install.rsp
print_color "green" "替换文件参数:/home/oracle/response/db_install.rsp"
while read line
do
OLD_IFS="$IFS"
IFS="="
arr_cut_01=($line)
OLD_IFS="$IFS"
IFS="$OLD_IFS"
sed -n "s%^${arr_cut_01[0]}.*=\$%$line%p" /home/oracle/response/db_install.rsp
sed -i "s%^${arr_cut_01[0]}.*=\$%$line%" /home/oracle/response/db_install.rsp
done < /tmp/oracle_install_rsp.txt
}
function dbca_rsp() { #替换建库文件参数/home/oracle/response/dbca.rsp
print_color "green" "替换文件参数:/home/oracle/response/dbca.rsp"
while read line
do
OLD_IFS="$IFS"
IFS="="
arr_cut_02=($line)
IFS="$OLD_IFS"
OLD_IFS="$IFS"
sed -n "s%^${arr_cut_02[0]} = .*%$line%p" /home/oracle/response/dbca.rsp
sed -n "s%^#${arr_cut_02[0]}[ ]*=.*%$line%p" /home/oracle/response/dbca.rsp
sed -i "s%^${arr_cut_02[0]} = .*%$line%" /home/oracle/response/dbca.rsp
sed -i "s%^#${arr_cut_02[0]}[ ]*=.*%$line%" /home/oracle/response/dbca.rsp
done < /tmp/oracle_dbca_rsp.txt
}
#start install oracle software and start listen
function install_oracle() {
print_color "green" "开始安装oracle"
#start install oracle
oracle_out='/tmp/oracle.out'
su - oracle -c "/tmp/database/runInstaller -force -silent -noconfig \
-responseFile /home/oracle/response/db_install.rsp -ignorePrereq" 1> ${oracle_out}
echo -e "\033[34mInstallNotice >>\033[0m \033[32m Oracle Install Starting \033[05m...\033[0m"
while true; do
grep '[FATAL] [INS-10101]' ${oracle_out} &> /dev/null #抓取错误关键字
if [[ $? == 0 ]];then #抓取成功则 安装失败
print_color "red" "Oracle start install has [ERROR]!!!"
cat ${oracle_out}
exit
fi
cat /tmp/oracle.out | grep oot.sh #抓取安装完成关键字
if [[ $? == 0 ]];then
sleep 3
#/data/soft/oracle/inventory/orainstRoot.sh
orainstRoot_sh=`cat /tmp/oracle.out | grep oot.sh | awk '{print $0}' | head -1`
print_color "yellow" "开始执行:${orainstRoot_sh}"
${orainstRoot_sh} #运行sh脚本
if [[ $? == 0 ]]; then
print_color "green" "Script 1 run ok"
else
print_color "red" "Script 1 run faild"
exit 1 #错误则退出
fi
sleep 3
# /data/soft/oracle/product/11.2.0/root.sh
root_sh=`cat /tmp/oracle.out | grep oot.sh | awk '{print $0}' | tail -1` #运行sh脚本
print_color "yellow" "开始执行:${root_sh}"
${root_sh}
if [[ $? == 0 ]];then
print_color "green" "Script 2 run ok"
else
print_color "red" "Script 2 run faild"
exit 1 #错误则退出
fi
#start listen
print_color "green" "以静默方式配置监听..."
#以静默方式配置监听
su - oracle -c "netca /silent /responsefile /home/oracle/response/netca.rsp"
if [ "$?" -ne 0 ]; then echo "执行结果: exit $?, 程序退出!"; exit 1; fi
netstat -anptu | grep 1521
if [[ $? == 0 ]]; then
print_color "green" "Oracle run listen"
break
else
print_color "red" "Oracle no run listen"
exit
fi
fi
sleep 3
done
}
#以静默方式建立新库install oracle single instance
function CreateDatabase() {
print_color "green" "Start CreateDatabase..." #密码已经在文件中定义
su - oracle -c "dbca -silent -createDatabase -responseFile /home/oracle/response/dbca.rsp || echo 'dbca执行失败,请检查dbca.rsp与安装日志'"
su - oracle -c "ps -ef | grep ora_ | grep -v grep"
su - oracle -c "lsnrctl status"
}
function main() {
kernel_config && \
j_para && \
install_package && \
base_config && \
oracle_file && \
db_install_rsp && \
dbca_rsp && \
install_oracle && \
CreateDatabase && \
print_color "green" "Oracle 11g 安装完成!" || print_color "red" "Oracle 11g 安装失败!"
exit 0
}
#run script
main
注:(仅供学习参考)本脚本经过本人多次测试成功,但由于oracle 11g安装复杂,需要定义各种参数、变量,小小错误就会导致安装失败。请各位在测试机上测试完成后,方可用于正式环境。(QQ:755136755)