一个系统运维的打怪升级之路

Oracle 11gR2一键安装脚本(测试版)

2019-04-30
163次查阅
2019/4/30

接上一篇《CentOS7系统静默安装和配置Oracle 11gR2》写了一个Oracle安装脚本,一共分为三个文件:

  1. init.sh(初始化系统)
  2. install.sh(安装)
  3. config(配置文件)

init.sh 脚本

#!/bin/bash
# create by sunkai at 2019/4/30
# Oracle安装脚本测试版 v0.1
# 适用于CentOS7

. config

######################################
# 系统初始化
######################################

if [ $check_init -eq 1 ]; then
    echo 'init.sh 已经执行'
    exit 1
fi

echo_info()
{
    echo_yellow $1
    echo_yellow '----------------------------------------------------------------------'
    $2
    echo_yellow '----------------------------------------------------------------------'
    echo
}

# 更新系统、安装依赖
mv -f /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
wget http://mirrors.aliyun.com/repo/Centos-7.repo -O /etc/yum.repos.d/CentOS-Base.repo
yum clean all
yum makecache
yum update -y
yum install -y unzip lrzsz 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

# 创建用户组
groupadd oinstall
groupadd dba
useradd -g oinstall -G dba oracle
echo_info '创建用户组...完成' 'id oracle'

# 修改主机名
hostnamectl set-hostname $host_name
sed -i "s/127.0.0.1.*$/& ${host_name}/" /etc/hosts
sed -i "s/::1.*$/& ${host_name}/" /etc/hosts
echo_info '修改主机名...完成' 'cat /etc/hosts'

# 创建oracle数据库安装目录并解压数据包
mkdir -p $ora_location
mkdir $oracle_install_dir
mkdir $inventory_dir
unzip linux.x64_11gR2_database_1of2.zip -d $ora_location
unzip linux.x64_11gR2_database_2of2.zip -d $ora_location
chown -R oracle:oinstall $ora_location
echo_info '修改主机名...完成' "du -sh $ora_location/*"

# 修改OS系统标识
sed -i 's/^CentOS Linux release.*/#&/' /etc/redhat-release
sed -i '1a\redhat-7' /etc/redhat-release
echo_info '修改OS系统标识' 'cat /etc/redhat-release'

# 关闭防火墙、SELinux
systemctl disable firewalld.service
systemctl stop firewalld.service
echo_info '关闭防火墙...完成' 'systemctl status firewalld.service'

sed -i 's/^SELINUX=.*$/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
echo_info '关闭SELinux...完成' 'getenforce'

# 修改内核参数
cat >> /etc/sysctl.conf << EOF
# Oracle
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.conf.all.rp_filter = 1
fs.file-max = 6815744
fs.aio-max-nr = 1048576
kernel.shmall = 2097152
kernel.shmmax = 2147483648
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
echo_info '/etc/sysctl.conf 文件修改...完成' '/sbin/sysctl -p'

# 对oracle用户设置限制,提高软件运行性能
cat >> /etc/security/limits.conf << EOF
# Oracle
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
echo_info '/etc/security/limits.conf 文件修改...完成' 'tail -n 7 /etc/security/limits.conf'

# 修改系统登陆配置文件
echo 'session    required     /lib64/security/pam_limits.so' >> /etc/pam.d/login
echo 'session    required     pam_limits.so' >> /etc/pam.d/login
echo_info '/etc/pam.d/login文件修改...完成' 'tail -n 2 /etc/pam.d/login'

cat >> /etc/csh.login << EOF
# Oracle
if ( \$USER == "oracle" ) then
    limit maxproc 16384
    limit descriptors 65536
    umask 022
endif
EOF
echo_info '/etc/csh.login 文件修改...完成' 'tail -n 5 /etc/csh.login'

cat >> /etc/profile << EOF
# Oracle
if [ \$USER = "oracle" ]; then
    if [ \$SHELL = "/bin/ksh" ]; then
        ulimit -p 16384
        ulimit -n 65536
    else
        ulimit -u 16384 -n 65536
    fi
    umask 022
fi
EOF
echo_info '/etc/profile 文件修改...完成' 'tail -n 9 /etc/profile'
source /etc/profile

# 配置oracle用户环境变量
cat >> /home/oracle/.bash_profile << EOF
# Oracle
export ORACLE_HOSTNAME=$host_name
export ORACLE_BASE=$oracle_install_dir
export ORACLE_HOME=\$ORACLE_BASE/product/11.2.0
export ORACLE_SID=$ora_sid
export LANG=en_US.UTF-8
export NLS_LANG=AMERICAN_AMERICA.AL32UTF8
export NLS_DATE_FORMAT="YYYY-MM-DD HH24:MI:SS"
export PATH=\$ORACLE_HOME/bin:\$PATH
EOF
echo_info '/home/oracle/.bash_profile' 'tail -n 8 /home/oracle/.bash_profile'
source /home/oracle/.bash_profile

