刚入职不久,主管就叫我用shell脚本部署Oracle数据库,一开始懵的一批,手动安装也搞了很久,皇天不负有心人,哈哈!!!搞了几天终于搞好了,也搞了份脚本安装oracle。(对于经常搭建Oracle数据库,重复步骤做得很烦的盆友可以来看看)
Oracle安装脚本:
[root@oracle ~]# 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 <>/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的启动程序:
[root@oracle ~]# 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
直接执行安装脚本即可:
[root@oracle ~]# bash oracle_install.sh