基于centos7.x之上oracle 11g 静默安装-脚本

最近公司在使用oracle 11g 数据库,公司本地需要有oracle 11g环境,安装完后,整理了一下,感觉还是写个一键安装脚本吧,后面再需要的话,直接执行一个脚本完事。

脚本说明:

脚本目录结构如下图所示。

bin目录存放执行脚本:oracle_silent_install.sh.该脚本需要再root用户下执行。

file目录存放的配置文件,oracle安装(db_install.rsp)监听(netca.rap)建库(dbca)。脚本中没有使用netca.rap,二十直接修改ora目录下三个文件。

基于centos7.x之上oracle 11g 静默安装-脚本_第1张图片

资源下载

oracle安装介质下载:oracle官网

脚本下载地址:黑客世界

脚本下载 oracle_11g_install_01.tar.gz,解压后得到 oracle_11g_install目录,但是这里的 share 目录没有orace的安装介质,只需要将oracle官网下载的安装介质放到share目录即可。

脚本和执行效果

oracle 11g 静默安装的脚本oracle_silent_install.sh内容如下:

#!/bin/bash
#  -------------+-----------------------------
# * Filename    :       selinux.sh
# * Date        :       2020-06-15
# * Author      :       wangjianxiong
# * Description :       oracle silent install
#  -------------+-----------------------------
#
# bin  conf file  log  readme  share  tmp
#

os="" 
ip="192.168.64.137"
myhostname="oracle.daxiong.com"
myhostname_alias="oracle"

num=$RANDOM
mymem=`free | grep Mem | awk '{print $2}'`
myswap=`free | grep Swap | awk '{print $2}'`

Ora_Installation_Position=/u01/app
INSTALLATION_FAILURE=/tmp/oracle_installation_failure_exit_code
ORACLE_INSTALL_LOG=/tmp/oracle_install.log

STEP="1"

show_spinner()
{
    $* &
    PID=$!
    local delay=0.1
    local spinstr='|/-\'
    spin[0]="-"
    spin[1]="\\"
    spin[2]="|"
    spin[3]="/"
    sleep 0.05
    echo -n " ... "
    while kill -0 $PID 2>/dev/null
    do
        if [ -z $DEBUG ];then
            for i in "${spin[@]}"
                do
                    echo -ne "$i"
                    sleep 0.1
                    echo -ne "\b"
            done
        else
            sleep 0.1
        fi
    done

    if [ -f $INSTALLATION_FAILURE ]; then
        failure_reason=`cat $INSTALLATION_FAILURE`
        if [ -z $DEBUG ]; then
            echo -e "$(tput setaf 1)\nFAIL\n$(tput sgr0)"|tee -a $ORACLE_INSTALL_LOG
            echo -e "$(tput setaf 1)Reason: $failure_reason\n$(tput sgr0)"|tee -a $ORACLE_INSTALL_LOG
        else
            echo "FAIL"
            echo "Reason: $failure_reason"
        fi
        exit 1
    else
        if [ -z $DEBUG ]; then
            echo -e "$(tput setaf 2)PASS$(tput sgr0)"|tee -a $ORACLE_INSTALL_LOG
        else
            echo "PASS"
        fi
    fi
}

echo_title(){
    echo "\n================">> $ORACLE_INSTALL_LOG
    echo ""|tee -a $ORACLE_INSTALL_LOG
    echo -n " ${STEP}. $*:" |tee -a $ORACLE_INSTALL_LOG
    STEP=`expr $STEP + 1`
}

echo_subtitle(){
    echo "\n----------------" >> $ORACLE_INSTALL_LOG
    echo -n "        $*:"|tee -a $ORACLE_INSTALL_LOG
}

fail(){
    tput cub 6
    #echo -e "$(tput setaf 1) \nFAIL\n$(tput sgr0)"|tee -a $ORACLE_INSTALL_LOG
    echo -e "$(tput setaf 1) Reason: $*\n$(tput sgr0)"|tee -a $ORACLE_INSTALL_LOG
    echo "$*  \n\nThe detailed installation log could be found in $ORACLE_INSTALL_LOG " > $INSTALLATION_FAILURE
    exit 1
}

# Set the host name
set_hostname()
{
    echo_subtitle "设置主机名,修改/etc/hosts"
    sleep 1

    hostnamectl set-hostname $myhostname
cat >>/etc/hosts <<EOF
$ip $myhostname $myhostname_alias
EOF
}

