Shell脚本静默安装数据库Oracle 12c (Centos6)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Shell脚本静默安装数据库Oracle 12c (Centos6)相关的知识,希望对你有一定的参考价值。
刚入职不久,主管就叫我用shell脚本部署Oracle数据库,一开始懵的一批,手动安装也搞了很久,皇天不负有心人,哈哈!!!搞了几天终于搞好了,也搞了份脚本安装oracle。(对于经常搭建Oracle数据库,重复步骤做得很烦的盆友可以来看看)
Oracle安装脚本:
[[email protected] ~]# cat oracle_install.sh
#!/bin/bash #install oracle #CentOS release 6.9 #关闭selinux sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config setenforce 0 #关闭iptables service iptables stop chkconfig iptables off #修改/etc/hosts,hostname hostname oracle echo "NETWORKING=yes" > /etc/sysconfig/network echo "HOSTNAME=oracle" >>/etc/sysconfig/network ip=`ifconfig | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[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 "$ip oracle" >>/etc/hosts #安装依赖包 yum -y install update expect openssh-clients binutils compat-libstdc++ compat-libstdc++-33 elfutils-libelf-devel gcc gcc-c++ glibc-devel glibc-headers ksh libaio-devel libstdc++-devel make sysstat unixODBC-devel binutils-* compat-libstdc++* elfutils-libelf* glibc* gcc-* libaio* libgcc* libstdc++* make* sysstat* unixODBC* wget unzip java-1.8.0-openjdk* vim lrzsz wget unzip #创建用户和组 testgroup1=oinstall testgroup2=dba testgroup3=oper testuser=oracle egrep "$testgroup1" /etc/group >/dev/null if [ $? -eq 0 ];then echo "this $testgroup1 group is exits" else groupadd $testgroup1 fi egrep "$testgroup2" /etc/group >/dev/null if [ $? -eq 0 ];then echo "this $testgroup2 group is exits" else groupadd $testgroup2 fi egrep "$testgroup3" /etc/group >/dev/null if [ $? -eq 0 ];then echo "this $testgroup3 group is exits" else groupadd $testgroup3 fi egrep "$testuser" /etc/passwd >/dev/null if [ $? -eq 0 ];then echo "this $testuser user is exits" 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 -R oracle:oinstall /opt/oracle chmod -R 775 /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 = 250 32000 100 128" >>/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 = 9000 65500">>/etc/sysctl.conf sysctl -p >>/dev/null #修改用户限制文件 echo "oracle soft nofile 1024" >>/etc/security/limits.conf echo "oracle hard nofile 65536" >>/etc/security/limits.conf echo "oracle soft nproc 2047" >>/etc/security/limits.conf echo "oracle hard nproc 16384" >>/etc/security/limits.conf echo "oracle soft stack 10240" >>/etc/security/limits.conf echo "oracle hard stack 32768" >>/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 #设置环境变量 # For root user echo "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">>/etc/profile source /etc/profile # For Oracle user echo "export ORACLE_BASE=/opt/oracle" >>/home/oracle/.bash_profile echo "export ORACLE_HOME=/opt/oracle/12c" >>/home/oracle/.bash_profile echo "export ORACLE_SID=orcl" >>/home/oracle/.bash_profile echo "export PATH=\$PATH:\$HOME/bin:\$ORACLE_HOME/bin" >>/home/oracle/.bash_profile echo "export LD_LIBRARY_PATH=\$ORACLE_HOME/lib:/usr/lib" >>/home/oracle/.bash_profile echo "export PATH=\$PATH:\$ORACLE_HOME/bin" >>/home/oracle/.bash_profile echo "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">>/home/oracle/.bash_profile source /home/oracle/.bash_profile #!!!上传文件到/opt/oracle目录或者直接下载,(文件过大,建议上传),我这里是从其他机器copy过来的。 /usr/bin/expect<<-EOF set timeout 100 spawn scp [email protected]:/opt/oracle/linuxamd64_12102_database_* /opt/oracle expect { "*yes/no" { send "yes\r"; exp_continue } "*password:" { send "00000000\r" } } expect eof EOF su - oracle <<EOF cd /opt/oracle; #wget http://download.oracle.com/otn/linux/oracle12c/121020/linuxamd64_12102_database_1of2.zip; #wget http://download.oracle.com/otn/linux/oracle12c/121020/linuxamd64_12102_database_2of2.zip; unzip linuxamd64_12102_database_1of2.zip; unzip linuxamd64_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 "parameter update succeeful!" else echo "$install parameter don't update!" fi if [ "$hostname" = "ORACLE_HOSTNAME=" ] then sed -i "s/ORACLE_HOSTNAME=/ORACLE_HOSTNAME=oracle/g" /opt/oracle/database/response/db_install.rsp echo "parameter update succeeful!" else echo "$hostname parameter don't update!" 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 "parameter update succeeful!" else echo "$group_name parameter don't update!" 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 "parameter update succeeful!" else echo "$inventory parameter don't update!" 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 "parameter update succeeful!" else echo "$languages parameter don't update!" 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 "parameter update succeeful!" else echo "$oracle_home parameter don't update!" 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 "parameter update succeeful!" else echo "$oracle_base parameter don't update!" 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 "parameter update succeeful!" else echo "$InstallEdition parameter don't update!" 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 "parameter update succeeful!" else echo "$dba_group parameter don't update!" 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 "parameter update succeeful!" else echo "$oper_group parameter don't update!" 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 "parameter update succeeful!" else echo "$updates parameter don't update!" fi #安装db_install.rsp su - oracle <<EOF cd /opt/oracle/database; ./runInstaller -silent -responseFile /opt/oracle/database/response/db_install.rsp -ignorePrereq; EOF sleep 500 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/lsnrctl start\" ">>/etc/rc.d/rc.local echo "su - oracle -c \"/opt/oracle/12c/bin/dbstart\" ">>/etc/rc.d/rc.local touch /var/lock/subsys/oracle chmod 755 /etc/init.d/oracle chkconfig oracle on #echo "export PATH=\$PATH:\$ORACLE_HOME/bin" >>/home/oracle/.bash_profile #source /home/oracle/.bash_profile #查看状态和进程 netstat -tulnp |grep 1521 ps -ef |grep ora_ |grep -v grep su - oracle << EOF cd /opt/oracle/12c/bin/; ./lsnrctl status EOF if [ $? -eq 0 ];then #oracle安装完成 echo "oracle installed succeeful!" fi
用service接替oracle的启动程序:
[[email protected] ~]# cat /etc/init.d/oracle
#!/bin/bash # oracle: Start/Stop Oracle Database 11g R2 # chkconfig: 345 90 10 # description: The Oracle Database is an Object-Relational Database Management System. # . /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 $0 already running. exit 1 fi echo -n $"Starting Oracle Database:" su - $ORACLE_USER -c "$ORACLE_HOME/bin/lsnrctl start" su - $ORACLE_USER -c "$ORACLE_HOME/bin/dbstart $ORACLE_HOME" #su - $ORACLE_USER -c "$ORACLE_HOME/bin/emctl start dbconsole" touch $LOCKFILE ;; 'stop') if [ ! -f $LOCKFILE ]; then echo $0 already stopping. exit 1 fi echo -n $"Stopping Oracle Database:" su - $ORACLE_USER -c "$ORACLE_HOME/bin/lsnrctl stop" su - $ORACLE_USER -c "$ORACLE_HOME/bin/dbshut" #su - $ORACLE_USER -c "$ORACLE_HOME/bin/emctl stop dbconsole" rm -f $LOCKFILE ;; 'restart') $0 stop $0 start ;; 'status') if [ -f $LOCKFILE ]; then echo $0 started. else echo $0 stopped. fi ;; *) echo "Usage: $0 [start|stop|status]" exit 1 esac exit 0
直接执行安装脚本即可:
[[email protected] ~]# bash oracle_install.sh
以上是关于Shell脚本静默安装数据库Oracle 12c (Centos6)的主要内容,如果未能解决你的问题,请参考以下文章