配置Oracle資料庫和監聽隨Linux系統自啟動(轉)

yhj20041128001發表於2012-07-18
配置Oracle資料庫和監聽隨Linux系統自啟動

 
1.設定/etc/oratab
testdb2:/opt/oracle/product/9.2.0:Y
要把最後一個選項從N改為Y
2.測試dbstart和dbshut命令
dbstart和dbshut會檢查/etc/oratab的設定,如果設定為Y,則執行啟動或關閉,如果設定成N,則不做任何操作
測試方法:su - oracle
         dbstart or dbshut
我在測試過程中,報錯,意思是找不到init檔案,進入sqlplus建立一個init檔案即可:

        sqlplus /nolog

         conn /as sysdba

        create pfile from spfile;
再次測試則成功
3.建立作業系統啟動和關閉時呼叫的檔案

3.1)修改/etc/rc.d/init.d/oracle檔案

              su - root

要在開機關機時自動啟動和關閉資料庫的話,需要修改 Linux 的啟動指令碼檔案,這裡要建立/etc/rc.d/init.d/oracle檔案,這個檔案的指令碼會呼叫 dbshut 及 dbstart,內容如下:

# !/bin/sh
#
# /etc/rc.d/init.d/oracle
# 解說∶啟動及停止 Oracle 資料庫和監聽程式
# 檢查如何執行指令碼檔案
case "$1" in
  start)
        echo -n "Starting Oracle Databases: "
        echo "----------------------------------------------------" >> /var/log/oracle
        date +"! %T %a %D : Starting Oracle Databases as part of system up." >> /var/log/oracle
        echo "----------------------------------------------------" >> /var/log/oracle
        su - oracle -c dbstart >> /var/log/oracle
        echo "Done."
        echo -n "Starting Oracle Listeners: "
        su - oracle -c "lsnrctl start" >> /var/log/oracle
        echo "Done."
        echo ""
        echo "----------------------------------------------------" >> /var/log/oracle
        date +"! %T %a %D : Finished." >> /var/log/oracle
        echo "----------------------------------------------------" >> /var/log/oracle
        touch /var/lock/subsys/oracle
        ;;
  stop)
        echo -n "Shutting Down Oracle Listeners: "
        echo "----------------------------------------------------" >> /var/log/oracle
        date +"! %T %a %D : Shutting Down Oracle Databases as part of system down." >> /var/log/oracle
        echo "----------------------------------------------------" >> /var/log/oracle
        su - oracle -c "lsnrctl stop" >> /var/log/oracle
        echo "Done."
        rm -f /var/lock/subsys/oracle
        echo -n "Shutting Down Oracle Databases: "
        su - oracle -c dbshut >> /var/log/oracle
        echo "Done."
        echo ""
        echo "----------------------------------------------------" >> /var/log/oracle
        date +"! %T %a %D : Finished." >> /var/log/oracle
        echo "----------------------------------------------------" >> /var/log/oracle
        ;;
  restart)
        echo -n "Restarting Oracle Databases: "
        echo "----------------------------------------------------" >> /var/log/oracle
        date +"! %T %a %D : Restarting Oracle Databases as part of system up." >> /var/log/oracle
        echo "----------------------------------------------------" >> /var/log/oracle
        su - oracle -c dbshut >> /var/log/oracle
        su - oracle -c dbstart >> /var/log/oracle
        echo "Done."
        echo -n "Restarting Oracle Listeners: "
        su - oracle -c "lsnrctl stop" >> /var/log/oracle
        su - oracle -c "lsnrctl start" >> /var/log/oracle
        echo "Done."
        echo ""
        echo "----------------------------------------------------" >> /var/log/oracle
        date +"! %T %a %D : Finished." >> /var/log/oracle
        echo "----------------------------------------------------" >> /var/log/oracle
        touch /var/lock/subsys/oracle
        ;;
  *)
        echo "Usage: oracle {start|stop|restart}"
        exit 1
esac
編輯完這個檔案後,要將其改為可執行
chmod +x oracle
chmod 777 oracle
然後測試一下,保證該指令碼可以正常執行
cd /etc/rc.d/init.d
./oracle start
./oracle stop
./oracle restart
 
3.2)在相應的runlevel目錄裡建立符號連線

要在正確的執行等級目錄 (runlevel ctories) /etc/rc.d/rcX.d 中建立及刪除符號連結 (symbolic links)。

以下指令確保在執行等級 2, 3 及 5 下會啟動資料庫,級別4沒有使用,所以不管。∶

  ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc2.d/S99oracle
ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc3.d/S99oracle
ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc5.d/S99oracle
要在重新啟動時停止資料庫,我們需要以下連結∶ 
  ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc0.d/K01oracle
ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc6.d/K01oracle
3.2)修改dbshut檔案
因為dbshut命令預設是用shutdown normal方式關閉資料庫,但實際情況下這種方式很難關掉資料庫,因此需要更改dbshut指令碼,將其中的shutdown命令改為 shutdown immediate
su - oracle
cd $ORACLE_HOME/bin
vi dbshut
 
附錄:另外一種寫法
vi /etc/rc.d/init.d/oracle
#!/bin/bash
##############################################################
# Script. Function: Auto start oracle instance and listener. #
# Script. Name:     oracle                                   #
# Author:          Secooler Hou                              #
# Date:            2009-11-13 19:58:49                       #
##############################################################
case "$1" in
start)
echo "Starting Oracle Database..."
su - oracle -c "dbstart"
echo "Done."
echo "Starting Oracle Listener..."
su - oracle -c "lsnrctl start"
echo "Done."
touch /var/lock/subsys/oracle
;;
stop)
echo "Shutting down Oracle Listener..."
su - oracle -c "lsnrctl stop" >> /var/log/oracle
echo "Done."
rm -f /var/lock/subsys/oracle
echo "Shutting down Oracle Database..."
su - oracle -c "dbshut"
echo "Done."
echo ""
;;
restart)
$0 stop
$0 start
;;
*)
exit 1
esac

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23757700/viewspace-735894/,如需轉載,請註明出處,否則將追究法律責任。

相關文章