# Set the swap size
# If the physical memory is 2GB or less, swap should be 1.5 times the physical memory.
# If there is between 2GB and 16GB of physical memory, then swap size should be equal to physical memory.
# If the physical memory is greater than 16GB, then 16GB is sufficient for SWAP.
set_swap()
{
    echo_subtitle "设置swap交互分区"
    sleep 1

    if [ $mymem -le 2048000 ];then
        let addswap=3072000-$myswap
    elif [ $mymem -gt 2048000 ] && [ $mymem -lt 16384000 ];then
        let addswap=$mymem-$myswap
    elif [ $mymem -gt 16384000 ];then
        let addswap=16384000-$myswap
    fi
    
    if [ $addswap -lt 40 ];then # Swap area needs to be at least 40 KiB
        echo "Swap area needs to be at least 40 KiB" >> $ORACLE_INSTALL_LOG 2>&1
        echo "Swap now size [ $myswap ], No need to expand." >> $ORACLE_INSTALL_LOG 2>&1
    else
        # Add a SWAP partition under /var to randomly generate a directory ending with the number beginning swap.
        dd if=/dev/zero of=/var/swap_$num bs=1024 count=$addswap >> $ORACLE_INSTALL_LOG 2>&1
        mkswap /var/swap_$num >> $ORACLE_INSTALL_LOG 2>&1
        swapon /var/swap_$num >> $ORACLE_INSTALL_LOG 2>&1
        if [ $? -eq 0 ];then
            echo "/var/swap_$num          swap                    swap    defaults        0 0" >> /etc/fstab
        else
            fail "swap设置失败,请查看日志文件 : $ORACLE_INSTALL_LOG"
        fi
        # Add all swap partitions, or free.
        ret=`cat /proc/swaps | grep ^/ | awk '{print $3}' |  awk '{sum+=$1}END{print sum}'`
        echo "Swap is set to $ret" >> $ORACLE_INSTALL_LOG 2>&1
    fi
}

# check sys
check_sys()
{
    echo_subtitle "检查系统"
    sleep 1

    # 2>&1 >/dev/null : Standard output enters /dev/null and error output is printed to the screen.
    # >/dev/null 2>&1 : Both standard output and error output enter /dev/null.
    rpm -q centos-release >/dev/null 2>&1
    if [ $? -eq 0 ];then
        v=`cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'`
        if [ $v -eq 6 ];then
            os="centos6"
            fail "这个系统不是 Centos7."
        elif [ $v -eq 7 ];then
            os="centos7"
        fi
    else
        fail "这个系统不是 Centos."
    fi
}

# set selinux=disabled
set_selinux()
{
    echo_subtitle "设置selinux=disabled"
    sleep 1

    setenforce 0
    sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
    if [ $? -eq 0 ];then
        echo "Current set seliunx=permissive, restart system after selinux=disabled." >> $ORACLE_INSTALL_LOG 2>&1
    else
        fail "设置 selinux 失败."
    fi
}

# Checking the Software Requirements
# Oracle's website : https://docs.oracle.com/cd/E11882_01/install.112/e24326/toc.htm#CIHFICFD
# The following or later version of packages for Oracle Linux 4 and Red Hat Enterprise Linux 4 must be installed:
# Remove the version number and use yum to install.

must_pkg()
{
    echo_subtitle "安装oracle所需依赖包"

yum -y install binutils \
compat-libcap1 \
compat-libstdc++-33 \
compat-libstdc++-33*i686 \
gcc \
gcc-c++ \
glibc \
glibc*.i686 \
glibc-devel \
glibc-devel*.i686 \
ksh \
libaio \
libaio*.i686 \
libaio-devel \
libaio-devel*.i686 \
libgcc \
libgcc*.i686 \
libstdc++ \
libstdc++*.i686 \
libstdc++-devel \
libstdc++-devel*.i686 \
libXi \
libXi*.i686 \
libXtst \
libXtst*.i686 \
make \
sysstat \
unixODBC \
unixODBC*.i686 \
unixODBC-devel \
unixODBC-devel*.i686 >> $ORACLE_INSTALL_LOG 2>&1
if [ $? -ne 0 ];then
    fail "安装oracle所需依赖失败."
fi
}

