Shell脚本静默安装数据库Oracle 12c (Centos6)

前端之家收集整理的这篇文章主要介绍了Shell脚本静默安装数据库Oracle 12c (Centos6)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


刚入职不久,主管就叫我用shell脚本部署Oracle数据库,一开始懵的一批,手动安装也搞了很久,皇天不负有心人,哈哈!!!搞了几天终于搞好了,也搞了份脚本安装oracle。(对于经常搭建Oracle数据库,重复步骤做得很烦的盆友可以来看看)


Oracle安装脚本:

[root@oracle ~]# cat oracle_install.sh

#!/bin/bash
#installoracle
#CentOSrelease6.9

#关闭selinux
sed-i's/SELINUX=enforcing/SELINUX=disabled/g'/etc/selinux/config
setenforce0

#关闭iptables
serviceiptablesstop
chkconfigiptablesoff

#修改/etc/hosts,hostname
hostnameoracle
echo"NETWORKING=yes">/etc/sysconfig/network
echo"HOSTNAME=oracle">>/etc/sysconfig/network
ip=`ifconfig|grep-o'[0-9]\{1,3\}\.[0-9]\{1,3\}'|awk'NR==1{print$0}'`
#ip=`ifconfig|sed-n'/192.168/p'|awk'{print$1$2}'|awk-F:'{print$2}'`
echo"$iporacle">>/etc/hosts

#安装依赖包
yum-yinstallupdateexpectopenssh-clientsbinutilscompat-libstdc++compat-libstdc++-33elfutils-libelf-develgccgcc-c++glibc-develglibc-headerskshlibaio-devellibstdc++-develmakesysstatunixODBC-develbinutils-*compat-libstdc++*elfutils-libelf*glibc*gcc-*libaio*libgcc*libstdc++*make*sysstat*unixODBC*wgetunzipjava-1.8.0-openjdk*vimlrzszwgetunzip

#创建用户和组
testgroup1=oinstall
testgroup2=dba
testgroup3=oper
testuser=oracle
egrep"$testgroup1"/etc/group>/dev/null
if[$?-eq0];then
echo"this$testgroup1groupisexits"
else
groupadd$testgroup1
fi
egrep"$testgroup2"/etc/group>/dev/null
if[$?-eq0];then
echo"this$testgroup2groupisexits"
else
groupadd$testgroup2
fi
egrep"$testgroup3"/etc/group>/dev/null
if[$?-eq0];then
echo"this$testgroup3groupisexits"
else
groupadd$testgroup3
fi
egrep"$testuser"/etc/passwd>/dev/null
if[$?-eq0];then
echo"this$testuseruserisexits"
else
useradd-g$testgroup1-G$testgroup2,$testgroup3$testuser
echo"oracle"|passwd--stdin$testuser
fi

#创建软件安装目录,并赋权限
mkdir-p/opt/oracle
#$ORACLE_BASE
mkdir-p/opt/oracle/12c
#$ORACLE_HOME
mkdir/opt/oracle/oradata
#数据存放目录
mkdir/opt/oracle/inventory
#清单目录
mkdir/opt/oracle/flash_recovery_area
#数据恢复目录
chown-Roracle:oinstall/opt/oracle
chmod-R775/opt/oracle

#添加系统参数
sed-i's/^fs.file-max/#&/g'/etc/sysctl.conf
sed-i's/^kernel.sem/#&/g'/etc/sysctl.conf
sed-i's/^kernel.shmmni/#&/g'/etc/sysctl.conf
sed-i's/^kernel.shmall/#&/g'/etc/sysctl.conf
sed-i's/^kernel.shmmax/#&/g'/etc/sysctl.conf
sed-i's/^net.core.rmem_default/#&/g'/etc/sysctl.conf
sed-i's/^net.core.rmem_max/#&/g'/etc/sysctl.conf
sed-i's/^net.core.wmem_default/#&/g'/etc/sysctl.conf
sed-i's/^net.core.wmem_max/#&/g'/etc/sysctl.conf
sed-i's/^fs.aio-max-nr/#&/g'/etc/sysctl.conf
sed-i's/^net.ipv4.ip_local_port_range/#&/g'/etc/sysctl.conf
echo"fs.file-max=6815744">>/etc/sysctl.conf
echo"kernel.sem=25032000100128">>/etc/sysctl.conf
echo"kernel.shmmni=4096">>/etc/sysctl.conf
echo"kernel.shmall=1073741824">>/etc/sysctl.conf
echo"kernel.shmmax=4398046511104">>/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=1048576">>/etc/sysctl.conf
echo"fs.aio-max-nr=1048576">>/etc/sysctl.conf
echo"net.ipv4.ip_local_port_range=900065500">>/etc/sysctl.conf
sysctl-p>>/dev/null

