Linux下让Oracle服务自动启动与停止

Linux下让Oracle服务自动启动与停止




如果在Windows下安装Oracle的话,会注册相应的服务,并随着操作系统启动而自动启动。但是,在Linux(环境是:RHEL5.2)下却需要进行一些配置才能实现这个功能。具体如下:

一、修改Oracle系统配置文件/etc/oratab
    [root@RHEL~]# gedit/etc/oratab
    /etc/oratab 格式为: SID:ORACLE_HOME:AUTO,把AUTO域设置为Y(大写)。只有这样,oracle 自带的dbstart和dbshut才能够发挥作用。我的环境为:orcl:/usr/app/oracle/product/10.2.0/db_1:Y
二、编写服务脚本
    [root@RHEL~]# gedit /etc/rc.d/init.d/oracle
    内容为:
#!/bin/bash
#
#################FUNCTION#############
#
# AutoStart Oracle and listener Using dbstart
# AutoStop Oracle and listener Using dbstop
#
#####################################
#
# Edited by LDY 2009-01-04
#

ORACLE_HOME=/usr/app/oracle/product/10.2.0/db_1
LOG=$ORACLE_HOME/oracle.log

case "$1" in
start)
echo "Starting Oracle Databases ... "
echo "-------------------------------------------------" >>$LOG 2>&1
date +" %T %a %D : Starting Oracle Databasee as part of system up.">> $LOG 2>&1
su - oracle -c "$ORACLE_HOME/bin/dbstart" >> $LOG 2>&1
echo "Done."
date +" %T %a %D : Finished." >> $LOG 2>&1
echo "-------------------------------------------------" >>$LOG 2>&1
touch /var/lock/subsys/oracle

;;
stop)
echo "Stopping Oracle Databases ... "
echo "-------------------------------------------------" >> $LOG2>&1
date +" %T %a %D : Stopping Oracle Databases as part of system down.">> $LOG 2>&1
su - oracle -c "$ORACLE_HOME/bin/dbshut" >> $LOG 2>&1
echo "Done."
date +" %T %a %D : Finished." >> $LOG 2>&1
echo "-------------------------------------------------" >>$LOG 2>&1
rm -f /var/lock/subsys/oracle

;;
restart)
$0 stop
$0 start

;;
*)
echo "Usage: oracle {start|stop|restart}"
exit 1
esac
    保存后,需要修改一下权限:
    [root@RHEL~]# chmod 755 /etc/rc.d/init.d/oracle
    关于这部分服务的编写,可以参考/etc/rc.d/init.d下其他服务,比如iptables之类的。
三、建立服务连接
    系统启动时启动数据库,我们需要以下连结∶
    [root@RHEL~]# ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc2.d/S99oracle
    [root@RHEL ~]# ln-s /etc/rc.d/init.d/oracle /etc/rc.d/rc3.d/S99oracle
    [root@RHEL ~]# ln-s /etc/rc.d/init.d/oracle /etc/rc.d/rc5.d/S99oracle #rc4.d unused
    要在重新启动时停止数据库,我们需要以下连结∶
    [root@RHEL~]# ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc0.d/K01oracle #stop
    [root@RHEL ~]# ln-s /etc/rc.d/init.d/oracle /etc/rc.d/rc6.d/K01oracle # restart
四、修改dbstart和dbshut
    尽管在服务脚本中,用到了dbstart和dbshut,但是还不知道里面有些什么。其实,还是需要简单地修改一下这两个文件,以便正确启动oracle 监听服务。
    [oracle@RHEL~]$  gedit $ORACLE_HOME/bin/dbstart
    可以在内容中找到:
# Set this to bring up Oracle Net Listener
ORACLE_HOME_LISTNER=/ade/vikrkuma_new/oracle
if [ ! $ORACLE_HOME_LISTNER ] ; then
  echo "ORACLE_HOME_LISTNER is not SET, unable to auto-start OracleNet Listener"
else
  LOG=$ORACLE_HOME_LISTNER/listener.log

  # Start Oracle Net Listener
  if [ -f $ORACLE_HOME_LISTNER/bin/tnslsnr ] ; then
    echo "$0: Starting Oracle Net Listener" >> $LOG2>&1
    $ORACLE_HOME_LISTNER/bin/lsnrctl start >> $LOG2>&1 &
    export VER10LIST=`$ORACLE_HOME_LISTNER/bin/lsnrctl version |grep "LSNRCTL for " | cut -d' ' -f5 | cut -d'.' -f1`
  else
    echo "Failed to auto-start Oracle Net Listene using$ORACLE_HOME_LISTNER/bin/tnslsnr"
  fi