create_orauser()
{
    echo_subtitle "创建oracle用户"

    # Create oInstall and DBA groups
    egrep "^oinstall" /etc/group >& /dev/null
    if [ $? -ne 0 ];then
        /usr/sbin/groupadd oinstall
    fi
    egrep "^dba" /etc/group >& /dev/null
    if [ $? -ne 0 ];then
        /usr/sbin/groupadd dba
    fi

    # Creating an Oracle user
    egrep "^oracle" /etc/passwd >& /dev/null
    if [ $? -ne 0 ];then
        /usr/sbin/useradd -g oinstall -G dba oracle
        if [ $? -ne 0 ];then
            fail "创建oracle用户失败"
        fi
    fi    

    echo oracle | passwd --stdin oracle >& /dev/null
    if [ $? -eq 0 ];then
        echo "oracle用户的密码: oracle"  >> $ORACLE_INSTALL_LOG 2>&1
    else
        fail "设置oracle用户密码失败"
    fi
}

# Configuring Kernel Parameters and Resource Limits
# Oracle's website : https://docs.oracle.com/cd/E11882_01/install.112/e24326/toc.htm#BHCCADGD

conf_kernel()
{
    echo_subtitle "设置内核参数"
    sleep 1

cat >>/etc/sysctl.conf <<EOF
fs.aio-max-nr = 1048576
fs.file-max = 6815744
kernel.shmall = 2097152
kernel.shmmax = 536870912
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

/sbin/sysctl -p >> $ORACLE_INSTALL_LOG 2>&1
}

set_limit()
{
    echo_subtitle "修改用户限制"
    sleep 1

cat >>/etc/security/limits.conf <<EOF
oracle soft nproc 2047
oracle hard nproc 16384
oracle soft nofile 1024
oracle hard nofile 65536
oracle soft stack 10240
oracle hard stack 10240
EOF

cat >>/etc/pam.d/login <<EOF
session required /lib64/security/pam_limits.so
session required pam_limits.so
EOF

cat >>/etc/profile <<EOF
if [ \$USER = "oracle" ]; then
   if [ \$SHELL = "/bin/ksh" ]; then
       ulimit -p 16384
       ulimit -n 65536
    else
       ulimit -u 16384 -n 65536
   fi
fi
EOF

source /etc/profile
}

# Create installation directory.
create_dir()
{
    echo_subtitle "创建安装目录"

    [ ! -d $Ora_Installation_Position ] && mkdir -p $Ora_Installation_Position
    [ ! -d $Ora_Installation_Position/oracle/product/11.2.0 ] && mkdir -p $Ora_Installation_Position/oracle/product/11.2.0    
    [ ! -d $Ora_Installation_Position/oracle/oradata ] && mkdir -p $Ora_Installation_Position/oracle/oradata
    [ ! -d $Ora_Installation_Position/oracle/oraInventory ] && mkdir -p $Ora_Installation_Position/oracle/oraInventory
    [ ! -d $Ora_Installation_Position/oracle/fast_recovery_area ] && mkdir -p $Ora_Installation_Position/oracle/fast_recovery_area  

    chown -R oracle:oinstall $Ora_Installation_Position
    chmod -R 775 $Ora_Installation_Position

cat >/etc/oraInst.loc <<EOF
inventory_loc=$Ora_Installation_Position/oracle/oraInventory
inst_group=oinstall
EOF

    chown oracle:oinstall /etc/oraInst.loc
    chmod 664 /etc/oraInst.loc
}

# Configure The Oracle environment variables.
conf_ora_env()
{
    echo_subtitle "设置oracle用户环境变量"

cat >>/home/oracle/.bash_profile <<EOF
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1
export TNS_ADMIN=\$ORACLE_HOME/network/admin
export PATH=.:\${PATH}:\$HOME/bin:\$ORACLE_HOME/bin
export PATH=\${PATH}:/usr/bin:/bin:/usr/local/bin
export LD_LIBRARY_PATH=\${LD_LIBRARY_PATH}:\$ORACLE_HOME/lib
export LD_LIBRARY_PATH=\${LD_LIBRARY_PATH}:\$ORACLE_HOME/oracm/lib
export LD_LIBRARY_PATH=\${LD_LIBRARY_PATH}:/lib:/usr/lib:/usr/local/lib
export CLASSPATH=\${CLASSPATH}:\$ORACLE_HOME/jlib
export CLASSPATH=\${CLASSPATH}:\$ORACLE_HOME/rdbms/jlib
export CLASSPATH=\${CLASSPATH}:\$ORACLE_HOME/network/jlib
export LIBPATH=\${CLASSPATH}:\$ORACLE_HOME/lib:\$ORACLE_HOME/ctx/lib
export ORACLE_OWNER=oracle
export SPFILE_PATH=\$ORACLE_HOME/dbs
export ORA_NLS10=\$ORACLE_HOME/nls/data
export ORACLE_SID=orcl
export NLS_LANG=AMERICAN_AMERICA.AL32UTF8
EOF

su - oracle -c "source /home/oracle/.bash_profile"
}