#修改用户限制文件
echo"oraclesoftnofile1024">>/etc/security/limits.conf
echo"oraclehardnofile65536">>/etc/security/limits.conf
echo"oraclesoftnproc2047">>/etc/security/limits.conf
echo"oraclehardnproc16384">>/etc/security/limits.conf
echo"oraclesoftstack10240">>/etc/security/limits.conf
echo"oraclehardstack32768">>/etc/security/limits.conf

#关联设置
echo"sessionrequired/lib64/security/pam_limits.so">>/etc/pam.d/login
echo"sessionrequiredpam_limits.so">>/etc/pam.d/login

#设置环境变量
#Forrootuser
echo"if[\$USER="oracle"];then
if[\$SHELL="/bin/ksh"];then
ulimit-p16384
ulimit-n65536
else
ulimit-u16384-n65536
fi
umask022
fi">>/etc/profile
source/etc/profile

#ForOracleuser
echo"exportORACLE_BASE=/opt/oracle">>/home/oracle/.bash_profile
echo"exportORACLE_HOME=/opt/oracle/12c">>/home/oracle/.bash_profile
echo"exportORACLE_SID=orcl">>/home/oracle/.bash_profile
echo"exportPATH=\$PATH:\$HOME/bin:\$ORACLE_HOME/bin">>/home/oracle/.bash_profile
echo"exportLD_LIBRARY_PATH=\$ORACLE_HOME/lib:/usr/lib">>/home/oracle/.bash_profile
echo"exportPATH=\$PATH:\$ORACLE_HOME/bin">>/home/oracle/.bash_profile
echo"if[\$USER="oracle"];then
if[\$SHELL="/bin/ksh"];then
ulimit-p16384
ulimit-n65536
else
ulimit-u16384-n65536
fi
umask022
fi">>/home/oracle/.bash_profile
source/home/oracle/.bash_profile

#!!!上传文件到/opt/oracle目录或者直接下载,(文件过大,建议上传),我这里是从其他机器copy过来的。
/usr/bin/expect<<-EOF
settimeout100
spawnscproot@192.168.135.108:/opt/oracle/linuxamd64_12102_database_*/opt/oracle
expect{
"*yes/no"{send"yes\r";exp_continue}
"*password:"{send"00000000\r"}
}
expecteof
EOF

su-oracle<<EOF
cd/opt/oracle;
#wgethttp://download.oracle.com/otn/linux/oracle12c/121020/linuxamd64_12102_database_1of2.zip;
#wgethttp://download.oracle.com/otn/linux/oracle12c/121020/linuxamd64_12102_database_2of2.zip;
unziplinuxamd64_12102_database_1of2.zip;
unziplinuxamd64_12102_database_2of2.zip;
exit;
EOF

