使用shell脚本安装Oracle 11g

脚本安装简单说明
READ ME:
	本安装脚本是在指定目录下进行安装的,指定目录为 /data 目录。首先需要将安装包上传到 /data 目录下。
	测试时本文是将一个20G的磁盘挂载到 data 目录下,目录名称等一些路径未用变量代替.
	如直接使用本脚本则需要目录结构一致即可
	/data/oracle/app
	/data/oracle/app/oradata
	setup为主目录,安装文件即解压到这个目录下
	/data/oracle/app/setup
附件:Oracle 11g安装包及pdksh安装包,链接永久有效
	链接:https://pan.baidu.com/s/1bk3sNrIVxZFeAj9qRuxE_Q 
	提取码:1234
shell脚本如下,如有改动欢迎指正,谢谢。
#!/bin/bash
#Explain:Oracle install
#Time:2021-08-16
#Version:1
#安装前将Oracle安装包上传到 /data 目录下,安装时一般会遇到 pdksh 这个包自动安装不上,也可提前手动安装好。
#注意 /data 目录下可提前挂载一块磁盘供Oracle数据库使用,因Oracle数据库占用空间较大,此处要注意

#安装解压软件
yum install unzip -y
#自定义主机名,依据个人方便,也可以不定义
hostnamectl set-hostname "oracle_install"
HOSTNAME=`hostname`
HOSTIP=`ifconfig | grep "inet " | head -1 | awk '{print $2}'`

#将主机IP地址和主机名写入文件中
echo "$HOSTIP $HOSTNAME" >>/etc/hosts

#修改selinux,禁用生效
sed -i.bak 's/enforcing/disabled/' /etc/selinux/config
setenforce 0

#关闭防火墙(个人测试直接关闭,如不能关闭则在进行测试)
systemctl stop firewalld
systemctl disable firewalld

#错误日志文件
rpmErrprLog=/root/rpmErrprLog.log

#安装依赖
yum -y install binutils \
compat-libstdc++-33 \
elfutils-libelf \
elfutils-libelf-devel \
expat \
gcc \
gcc-c++ \
glibc \
glibc-common \
glibc-devel \
glibc-headers \
libaio \
libaio-devel \
libgcc \
libstdc++ \
libstdc++-devel \
make \
pdksh \
sysstat \
unixODBC \
unixODBC-devel

#检查依赖是否安装完成(检查中有中文是防止安装版本为中文)
erroeNUM=`rpm -q \
binutils \
compat-libstdc++-33 \
elfutils-libelf \
elfutils-libelf-devel \
expat \
gcc \
gcc-c++ \
glibc \
glibc-common \
glibc-devel \
glibc-headers \
libaio \
libaio-devel \
libgcc \
libstdc++ \
libstdc++-devel \
make \
pdksh \
sysstat \
unixODBC \
unixODBC-devel | grep "not install|未安装软件包" | wc -l`

if [ $erroeNUM -gt 0 ];then
    rpm -q \
    binutils \
    compat-libstdc++-33 \
    elfutils-libelf \
    elfutils-libelf-devel \
    expat \
    gcc \
    gcc-c++ \
    glibc \
    glibc-common \
    glibc-devel \
    glibc-headers \
    libaio \
    libaio-devel \
    libgcc \
    libstdc++ \
    libstdc++-devel \
    make \
    pdksh \
    sysstat \
    unixODBC \
    unixODBC-devel &>$rpmErrprLog
    echo "Dependency installation failed,Please contact the administrator check $rpmErrprLog"
    exit 0
fi

#创建用户组
groupInstallNum=`cat /ect/group | grep -w oinstall | wc -l`
if [ $groupInstallNum -ne 1 ];then
    groupadd oinstall
fi
groupDbaNum=`cat /etc/group | grep -w dba | wc -l`
if [ $groupDbaNum -ne 1 ];then
    groupadd dba
fi
#创建用户并修改密码
userOracleNum=`cat /etc/passwd | grep -w oracle | wc -l`
if [ $userOracleNum -ne 1 ];then
    useradd -d /data/oracle -g oinstall -G dba oracle
    echo oracle:oracle|chpasswd
    [ $? -ne 0 ] && echo "user add is error,Please contact the administrator"
fi