# 写入检测文件
sed -i "s/check_init=0/check_init=1/g" config
echo '系统初始化完成,需要重启系统后执行 install.sh'
exit 0

install.sh 脚本

#!/bin/bash
# create by sunkai at 2019/4/30
# Oracle安装脚本测试版 v0.1
# 适用于CentOS7
# 配置文件可以修改下面的关联数组
#
#
#set -x

. config

######################################
# Oracle 11gR2 安装
######################################

if [ $check_init -eq 0 ]; then
    echo 'init.sh 没有执行'
    exit 1
fi

# db_install.rsp 配置文件修改参数 ######################################################
declare -A db_install_conf
db_install_conf['oracle.install.option']="INSTALL_DB_SWONLY"
db_install_conf['ORACLE_HOSTNAME']="$host_name"
db_install_conf['UNIX_GROUP_NAME']="oinstall"
db_install_conf['INVENTORY_LOCATION']="$inventory_dir"
db_install_conf['SELECTED_LANGUAGES']="en,zh_CN"
db_install_conf['ORACLE_HOME']="$oracle_install_dir/product/11.2.0"
db_install_conf['ORACLE_BASE']="$oracle_install_dir"
db_install_conf['oracle.install.db.InstallEdition']="EE"
db_install_conf['oracle.install.db.isCustomInstall']="true"
db_install_conf['oracle.install.db.DBA_GROUP']="dba"
db_install_conf['oracle.install.db.OPER_GROUP']="dba"
db_install_conf['oracle.install.db.config.starterdb.type']="GENERAL_PURPOSE"
db_install_conf['oracle.install.db.config.starterdb.globalDBName']="$global_db_name"
db_install_conf['oracle.install.db.config.starterdb.SID']="$ora_sid"
db_install_conf['oracle.install.db.config.starterdb.characterSet']="$character_set"
db_install_conf['oracle.install.db.config.starterdb.memoryLimit']="$memory_limit"
db_install_conf['oracle.install.db.config.starterdb.password.ALL']="oracle"
db_install_conf['oracle.install.db.config.starterdb.control']="DB_CONTROL"
db_install_conf['DECLINE_SECURITY_UPDATES']="true"
########################################################################################

# dbca.rsp 配置文件修改参数 ############################################################
declare -A dbca_conf
dbca_conf['GDBNAME']="$global_db_name"
dbca_conf['SID']="$ora_sid"
dbca_conf['SYSPASSWORD']="$sys_pwd"
dbca_conf['SYSTEMPASSWORD']="$sys_pwd"
dbca_conf['CHARACTERSET']="$character_set"
dbca_conf['NATIONALCHARACTERSET']="UTF8"
dbca_conf['MEMORYPERCENTAGE']="$memory_percentage"
dbca_conf['EMCONFIGURATION']="LOCAL"
########################################################################################

# 精准等待命令执行完毕
precise_waiting()
{
    $1
    while ps -ef | grep "$1" | grep -v 'grep'; do
       sleep 1
       continue
    done
}

# Oracle软件安装
db_install()
{
    # db_install.rsp 配置文件路径
    db_install_rsp="$oracle_file_dir/response/db_install.rsp"

    for i in ${!db_install_conf[@]}; do
        sed -i "s#^$i=.*#$i=${db_install_conf[$i]}#g" $db_install_rsp
        if [$? -eq 0]; then
            echo_green "$i=${db_install_conf[$i]} [Change Success]"
        else
            echo_red "$i [Change Faild]"
            exit 1
        fi
    done

    su - oracle -c "$oracle_file_dir/runInstaller -silent -ignorePrereq -responseFile $db_install_rsp"
    while ps -ef | grep "\-silent \-ignorePrereq \-responseFile $db_install_rsp" | grep -v 'grep' > /dev/null; do
       sleep 1
       continue
    done
    
    if [ ! -e $oracle_install_dir/product/11.2.0/bin/dbstart ]; then
        echo_red "$oracle_install_dir/product/11.2.0/bin/dbstart Not Found, Oracle Installation Failed!"
        exit 1
    fi
    sed -i 's/^ORACLE_HOME_LISTNER=.*$/ORACLE_HOME_LISTNER=$ORACLE_HOME/g' $oracle_install_dir/product/11.2.0/bin/dbstart

    #precise_waiting $oracle_install_dir/inventory/orainstRoot.sh
    precise_waiting $oracle_install_dir/product/11.2.0/root.sh
    sed -i "s#$ora_sid:$oracle_install_dir/product/11.2.0:.*#$ora_sid:$oracle_install_dir/product/11.2.0:Y#" /etc/oratab
}