#!!修改db_install.rsp文件
install=`sed-n'/oracle.install.option/p'/opt/oracle/database/response/db_install.rsp`
hostname=`sed-n'/ORACLE_HOSTNAME/p'/opt/oracle/database/response/db_install.rsp`
group_name=`sed-n'/UNIX_GROUP_NAME/p'/opt/oracle/database/response/db_install.rsp`
inventory=`sed-n'/INVENTORY_LOCATION/p'/opt/oracle/database/response/db_install.rsp`
languages=`sed-n'/^SELECTED_LANGUAGES=en$/p'/opt/oracle/database/response/db_install.rsp`
oracle_home=`sed-n'/ORACLE_HOME/p'/opt/oracle/database/response/db_install.rsp`
oracle_base=`sed-n'/ORACLE_BASE/p'/opt/oracle/database/response/db_install.rsp`
InstallEdition=`sed-n'/oracle.install.db.InstallEdition/p'/opt/oracle/database/response/db_install.rsp`
dba_group=`sed-n'/oracle.install.db.DBA_GROUP/p'/opt/oracle/database/response/db_install.rsp`
oper_group=`sed-n'/oracle.install.db.OPER_GROUP/p'/opt/oracle/database/response/db_install.rsp`
updates=`sed-n'/^DECLINE_SECURITY_UPDATES=$/p'/opt/oracle/database/response/db_install.rsp`
sed-i's/oracle.install.db.BACKUPDBA_GROUP=/oracle.install.db.BACKUPDBA_GROUP=dba/g'/opt/oracle/database/response/db_install.rsp
sed-i's/oracle.install.db.DGDBA_GROUP=/oracle.install.db.DGDBA_GROUP=dba/g'/opt/oracle/database/response/db_install.rsp
sed-i's/oracle.install.db.KMDBA_GROUP=/oracle.install.db.KMDBA_GROUP=dba/g'/opt/oracle/database/response/db_install.rsp
sed-i's/oracle.install.db.config.starterdb.globalDBName=/oracle.install.db.config.starterdb.globalDBName=orcl/g'/opt/oracle/database/response/db_install.rsp
sed-i's/oracle.install.db.config.starterdb.SID=/oracle.install.db.config.starterdb.SID=orcl/g'/opt/oracle/database/response/db_install.rsp
sed-i's/oracle.install.db.config.starterdb.type=/oracle.install.db.config.starterdb.type=GENERAL_PURPOSE/g'/opt/oracle/database/response/db_install.rsp
sed-i's/oracle.install.db.config.starterdb.password.ALL=/oracle.install.db.config.starterdb.password.ALL=oracle/g'/opt/oracle/database/response/db_install.rsp
sed-i's/oracle.install.db.config.starterdb.memoryLimit=/oracle.install.db.config.starterdb.memoryLimit=81920/g'/opt/oracle/database/response/db_install.rsp
if["$install"="oracle.install.option="]
then
sed-i"s/oracle.install.option=/oracle.install.option=INSTALL_DB_SWONLY/g"/opt/oracle/database/response/db_install.rsp
echo"parameterupdatesucceeful!"
else
echo"$installparameterdon'tupdate!"
fi
if["$hostname"="ORACLE_HOSTNAME="]
then
sed-i"s/ORACLE_HOSTNAME=/ORACLE_HOSTNAME=oracle/g"/opt/oracle/database/response/db_install.rsp
echo"parameterupdatesucceeful!"
else
echo"$hostnameparameterdon'tupdate!"
fi

if["$group_name"="UNIX_GROUP_NAME="]
then
sed-i"s/UNIX_GROUP_NAME=/UNIX_GROUP_NAME=oinstall/g"/opt/oracle/database/response/db_install.rsp
echo"parameterupdatesucceeful!"
else
echo"$group_nameparameterdon'tupdate!"
fi

if["$inventory"="INVENTORY_LOCATION="]
then
sed-i"s/INVENTORY_LOCATION=/INVENTORY_LOCATION=\/opt\/oracle\/inventory/g"/opt/oracle/database/response/db_install.rsp
echo"parameterupdatesucceeful!"
else
echo"$inventoryparameterdon'tupdate!"
fi


if["$languages"="SELECTED_LANGUAGES=en"]
then
sed-i"s/SELECTED_LANGUAGES=en/SELECTED_LANGUAGES=en,zh_CN/g"/opt/oracle/database/response/db_install.rsp
echo"parameterupdatesucceeful!"
else
echo"$languagesparameterdon'tupdate!"
fi

if["$oracle_home"="ORACLE_HOME="]
then
sed-i"s/ORACLE_HOME=/ORACLE_HOME=\/opt\/oracle\/12c/g"/opt/oracle/database/response/db_install.rsp
echo"parameterupdatesucceeful!"
else
echo"$oracle_homeparameterdon'tupdate!"
fi