unzip_oracle()
{
    echo_subtitle "解压安装介质"

    which unzip >> $ORACLE_INSTALL_LOG 2>&1
    if [ $? -ne 0 ];then
        yum -y install zip unzip >> $ORACLE_INSTALL_LOG 2>&1
    fi

    unzip ../share/linux.x64_11gR2_database_1of2.zip -d /tmp >> $ORACLE_INSTALL_LOG 2>&1
    unzip ../share/linux.x64_11gR2_database_2of2.zip -d /tmp >> $ORACLE_INSTALL_LOG 2>&1
}

set_etc()
{
    echo_subtitle "设置响应文件"
    sleep 1    

    cp -r ../file/etc /home/oracle
    chown -R oracle:oinstall /home/oracle/etc
    chmod -R 755 /home/oracle/etc
}

Initialize_db()
{
    echo_subtitle "初始化Oracle数据库"

    su - oracle -c "/tmp/database/runInstaller -silent -force -responseFile /home/oracle/etc/db_install.rsp -ignorePrereq" >> $ORACLE_INSTALL_LOG 2>&1

while true
do
    ret=`sed -n '$p' /tmp/oracle_install.log`
    if [ "$ret" == "Successfully Setup Software." ];then
        /u01/app/oracle/product/11.2.0/db_1/root.sh >> $ORACLE_INSTALL_LOG 2>&1
        break
    fi
done
}

oracle_netca()
{
    echo_subtitle "配置监听"

    sed -i "s/oracle.daxiong.com/$myhostname/" ../file/ora/listener.ora
    sed -i "s/oracle.daxiong.com/$myhostname/" ../file/ora/tnsnames.ora
    cat ../file/ora/listener.ora > /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora
    cat ../file/ora/tnsnames.ora > /u01/app/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora
    cat ../file/ora/sqlnet.ora > /u01/app/oracle/product/11.2.0/db_1/network/admin/sqlnet.ora
    su - oracle -c "/u01/app/oracle/product/11.2.0/db_1/bin/lsnrctl start" >> $ORACLE_INSTALL_LOG 2>&1
    if [ $? -ne 0 ];then
        fail "oracle 配置监听失败"
    fi
}

oracle_dbca()
{
    echo_subtitle "创建数据库"

    su - oracle -c "/u01/app/oracle/product/11.2.0/db_1/bin/dbca -silent -responseFile /home/oracle/etc/dbca.rsp" >> $ORACLE_INSTALL_LOG 2>&1
    if [ $? -ne 0 ];then
        fail "创建数据库失败"
    fi
}

clear
echo -e
echo "======================================="
echo "Oracle 11g silent installation"
echo "======================================="

echo_title "检查系统"
echo -e
show_spinner check_sys

echo_title "基本系统设置"
echo -e
show_spinner set_hostname
show_spinner set_swap
show_spinner set_selinux

echo_title "安装依赖包"
echo -e
show_spinner must_pkg

echo_title "创建oracle用户"
echo -e
show_spinner create_orauser

echo_title "配置内核参数和资源限制"
echo -e
show_spinner conf_kernel
show_spinner set_limit

echo_title "创建安装目录"
echo -e
show_spinner create_dir

echo_title "解压安装介质"
echo -e
show_spinner unzip_oracle
show_spinner conf_ora_env

echo_title "开始安装oracle"
echo -e
show_spinner set_etc
show_spinner Initialize_db

echo_title "配置监听创建数据库"
echo -e
show_spinner oracle_netca
show_spinner oracle_dbca
View Code

效果截图:

基于centos7.x之上oracle 11g 静默安装-脚本_第2张图片

你可能感兴趣的:(基于centos7.x之上oracle 11g 静默安装-脚本)