# 安装监听器
netca_install()
{
    # netca.rsp 配置文件路径
    netca_rsp="$oracle_file_dir/response/netca.rsp"
    su - oracle -c "$oracle_install_dir/product/11.2.0/bin/netca /silent /responsefile $netca_rsp"
    while ps -ef | grep "/silent /responsefile $netca_rsp" | grep -v 'grep' > /dev/null; do
       sleep 1
       continue
    done
}

# 创建数据库实例
dbca_install()
{
    # dbca.rsp 配置文件路径
    dbca_rsp="$oracle_file_dir/response/dbca.rsp"

    for i in ${!dbca_conf[@]}; do
        sed -i "s#\(^$i\|^\#$i\).*#$i = "${dbca_conf[$i]}"#g" $dbca_rsp
        if [$? -eq 0]; then
            echo_green "$i = \"${dbca_conf[$i]}\" [Change Success]"
        else
            echo_red "$i [Change Faild]"
            exit 1
        fi
    done

    su - oracle -c "$oracle_install_dir/product/11.2.0/bin/dbca -silent -responseFile $dbca_rsp"
    while ps -ef | grep "\-silent \-responseFile $dbca_rsp" | grep -v 'grep' > /dev/null; do
       sleep 1
       continue
    done
}

oracle_init()
{
cat >> /etc/init.d/oracle << EOF
#! /bin/sh
# chkconfig: 2345 55 25
# description: Oracle 11g AutoRun Services
# /etc/init.d/oracle


### BEGIN INIT INFO
# Provides:          oracle
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Oracle Management script
# Description:       Run-level Startup script. for the Oracle Instance, Listener, and \
#                    Web Interface
### END INIT INFO

case $1 in

    start)
        su - oracle -c "$oracle_install_dir/product/11.2.0/bin/dbstart start"
    ;;

    stop)
        su - oracle -c "$oracle_install_dir/product/11.2.0/bin/dbshut"
    ;;

    restart)
        if ps -ef | grep -E "ora_.*_$ora_sid" | grep -v grep; then
            su - oracle -c "$oracle_install_dir/product/11.2.0/bin/dbshut"
            sleep 1
            su - oracle -c "$oracle_install_dir/product/11.2.0/bin/dbstart start"
        else
            echo 'Oracle is not running...'
        fi
    ;;

    *)
        echo "Usage: $0 (start|stop|restart)"
        exit 1
    ;;
esac
EOF
chmod +x /etc/init.d/oracle
}

#su - oracle -c "$oracle_install_dir/product/11.2.0/bin/lsnrctl start"
#su - oracle -c "$oracle_install_dir/product/11.2.0/bin/dbstart start"

#操作选项
case $1 in

    db)
        db_install
    ;;

    netca)
        netca_install
    ;;

    dbca)
        dbca_install
        oracle_init
    ;;

    all)
        db_install
        netca_install
        dbca_install
        oracle_init
    ;;
    
    *)
        echo_yellow "使用方法:$0 (db|netca|dbca|all)"
        echo "db:安装Oracle软件"
        echo "netca:安装监听器"
        echo "dbca:安装数据库实例"
        echo "all:安装以上全部"
    ;;
esac

config 配置文件

# 主机名
host_name='oracle-db'

# 创建一个关于Oracle的路径
ora_location='/data/ora'

# Oracle安装目录
oracle_install_dir="$ora_location/oracle"

# Oracle文件解压目录
oracle_file_dir="$ora_location/database"

# 产品清单路径
inventory_dir="$ora_location/inventory"

# 数据库标识ID
ora_sid='SKDB'

# 全局数据库名称
global_db_name='orcl'

# 数据库字符集
character_set='AL32UTF8'

# Oracle占用物理内存百分比
memory_percentage='40'

# 超级用户密码
sys_pwd='password'

# 指定数据库的总内存分配
memory_limit='1024'

#用于脚本检测
check_init=0

# 输出颜色
echo_red()
{
    echo -e "\e[1;31m${1}\e[0m"
}

echo_green()
{
    echo -e "\e[1;32m${1}\e[0m"
}

echo_yellow()
{
    echo -e "\e[1;33m${1}\e[0m"
}

评论

想说点什么?