if["$oracle_base"="ORACLE_BASE="]
then
sed-i"s/ORACLE_BASE=/ORACLE_BASE=\/opt\/oracle/g"/opt/oracle/database/response/db_install.rsp
echo"parameterupdatesucceeful!"
else
echo"$oracle_baseparameterdon'tupdate!"
fi


if["$InstallEdition"="oracle.install.db.InstallEdition="]
then
sed-i"s/oracle.install.db.InstallEdition=/oracle.install.db.InstallEdition=EE/g"/opt/oracle/database/response/db_install.rsp
echo"parameterupdatesucceeful!"
else
echo"$InstallEditionparameterdon'tupdate!"
fi


if["$dba_group"="oracle.install.db.DBA_GROUP="]
then
sed-i"s/oracle.install.db.DBA_GROUP=/oracle.install.db.DBA_GROUP=dba/g"/opt/oracle/database/response/db_install.rsp
echo"parameterupdatesucceeful!"
else
echo"$dba_groupparameterdon'tupdate!"
fi


if["$oper_group"="oracle.install.db.OPER_GROUP="]
then
sed-i"s/oracle.install.db.OPER_GROUP=/oracle.install.db.OPER_GROUP=oper/g"/opt/oracle/database/response/db_install.rsp
echo"parameterupdatesucceeful!"
else
echo"$oper_groupparameterdon'tupdate!"
fi


if["$updates"="DECLINE_SECURITY_UPDATES="]
then
sed-i"s/DECLINE_SECURITY_UPDATES=/DECLINE_SECURITY_UPDATES=true/g"/opt/oracle/database/response/db_install.rsp
echo"parameterupdatesucceeful!"
else
echo"$updatesparameterdon'tupdate!"
fi

#安装db_install.rsp
su-oracle<<EOF
cd/opt/oracle/database;
./runInstaller-silent-responseFile/opt/oracle/database/response/db_install.rsp-ignorePrereq;
EOF
sleep500
sh/opt/oracle/inventory/orainstRoot.sh
sh/opt/oracle/12c/root.sh

#安装netca.rsp
su-oracle<<EOF
cd/opt/oracle/12c/bin/;
./netca/silent/responseFile/opt/oracle/database/response/netca.rsp;
EOF

#添加数据库实例,修改dbca.rsp文件
sed-i's/GDBNAME="orcl12c.us.oracle.com"/GDBNAME="orcl"/g'/opt/oracle/database/response/dbca.rsp
sed-i's/SID="orcl12c"/SID="orcl"/g'/opt/oracle/database/response/dbca.rsp
sed-i's/#DATAFILEDESTINATION=/DATAFILEDESTINATION=/g'/opt/oracle/database/response/dbca.rsp
sed-i's/DATAFILEDESTINATION=/DATAFILEDESTINATION=\/opt\/oracle\/oradata/g'/opt/oracle/database/response/dbca.rsp
sed-i's/#RECOVERYAREADESTINATION=/RECOVERYAREADESTINATION=/g'/opt/oracle/database/response/dbca.rsp
sed-i's/RECOVERYAREADESTINATION=/RECOVERYAREADESTINATION=\/opt\/oracle\/flash_recovery_area/g'/opt/oracle/database/response/dbca.rsp
sed-i's/#CHARACTERSET="US7ASCII"/CHARACTERSET="WE8MSWIN1252"/g'/opt/oracle/database/response/dbca.rsp
sed-i's/#SYSPASSWORD="password"/SYSPASSWORD="oracle"/g'/opt/oracle/database/response/dbca.rsp
sed-i's/#SYSTEMPASSWORD="password"/SYSTEMPASSWORD="oracle"/g'/opt/oracle/database/response/dbca.rsp
sed-i's/#SYSDBAPASSWORD="password"/SYSDBAPASSWORD="oracle"/g'/opt/oracle/database/response/dbca.rsp
sed-i's/#EMEXPRESSPORT=""/EMEXPRESSPORT=5500/g'/opt/oracle/database/response/dbca.rsp
sed-i's/#TOTALMEMORY="800"/TOTALMEMORY="3096"/g'/opt/oracle/database/response/dbca.rsp
#echo"CHARACTERSET="WE8MSWIN1252"">>/opt/oracle/database/response/dbca.rsp
#echo"SYSPASSWORD="oracle"">>/opt/oracle/database/response/dbca.rsp
#echo"SYSTEMPASSWORD="oracle"">>/opt/oracle/database/response/dbca.rsp
#echo"DBSNMPPASSWORD="oracle"">>/opt/oracle/database/response/dbca.rsp