fi
    这部分内容是启动Oracle监听服务,但是它指定的路径却是:/ade/vikrkuma_new/oracle。需要修改一下,变成我们自己的安装路径:
    ORACLE_HOME_LISTNER=/usr/app/oracle/product/10.2.0/db_1
    需要说明的是,如果监听(Listener)的名字不是标准的,那么还需要修改:$ORACLE_HOME_LISTNER/bin/lsnrctlstart 为$ORACLE_HOME_LISTNER/bin/lsnrctl start [监听名]。
    [oracle@RHEL~]$  gedit $ORACLE_HOME/bin/dbshut
    找遍内容,却没有发现有关于停止监听服务的内容。为了于dbstart对应,在文件中添加如下内容(其中着色部分是添加的内容):
.......................................................
# Save LD_LIBRARY_PATH
SAVE_LLP=$LD_LIBRARY_PATH

# Added by ldy 2009-01-04
# Set this to stop Oracle Net Listener
ORACLE_HOME_LISTNER=/usr/app/oracle/product/10.2.0/db_1
if [ ! $ORACLE_HOME_LISTNER ] ; then
  echo "ORACLE_HOME_LISTNER is not SET, unable to auto-stop OracleNet Listener"
else
  LOG=$ORACLE_HOME_LISTNER/listener.log

  # Stop Oracle Net Listener
  if [ -f $ORACLE_HOME_LISTNER/bin/tnslsnr ] ; then
    echo "$0: Stopping Oracle Net Listener" >>$LOG 2>&1
    $ORACLE_HOME_LISTNER/bin/lsnrctl stop >> $LOG2>&1 &
    export VER10LIST=`$ORACLE_HOME_LISTNER/bin/lsnrctl version |grep "LSNRCTL for " | cut -d' ' -f5 | cut -d'.' -f1`
  else
    echo "Failed to auto-stop Oracle Net Listene using$ORACLE_HOME_LISTNER/bin/tnslsnr"
  fi
fi
# End Added

# Set this in accordance with the platform
ORATAB=/etc/oratab
.......................................................
五、验证服务
    最简单的办法,就是重启电脑,查看效果。重启之后:
[oracle@RHEL ~]$ sqlplus/ as sysdba

SQL*Plus: Release 10.2.0.1.0 - Production on Sun Jan 4 18:41:37 2009

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


Connected to:
Oracle Database 10gEnterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL> select instance_name,status fromv$instance;

INSTANCE_NAME    STATUS
---------------- ------------
orcl            OPEN


[oracle@RHEL ~]$ lsnrctlstatus

LSNRCTL for Linux: Version 10.2.0.1.0 - Production on 04-JAN-2009 18:42:25

Copyright (c) 1991, 2005, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=RHEL.smnpc.com)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                    LISTENER
Version                  TNSLSNR for Linux: Version 10.2.0.1.0 - Production
StartDate               04-JAN-2009 15:16:45
Uptime                   0 days 3 hr. 25 min. 40 sec
TraceLevel              off
Security                 ON: Local OS Authentication
SNMP                     OFF
Listener Parameter File  /usr/app/oracle/product/10.2.0/db_1/network/admin/listener.ora
Listener Log File        /usr/app/oracle/product/10.2.0/db_1/network/log/listener.log
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=RHEL.smnpc.com)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC0)))
Services Summary...
Service "PLSExtProc" has 1 instance(s).
  Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) forthis service...
Service "orcl" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for thisservice...
Service "orclXDB" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for thisservice...
Service "orcl_XPT" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for thisservice...
The command completed successfully

    当然,也可以手动启动/关闭/重启服务:
    [root@RHEL~]# sercice oracle start
    [root@RHEL ~]# serciceoracle stop
    [root@RHEL ~]# serciceoracle restart
    如果在oracle用户下,那么直接用dbstart或者dbshut就可以了。此外,如果想启动数据库服务时也启动管理器或者isqlplus,那么还需要修改一下dbstart和dbshut脚本,或者在服务脚本中进行编写。增加:emctl start dbconsole和isqlplusctl start。

你可能感兴趣的:(Linux下让Oracle服务自动启动与停止)