#修改文件内容
#kernel.shmmax大小为实际内存的一半,单位是Byte
kernelSmmax=$(cat /proc/meminfo | grep -w MemTotal | awk '{print $2*1024/2}')
#备份文件,出错后方便修改
cp /etc/sysctl.conf /etc/sysctl.conf.bak
echo "fs.aio-max-nr=1048576" >>/etc/sysctl.conf
echo "fs.file-max=6815744" >>/etc/sysctl.conf
echo "kernel.shmall=2097152" >>/etc/sysctl.conf
echo "kernel.shmmni=4096" >>/etc/sysctl.conf
echo "kernel.shmmax = $kernelSmmax" >>/etc/sysctl.conf
echo "kernel.sem=250 32000 100 128" >>/etc/sysctl.conf
echo "net.ipv4.ip_local_port_range=9000 65535" >>/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=1048586" >>/etc/sysctl.conf
#使参数生效
sysctl -p

cp /etc/security/limits.conf /etc/security/limits.conf.bak
echo "oracle soft nproc 2047" >>/etc/security/limits.conf
echo "oracle hard nproc 16384" >>/etc/security/limits.conf
echo "oracle soft nofile 1024" >>/etc/security/limits.conf
echo "oracle hard nofile 65536" >>/etc/security/limits.conf


cp /etc/pam.d/login /etc/pam.d/login.bak
echo "session required /lib64/security/pam_limits.so" >>/etc/pam.d/login
echo "session required pam_limits.so" >>/etc/pam.d/login

cp /etc/profile /etc/profile.bak
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

#Oracle用户修改文件内容(这里注意单引号的使用,测试过程中发现使用双引号时写入的内容会有错误,如哪位朋友能看到这个,可以在评论去留言指点一下)
su - oracle -c "echo umask 022 >>.bash_profile"
su - oracle -c "echo export ORACLE_HOSTNAME=$HOSTNAME >>.bash_profile"
su - oracle -c "echo export ORACLE_BASE=/data/oracle >>.bash_profile"
su - oracle -c 'echo export ORACLE_HOME=\$ORACLE_BASE/product/11.2.0/ >>.bash_profile'
su - oracle -c "echo export ORACLE_SID=orcl >>.bash_profile"
su - oracle -c 'echo export PATH=.:\$ORACLE_HOME/bin:\$ORACLE_HOME/OPatch:\$ORACLE_HOME/jdk/bin:\$PATH >>.bash_profile'
su - oracle -c 'echo export LC_ALL=\"en_US\" >>.bash_profile'
su - oracle -c 'echo export LANG=\"en_US\" >>.bash_profile'
su - oracle -c 'echo export NLS_LANG=\"AMERICAN_AMERICA.ZHS16GBK\" >>.bash_profile'
su - oracle -c 'echo export NLS_DATE_FORMAT=\"YYYY-MM-DD HH24:MI:SS\" >>.bash_profile'
su - oracle -c "source .bash_profile"

#创建Oracle目录
mkdir /data/oracle/app
mkdir /data/oracle/app/oradata
mkdir /data/oracle/app/setup
chown -R oracle:oinstall /data/oracle/app

#解压Oracle压缩包到指定目录(这里是将Oracle的安装文件提前上传到指定的 /data 目录下 )
cd /data
unzip -q linux.x64_11gR2_database_1of2.zip -d /data/oracle/app/setup
unzip -q linux.x64_11gR2_database_2of2.zip -d /data/oracle/app/setup
[ $? -ne 0 ] && echo "unzip is error" && exit 1
#日志文件目录
dbLogFile="/data"
#修改配置文件内容
db_installFile="/data/oracle/app/setup/database/response/db_install.rsp"
sed -i "s/oracle.install.option=/oracle.install.option=INSTALL_DB_SWONLY/" $db_installFile
sed -i "s/ORACLE_HOSTNAME=/ORACLE_HOSTNAME=$HOSTNAME/" $db_installFile
sed -i "s/UNIX_GROUP_NAME=/UNIX_GROUP_NAME=oinstall/" $db_installFile
sed -i "s/INVENTORY_LOCATION=/INVENTORY_LOCATION=\/data\/oracle\/oraInventory/" $db_installFile
sed -i "s/SELECTED_LANGUAGES=/SELECTED_LANGUAGES=en,zh_CN/" $db_installFile
sed -i "s/ORACLE_HOME=/ORACLE_HOME=\/data\/oracle\/product\/11.2.0/" $db_installFile
sed -i "s/ORACLE_BASE=/ORACLE_BASE=\/data\/oracle/" $db_installFile
sed -i "s/oracle.install.db.InstallEdition=/oracle.install.db.InstallEdition=EE/" $db_installFile
sed -i "s/oracle.install.db.isCustomInstall.*$/oracle.install.db.isCustomInstall=false/" $db_installFile
sed -i "s/oracle.install.db.DBA_GROUP=/oracle.install.db.DBA_GROUP=dba/" $db_installFile
sed -i "s/oracle.install.db.OPER_GROUP=/oracle.install.db.OPER_GROUP=oinstall/" $db_installFile
sed -i "s/oracle.install.db.config.starterdb.type=/oracle.install.db.config.starterdb.type=GENERAL_PURPOSE/" $db_installFile
sed -i "s/oracle.install.db.config.starterdb.globalDBName=/oracle.install.db.config.starterdb.globalDBName=orcl/" $db_installFile
sed -i "s/oracle.install.db.config.starterdb.SID=/oracle.install.db.config.starterdb.SID=orcl/" $db_installFile
sed -i "s/oracle.install.db.config.starterdb.installExampleSchemas.*$/oracle.install.db.config.starterdb.installExampleSchemas=true/" $db_installFile
sed -i "s/oracle.install.db.config.starterdb.password.ALL=/oracle.install.db.config.starterdb.password.ALL=oracle/" $db_installFile
sed -i "s/^SECURITY_UPDATES_VIA_MYORACLESUPPORT=/SECURITY_UPDATES_VIA_MYORACLESUPPORT=false/" $db_installFile
sed -i "s/^DECLINE_SECURITY_UPDATES=/DECLINE_SECURITY_UPDATES=true/" $db_installFile