#执行数据库实例安装
su-oracle<<EOF
cd/opt/oracle/12c/bin/;
./dbca-silent-responseFile/opt/oracle/database/response/dbca.rsp;
oracle
oracle
EOF

#设置开机启动
sed-i's/ORACLE_HOME_LISTNER=$1/ORACLE_HOME_LISTNER=$ORACLE_HOME/g'/opt/oracle/12c/bin/dbstart
sed-i's/ORACLE_HOME_LISTNER=$1/ORACLE_HOME_LISTNER=$ORACLE_HOME/g'/opt/oracle/12c/bin/dbshut

#配置service开机启动
sed-i's/orcl:\/opt\/oracle\/12c\:N/orcl:\/opt\/oracle\/12c\:Y/g'/etc/oratab
echo"su-oracle-c\"/opt/oracle/12c/bin/lsnrctlstart\"">>/etc/rc.d/rc.local
echo"su-oracle-c\"/opt/oracle/12c/bin/dbstart\"">>/etc/rc.d/rc.local
touch/var/lock/subsys/oracle
chmod755/etc/init.d/oracle
chkconfigoracleon
#echo"exportPATH=\$PATH:\$ORACLE_HOME/bin">>/home/oracle/.bash_profile
#source/home/oracle/.bash_profile

#查看状态和进程
netstat-tulnp|grep1521
ps-ef|grepora_|grep-vgrep
su-oracle<<EOF
cd/opt/oracle/12c/bin/;
./lsnrctlstatus
EOF
if[$?-eq0];then
#oracle安装完成
echo"oracleinstalledsucceeful!"
fi


用service接替oracle的启动程序:

[root@oracle ~]# cat /etc/init.d/oracle

#!/bin/bash
#oracle:Start/StopOracleDatabase11gR2
#chkconfig:3459010
#description:TheOracleDatabaseisanObject-RelationalDatabaseManagementSystem.
#
./etc/rc.d/init.d/functions
LOCKFILE=/var/lock/subsys/oracle
ORACLE_HOME=/opt/oracle/12c
ORACLE_USER=oracle
case"$1"in
'start')
if[-f$LOCKFILE];then
echo$0alreadyrunning.
exit1
fi
echo-n$"StartingOracleDatabase:"
su-$ORACLE_USER-c"$ORACLE_HOME/bin/lsnrctlstart"
su-$ORACLE_USER-c"$ORACLE_HOME/bin/dbstart$ORACLE_HOME"
#su-$ORACLE_USER-c"$ORACLE_HOME/bin/emctlstartdbconsole"
touch$LOCKFILE
;;
'stop')
if[!-f$LOCKFILE];then
echo$0alreadystopping.
exit1
fi
echo-n$"StoppingOracleDatabase:"
su-$ORACLE_USER-c"$ORACLE_HOME/bin/lsnrctlstop"
su-$ORACLE_USER-c"$ORACLE_HOME/bin/dbshut"
#su-$ORACLE_USER-c"$ORACLE_HOME/bin/emctlstopdbconsole"
rm-f$LOCKFILE
;;
'restart')
$0stop
$0start
;;
'status')
if[-f$LOCKFILE];then
echo$0started.
else
echo$0stopped.
fi
;;
*)
echo"Usage:$0[start|stop|status]"
exit1
esac
exit0


直接执行安装脚本即可:

[root@oracle ~]# bash oracle_install.sh

猜你在找的Bash相关文章