由于linux下安装oracle比较麻烦,因此一直想将其简单化通过脚本方式一键安装,正好想简化自动化部署方式,因此用了半天时间写了个脚本并做了下验证,使用脚本安装非常简单,大概需要30分钟左右,只需要安装步骤执行即可。
备注:该脚本已经上传至资源,可以自行下载使用。
一、前提
操作系统:centos 6.5
oracle版本:oracle 11g, (安装包:linux.x64_11gR2_database_1of2.zip、linux.x64_11gR2_database_2of2.zip)
二、使用说明
1、脚本说明:
(1)oracle_11g_install.sh:oracle安装脚本
(2)oracle:设置oracle的启动程序为service的启动文件
2、操作步骤
(1)手动通过工具将需要安装包的linux.x64_11gR2_database_1of2.zip、linux.x64_11gR2_database_2of2.zip 上传到服务器/opt/oracle目录下
(2)将安装脚本 oracle_11g_install.sh 放到/home下
chmod 755 oracle_11g_install.sh
bash oracle_11g_install.sh
即可完成oracle11g的自动安装
(3)设置oracle的启动程序为service
将启动脚本oracle上传到 /etc/init.d/ 目录下
chmod a+x /etc/init.d/oracle
chkconfig --add oracle
3、注意事项
(1)尽量是比较干净的环境
(2)如果过程安装失败,可手动删除创建的文件夹,然后继续执行安装脚本即可
三、具体脚本介绍
1、oracle11g安装脚本(命名为:oracle_11g_install.sh),如下所示:
#!/bin/bash
#The script is writed by lcm
#centos6.5上静默安装oracle11g
#1、【关闭selinux】
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
#2、【关闭防火墙 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
yum -y install binutils compat-libstdc++-33 compat-libstdc++-33.i686 elfutils-libelf elfutils-libelf-devel gcc gcc-c++ glibc glibc.i686 glibc-common glibc-devel glibc-devel.i686 glibc-headers ksh libaio libaio.i686 libaio-devel libaio-devel.i686 libgcc libgcc.i686 libstdc++ libstdc++.i686 libstdc++-devel make sysstat unixODBC unixODBC-devel
#创建用户和组
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/11g //存在问题
#$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/11g" >>/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目录或者直接下载,(文件过大,建议上传),我这里是手动通过工具将其他机器上的linux.x64_11gR2_database_1of2.zip、linux.x64_11gR2_database_2of2.zip 上传到/opt/oracle目录下
#解压oracle 11g ,待解压完毕后会生成文件夹/opt/oracle/database,里面是解压后的所有oracle安装文件
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 linux.x64_11gR2_database_1of2.zip
unzip linux.x64_11gR2_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\/11g/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/11g/root.sh
#安装netca.rsp
su - oracle <<EOF
cd /opt/oracle/11g/bin/;
./netca /silent /responseFile /opt/oracle/database/response/netca.rsp;
EOF
#添加数据库实例,修改dbca.rsp文件
sed -i 's/GDBNAME = "orcl11g.us.oracle.com"/GDBNAME = "orcl"/g' /opt/oracle/database/response/dbca.rsp
sed -i 's/SID = "orcl11g"/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 = "AL32UTF8"/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 = "AL32UTF8"" >>/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/11g/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/11g/bin/dbstart
sed -i 's/ORACLE_HOME_LISTNER=$1/ORACLE_HOME_LISTNER=$ORACLE_HOME/g' /opt/oracle/11g/bin/dbshut
#配置service开机启动
sed -i 's/orcl:\/opt\/oracle\/11g\:N/orcl:\/opt\/oracle\/11g\:Y/g' /etc/oratab
echo "su - oracle -c \"/opt/oracle/11g/bin/lsnrctl start\" ">>/etc/rc.d/rc.local
echo "su - oracle -c \"/opt/oracle/11g/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/11g/bin/;
./lsnrctl status
EOF
if [ $? -eq 0 ];then
#oracle安装完成
echo "oracle installed succeeful!"
fi
2、oracle的service启动程序文件(命名为: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/11g
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