#安装Oracle
su - oracle -c "/data/oracle/app/setup/database/runInstaller -silent -ignorePrereq -responseFile /data/oracle/app/setup/database/response/db_install.rsp" &>$dbLogFile/dbInstallFile.log
if [ $? -eq 0 ];then
    echo $(date)
    #这里睡眠5分钟是执行安装过程,不然执行下面的shell语句会报错
    sleep 300
    sh /data/oracle/oraInventory/orainstRoot.sh
    sh /data/oracle/product/11.2.0/root.sh
    
    #配置监听
    su - oracle -c "netca /silent /responsefile /data/oracle/app/setup/database/response/netca.rsp" &>$dbLogFile/netca.log
    [ $? -ne 0 ] && echo "the oracle port is erroe" && exit 1
    
    #静默创建数据库(TOTALMEMORY 为实际内存的80%)
    MemTotal=$(cat /proc/meminfo  | grep -w MemTotal | awk '{print $2/1024*0.8}' | cut -d. -f1)
    dbcaFile="/data/oracle/app/setup/database/response/dbca.rsp"
    sed -i "0,/^GDBNAME = \"orcl11.us.oracle.com\"$/{s/^GDBNAME = \"orcl11.us.oracle.com\"$/GDBNAME = \"orcl\"/}" $dbcaFile
    sed -i "s/SID = \"orcl11g\"/SID = \"orcl\"/" $dbcaFile
    sed -i "s/#SYSPASSWORD = \"password\"/SYSPASSWORD = \"oracle\"/" $dbcaFile
    sed -i "s/#SYSTEMPASSWORD = \"password\"/SYSTEMPASSWORD = \"oracle\"/" $dbcaFile
    sed -i "0,/#SYSMANPASSWORD = \"password\"/{s/#SYSMANPASSWORD = \"password\"/SYSMANPASSWORD = \"oracle\"/}" $dbcaFile
    sed -i "0,/#DBSNMPPASSWORD = \"password\"/{s/#DBSNMPPASSWORD = \"password\"/DBSNMPPASSWORD = \"oracle\"/}" $dbcaFile
    sed -i "s/#DATAFILEDESTINATION =/DATAFILEDESTINATION =\/data\/oracle\/app\/oradata/" $dbcaFile
    sed -i "s/#RECOVERYAREADESTINATION=/RECOVERYAREADESTINATION=\/data\/oracle\/fast_recovery_area/" $dbcaFile
    sed -i "s/#CHARACTERSET = \"US7ASCII\"/CHARACTERSET = \"AL32UTF8\"/" $dbcaFile
    sed -i "s/#TOTALMEMORY = \"800\"/TOTALMEMORY = \"$MemTotal\"/" $dbcaFile
    #执行静默建库
    su - oracle -c "dbca -silent -responseFile /data/oracle/app/setup/database/response/dbca.rsp" &>$dbLogFile/dbcaFile.log
    echo $(date)
    #此处也可以不用 sleep 因为在执行静默安装时会首先安装完成然后在执行后面内容
    sleep 120
    if [ `ps -ef | grep ora_ | grep -v grep | wc -l` -gt 20 ]
        then
            echo "oracle 进程启动!"
    fi

else
    echo "db_install is error,Please contact the administrator"
    exit 1
fi