在Linux上自動啟動和關閉Oracle資料庫(9i/10g/11g)

來源:互聯網
上載者:User
  

在Oracle 1gR2或更高版本下使用RAC或ASM時,Oracle Clusterware會自動啟動和停止Oracle資料庫執行個體,因此下面的過程不是必需的,對於其他情況,你就可以使用下面描述的方法了。

◆su命令

下面的描述是Oracle建議採用的自動啟動和關閉Oracle 9i執行個體的方法。

一旦執行個體建立好後,標記/etc/oratab檔案設定每個執行個體的重啟標誌為“Y”:

 

TSH1:/u01/app/oracle/product/9.2.0:Y

 

接下來,作為root使用者登陸建立一個叫做/etc/init.d/dbora的檔案,包括下面的內容:

 

#!/bin/sh
# chkconfig: 345 99 10
# description: Oracle auto start-stop script.
#
# Set ORA_HOME to be equivalent to the $ORACLE_HOME
# from which you wish to execute dbstart and dbshut;
#
# Set ORA_OWNER to the user id of the owner of the
# Oracle database in ORA_HOME.

ORA_HOME=/u01/app/oracle/product/9.2.0
ORA_OWNER=oracle

if [ ! -f $ORA_HOME/bin/dbstart ]
then
echo "Oracle startup: cannot start"
exit
fi

case "$1" in
'start')
# Start the Oracle databases:
# The following command assumes that the oracle login
# will not prompt the user for any values
su - $ORA_OWNER -c "$ORA_HOME/bin/lsnrctl start"
su - $ORA_OWNER -c $ORA_HOME/bin/dbstart
;;
'stop')
# Stop the Oracle databases:
# The following command assumes that the oracle login
# will not prompt the user for any values
su - $ORA_OWNER -c $ORA_HOME/bin/dbshut
su - $ORA_OWNER -c "$ORA_HOME/bin/lsnrctl stop"
;;
esac

 

使用chmod命令設定許可權為750:

 

chmod 750 /etc/init.d/dbora

 

使用下面的命令配合適當的運行層級設定dbora服務自動啟動:

 

chkconfig --level 345 dbora on

 

這樣有關的執行個體就會隨系統的啟動而啟動了。

這個方法仍然適用於Oracle10g和11g,使用正確的路徑對ORA_HOME變數做一下改動,並將其添加到dbstart和dbshut末尾的行,在Oracle10gR2下可以移除啟動和停止監聽器的行了,因此dbstart命令包括啟動監聽器。

 

#!/bin/sh
# chkconfig: 345 99 10
# description: Oracle auto start-stop script.
#
# Set ORA_HOME to be equivalent to the $ORACLE_HOME
# from which you wish to execute dbstart and dbshut;
#
# Set ORA_OWNER to the user id of the owner of the
# Oracle database in ORA_HOME.

ORA_HOME=/u01/app/oracle/product/10.2.0/db_1
#ORA_HOME=/u01/app/oracle/product/11.1.0/db_1
ORA_OWNER=oracle

if [ ! -f $ORA_HOME/bin/dbstart ]
then
echo "Oracle startup: cannot start"
exit
fi

case "$1" in
'start')
# Start the Oracle databases:
# The following command assumes that the oracle login
# will not prompt the user for any values
su - $ORA_OWNER -c "$ORA_HOME/bin/dbstart $ORA_HOME"
;;
'stop')
# Stop the Oracle databases:
# The following command assumes that the oracle login
# will not prompt the user for any values
su - $ORA_OWNER -c "$ORA_HOME/bin/dbshut $ORA_HOME"
;;
esac

 

 

◆rsh命令

在Oracle10g中,Oracle推薦使用rsh命令而不是以前推薦的su命令了,在Oracle10gR2中,dbstart命令可以自動啟動監聽器,因此在這兩個版本之間有些不同之處,下面的說明更適合Oracle10g。

一旦執行個體建立完畢,編輯/etc/oratab檔案設定每個執行個體的重啟標誌為“Y”:

 

TSH1:/u01/app/oracle/product/9.2.0:Y

 

接下來,作為root使用者建立一個叫做/etc/init.d/dbora的檔案,包括下面的內容:

 

#!/bin/sh
# chkconfig: 345 99 10
# description: Oracle auto start-stop script.
#
# Change the value of ORACLE_HOME to specify the correct Oracle home
# directory for your installation.

ORACLE_HOME=/u01/app/oracle/product/10.2.0/db_1
#
# Change the value of ORACLE to the login name of the
# oracle owner at your site.
#
ORACLE=oracle

PATH=${PATH}:$ORACLE_HOME/bin
HOST=`hostname`
PLATFORM=`uname`
export ORACLE_HOME PATH
#
if [ ! "$2" = "ORA_DB" ] ; then
if [ "$PLATFORM" = "HP-UX" ] ; then
remsh $HOST -l $ORACLE -n "$0 $1 ORA_DB"
exit
else
rsh $HOST -l $ORACLE  $0 $1 ORA_DB
exit
fi
fi
#
case $1 in
'start')
$ORACLE_HOME/bin/dbstart $ORACLE_HOME
;;
'stop')
$ORACLE_HOME/bin/dbshut $ORACLE_HOME
;;
*)
echo "usage: $0 {start|stop}"
exit
;;
esac
#
exit

 

 

使用chmodml設定許可權為750:

chmod 750 /etc/init.d/dbora

使用下面的命令配合適當的運行層級設定dbora服務自動啟動:

chkconfig --level 345 dbora on

現在相關的執行個體應該隨系統的啟動而自動啟動了。

這個方法依賴於RSH伺服器,它需要額外的軟體包和配置:

  1. # Install the rhs and rsh-server packages from the OS CD/DVD.
  2. rpm -Uvh --force rsh-*
  3.  
  4. # Enable rsh and rlogin.
  5. chkconfig rsh on
  6. chkconfig rlogin on
  7. service xinetd reload

在FC5和FC6下嘗試這個方法時有問題,rsh是不被支援的,結果,我寧願使用su命令。

這個方法也可以用於沒有使用ASM或RAC的11g資料庫。

◆已知問題的解決

在Oracle10gR2中使用時,調用dbstart可能會產生下面的錯誤訊息:

Failed to auto-start Oracle Net Listener using /ade/vikrkuma_new/oracle/bin/tnslsnr

這是由於在dbstart指令碼中使用了寫入程式碼路徑,要解決這個問題,編輯$ORACLE_HOME/bin/dbstart指令碼,用

ORACLE_HOME_LISTNER=$ORACLE_HOME

替換

ORACLE_HOME_LISTNER=/ade/vikrkuma_new/oracle  //(大概在78行附近)

現在dbstart在啟動監聽器時應該就沒有問題了。

原文出處:http://www.oracle-base.com/articles/linux/AutomatingDatabaseStartupAndShutdownOnLinux.php

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.