其实在以前的博客中我写过类似的日志,但是由于以前的域名过期了,所以日志也找不到了。
今天,想起来了,就记录一下,方便自己,也方便他人。
本文的背景说明:
RHEL 5 Update 10 + Oracle Database 11g standalone。
实现开机自动启动数据库实例的方法其实有很多种,下面会描述两个我可以想到的方法(也许有更多,欢迎留言跟帖,共同讨论,共同进步)。
一、DIY shell脚本实现。
这种方式实现的思路显而易见,手动编写一段shell脚本,在开机的时候调用就可以了。
shell脚本内容如下:
[root@dg1 ~]# ls /home/oracle | grep --color sh do_oracle.sh [root@dg1 ~]# [root@dg1 ~]# cat /home/oracle/do_oracle.sh #!/bin/bash # Intro echo "---> Do Oracle in Shell" echo "" # Variable s_instance_name=$1 s_action=$2 #echo "Instance is: $s_instance_name" #echo "Action is: $s_action" export ORACLE_SID=$s_instance_name if [ $s_action == "startup" ] then #echo "I will do it." lsnrctl start; sqlplus / as sysdba <<
你需要注意的是,我的脚本“do_oracle.sh”,是放置在OS系统用户oracle的家目录下的。
然后在开机脚本中引用上面的shell脚本:
[root@dg1 ~]# cat /etc/rc.local #!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local # Startup Oracle on boot su - oracle -c "sh do_oracle.sh sameol startup" [root@dg1 ~]#
这样,Linux开机就可以启动数据库实例了。
如果不放心,你可以手动执行一下脚本,测试一下,具体如下:
脚本关库:
[root@dg1 ~]# ps -ef | grep pmon oracle 22628 1 0 22:37 ? 00:00:00 ora_pmon_sameol root 22812 22406 0 22:45 pts/5 00:00:00 grep pmon [root@dg1 ~]# [root@dg1 ~]# ps -ef | grep lsnr oracle 22620 1 0 22:37 ? 00:00:00 /u01/app/oracle/product/11.2.0/dbhome_1/bin/tnslsnr LISTENER -inherit root 22814 22406 0 22:45 pts/5 00:00:00 grep lsnr [root@dg1 ~]# [root@dg1 ~]# su - oracle -c "sh do_oracle.sh sameol shutdown" ---> Do Oracle in Shell LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 16-JUN-2016 22:46:14 Copyright (c) 1991, 2013, Oracle. All rights reserved. Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1521))) The command completed successfully SQL*Plus: Release 11.2.0.4.0 Production on Thu Jun 16 22:46:14 2016 Copyright (c) 1982, 2013, Oracle. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options SQL> Database closed. Database dismounted. ORACLE instance shut down. SQL> Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options [root@dg1 ~]# [root@dg1 ~]# ps -ef | grep pmon root 22842 22406 0 22:46 pts/5 00:00:00 grep pmon [root@dg1 ~]# [root@dg1 ~]# ps -ef | grep lsnr root 22844 22406 0 22:46 pts/5 00:00:00 grep lsnr [root@dg1 ~]# [root@dg1 ~]#
脚本起库:
[root@dg1 ~]# ps -ef | grep pmon root 22842 22406 0 22:46 pts/5 00:00:00 grep pmon [root@dg1 ~]# [root@dg1 ~]# ps -ef | grep lsnr root 22844 22406 0 22:46 pts/5 00:00:00 grep lsnr [root@dg1 ~]# [root@dg1 ~]# [root@dg1 ~]# su - oracle -c "sh do_oracle.sh sameol startup" ---> Do Oracle in Shell LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 16-JUN-2016 22:47:06 Copyright (c) 1991, 2013, Oracle. All rights reserved. Starting /u01/app/oracle/product/11.2.0/dbhome_1/bin/tnslsnr: please wait... TNSLSNR for Linux: Version 11.2.0.4.0 - Production System parameter file is /u01/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora Log messages written to /u01/app/oracle/diag/tnslsnr/dg1/listener/alert/log.xml Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521))) Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=dg1)(PORT=1521))) Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1521))) STATUS of the LISTENER ------------------------ Alias LISTENER Version TNSLSNR for Linux: Version 11.2.0.4.0 - Production Start Date 16-JUN-2016 22:47:06 Uptime 0 days 0 hr. 0 min. 10 sec Trace Level off Security ON: Local OS Authentication SNMP OFF Listener Parameter File /u01/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora Listener Log File /u01/app/oracle/diag/tnslsnr/dg1/listener/alert/log.xml Listening Endpoints Summary... (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521))) (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=dg1)(PORT=1521))) Services Summary... Service "sameol" has 1 instance(s). Instance "sameol", status UNKNOWN, has 1 handler(s) for this service... The command completed successfully SQL*Plus: Release 11.2.0.4.0 Production on Thu Jun 16 22:47:16 2016 Copyright (c) 1982, 2013, Oracle. All rights reserved. Connected to an idle instance. SQL> ORACLE instance started. Total System Global Area 1653518336 bytes Fixed Size 2253784 bytes Variable Size 1543506984 bytes Database Buffers 100663296 bytes Redo Buffers 7094272 bytes Database mounted. Database opened. SQL> System altered. SQL> Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options [root@dg1 ~]# [root@dg1 ~]# ps -ef | grep pmon oracle 22880 1 0 22:47 ? 00:00:00 ora_pmon_sameol root 23000 22406 0 22:47 pts/5 00:00:00 grep pmon [root@dg1 ~]# ps -ef | grep lsnr oracle 22872 1 0 22:47 ? 00:00:00 /u01/app/oracle/product/11.2.0/dbhome_1/bin/tnslsnr LISTENER -inherit root 23004 22406 0 22:47 pts/5 00:00:00 grep lsnr [root@dg1 ~]#
二、通过/etc/oratab实现。
配置:/etc/oratab
[oracle@dg1 ~]$ cat /etc/oratab # # This file is used by ORACLE utilities. It is created by root.sh # and updated by either Database Configuration Assistant while creating # a database or ASM Configuration Assistant while creating ASM instance. # A colon, ':', is used as the field terminator. A new line terminates # the entry. Lines beginning with a pound sign, '#', are comments. # # Entries are of the form: # $ORACLE_SID:$ORACLE_HOME:: # # The first and second fields are the system identifier and home # directory of the database respectively. The third filed indicates # to the dbstart utility that the database should , "Y", or should not, # "N", be brought up at system boot time. # # Multiple entries with the same $ORACLE_SID are not allowed. # # #sameol:/u01/app/oracle/product/11.2.0/dbhome_1:N sameol:/u01/app/oracle/product/11.2.0/dbhome_1:Y [oracle@dg1 ~]$
在$ORACLE_HOME/bin下有两个文件也是需要注意的:
[oracle@dg1 ~]$ ls -ltr $ORACLE_HOME/bin | grep --color dbstart -rwxr-x--- 1 oracle oinstall 13855 Jan 1 2000 dbstart [oracle@dg1 ~]$ [oracle@dg1 ~]$ ls -ltr $ORACLE_HOME/bin | grep --color dbshut -rwxr-x--- 1 oracle oinstall 6088 Jan 1 2000 dbshut [oracle@dg1 ~]$ [oracle@dg1 ~]$
修改上面两个脚本中的“ORACLE_HOME_LISTNER”:
[oracle@dg1 ~]$ cat $ORACLE_HOME/bin/dbshut | grep --color ORACLE_HOME_LISTNER #ORACLE_HOME_LISTNER=$1 ORACLE_HOME_LISTNER=$ORACLE_HOME if [ ! $ORACLE_HOME_LISTNER ] ; then echo "ORACLE_HOME_LISTNER is not SET, unable to auto-stop Oracle Net Listener" LOG=$ORACLE_HOME_LISTNER/listener.log ORACLE_HOME=$ORACLE_HOME_LISTNER ; export ORACLE_HOME if [ -f $ORACLE_HOME_LISTNER/bin/tnslsnr ] ; then $ORACLE_HOME_LISTNER/bin/lsnrctl stop >> $LOG 2>&1 & echo "Failed to auto-stop Oracle Net Listener using $ORACLE_HOME_LISTNER/bin/tnslsnr" [oracle@dg1 ~]$ [oracle@dg1 ~]$ cat $ORACLE_HOME/bin/dbstart | grep --color ORACLE_HOME_LISTNER # The Listener log is located at $ORACLE_HOME_LISTNER/listener.log #ORACLE_HOME_LISTNER=$1 ORACLE_HOME_LISTNER=$ORACLE_HOME if [ ! $ORACLE_HOME_LISTNER ] ; then echo "ORACLE_HOME_LISTNER is not SET, unable to auto-start Oracle Net Listener" LOG=$ORACLE_HOME_LISTNER/listener.log ORACLE_HOME=$ORACLE_HOME_LISTNER ; export ORACLE_HOME if [ -x $ORACLE_HOME_LISTNER/bin/tnslsnr ] ; then $ORACLE_HOME_LISTNER/bin/lsnrctl start >> $LOG 2>&1 & VER10LIST=`$ORACLE_HOME_LISTNER/bin/lsnrctl version | grep "LSNRCTL for " | cut -d' ' -f5 | cut -d'.' -f1` echo "Failed to auto-start Oracle Net Listener using $ORACLE_HOME_LISTNER/bin/tnslsnr" $LOGMSG "Restart Oracle Net Listener using an alternate ORACLE_HOME_LISTNER:" [oracle@dg1 ~]$ [oracle@dg1 ~]$
如上,将“ORACLE_HOME_LISTNER=$1”,修改为:ORACLE_HOME_LISTNER=$ORACLE_HOME。
测试一下:
启库:
[oracle@dg1 ~]$ ps -ef | grep pmon oracle 24872 23012 0 23:08 pts/5 00:00:00 grep pmon [oracle@dg1 ~]$ [oracle@dg1 ~]$ ps -ef | grep lsnr oracle 24874 23012 0 23:08 pts/5 00:00:00 grep lsnr [oracle@dg1 ~]$ [oracle@dg1 ~]$ env | grep SID ORACLE_SID=sameol [oracle@dg1 ~]$ [oracle@dg1 ~]$ $ORACLE_HOME/bin/dbstart Processing Database instance "sameol": log file /u01/app/oracle/product/11.2.0/dbhome_1/startup.log [oracle@dg1 ~]$ [oracle@dg1 ~]$ ps -ef | grep pmon oracle 24988 1 0 23:08 ? 00:00:00 ora_pmon_sameol oracle 25192 23012 0 23:08 pts/5 00:00:00 grep pmon [oracle@dg1 ~]$ ps -ef | grep lsnr oracle 24880 1 0 23:08 ? 00:00:00 /u01/app/oracle/product/11.2.0/dbhome_1/bin/tnslsnr LISTENER -inherit oracle 25194 23012 0 23:08 pts/5 00:00:00 grep lsnr [oracle@dg1 ~]$
停库:
[oracle@dg1 ~]$ ps -ef | grep pmon oracle 24988 1 0 23:08 ? 00:00:00 ora_pmon_sameol oracle 25192 23012 0 23:08 pts/5 00:00:00 grep pmon [oracle@dg1 ~]$ ps -ef | grep lsnr oracle 24880 1 0 23:08 ? 00:00:00 /u01/app/oracle/product/11.2.0/dbhome_1/bin/tnslsnr LISTENER -inherit oracle 25194 23012 0 23:08 pts/5 00:00:00 grep lsnr [oracle@dg1 ~]$ [oracle@dg1 ~]$ $ORACLE_HOME/bin/dbshut Processing Database instance "sameol": log file /u01/app/oracle/product/11.2.0/dbhome_1/shutdown.log [oracle@dg1 ~]$ [oracle@dg1 ~]$ ps -ef | grep pmon oracle 25331 23012 0 23:09 pts/5 00:00:00 grep pmon [oracle@dg1 ~]$ ps -ef | grep lsnr oracle 25333 23012 0 23:09 pts/5 00:00:00 grep lsnr [oracle@dg1 ~]$
最后,把dbstart写到开机脚本,就可以开机自动启动Oralce的数据库实例了:
[root@dg1 ~]# cat /etc/rc.local #!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local # Startup Oracle on boot #su - oracle -c "sh do_oracle.sh sameol startup" su - oracle -c "$ORACLE_HOME/bin/dbstart" [root@dg1 ~]#
——————————————————————————
Done。