天天看點

利用dbstart和dbshut腳本自動啟動和停止資料庫的問題

        客戶的兩台IBM Power 740小型機使用HACMP軟體建立互備關系的資料庫伺服器,每台小型機運作一個資料庫,任何一台伺服器出現故障當機,另一台小型機應該立即接管,且要一并接管資料庫,這時在一台小型機上就運作了兩個資料庫,故障伺服器恢複正常之後,相應的資料庫會自動切換回主機。

        在出現故障和恢複時,HACMP必須在兩台小型機上調用啟動和停止Oracle資料庫的腳本完成切換過程,大部分的朋友應該和我一樣應該會考慮使用Oracle的dbstart和dbshut腳本來生成相應的腳本。

        我也是這麼做的,但這麼做了之後在測試過程中發現了如下一個問題:

        1).正常啟動HACMP之後,兩個資料庫正常的在兩個小型機上運作。

        2).小型機A出現故障,資料庫A被正常的切換到了小型機B上運作。

        3).小型機A恢複正常,原有資料庫正常的切換了回來,但這時運作在小型機B上的資料庫當機了。

         上面的現象是什麼原因喃,我檢視了相關的文檔,下面是一段很重要的文字描述:

        Oracle recommends that you configure your system to automatically start Oracle Database when the system starts up, and to automatically shut it down when the system shuts down. Automating database startup and shutdown guards against incorrect database shutdown.

         Oracle推薦配置在系統啟動的時候自動啟動Oracle資料庫,當系統關閉的時候自動關閉資料庫,<b>自動化資料庫的啟動和關閉是為了防止不正确的資料庫關閉</b>。

        To automate database startup and shutdown, use the dbstart and dbshut scripts, which are located in the $ORACLE_HOME/bin directory. The scripts refer to the same entries in the oratab file, which are applied on the same set of databases. You cannot, for example, have the dbstart script. automatically start sid1, sid2, and sid3, and have the dbshut script. shut down only sid1. However, you can specify that the dbshut script. shuts down a set of databases while the dbstart script. is not used at all. To do this, include a dbshut entry in the system shutdown file, but do not include the dbstart entry from the system startup files.

        為了實作自動化啟動和關閉資料庫,需要使用存放在$ORACLE_HOME/bin目錄下的dbstart和dbshut腳本,<b>這兩個腳本引用/etc/oratab檔案中的相同條目,應用于在相同的資料庫集。</b>不能有如下的情況,例如,使用dbstart腳本自動的開始sid1,sid2和sid3資料庫,同時隻使用dbshut腳本停止sid1資料庫,這是做不到的。然而可以在不使用dbstart腳本啟動資料庫的情況下,指定使用dbshut腳本來停止資料庫集。如果你想這麼做,将包含dbshut腳本的條目寫入作業系統停止檔案,但是不要将包含dbstart腳本的條目寫入作業系統的啟動檔案中。

       在使用dbstart和dbshut腳本啟動和停止資料庫之前需要先将存儲在/etc/oratab配置檔案中的資料庫屬性修改為Y,格式如下:sid:oracle_home_directory:[Y|N],然後就可以在系統啟動配置檔案和系統停止配置檔案中加入dbstart和dbshut腳本使得系統在啟動和異常關閉的情況下先啟動和正常關閉資料庫,避免資料庫不正常的關閉帶來的損失。腳本标準的用法是:

dbstart $ORACLE_HOME

dbshut $ORACLE_HOME

        從上面的描述我們可以了解到,如果一台伺服器上有多套Oracle資料庫,那麼沒法控制使用dbstart和dbshut腳本啟動和停止某一個資料庫,兩個腳本會将/etc/oratab配置檔案中屬性修改為Y的資料庫都啟動和停止,這就是出現最開始描述的問題的原因。

        為了解決這個問題,隻有手動寫腳本來固定啟動和停止某個資料庫,下面是一個例子:

<b>1).自動啟動腳本。</b>

root使用者下面的腳本:

##############################################################

## start oracle server

echo "`hostname`:The ORACLE Server typt is starting,Please Waiting."

sleep 3

su - oracle -c "./a_start.sh"

echo "`hostname`:The ORACLE Server typt is started."

oracle使用者下面的腳本:

a_start.sh

echo "Switch To typt"

export ORACLE_SID=typt

lsnrctl start

echo "Start Oracle DataBase typt Begin"

sqlplus /nolog  &lt;&lt;EOF

connect /as sysdba

startup

exit

EOF

echo "Start Oracle DataBase typt End"

        HACMP通過root使用者下的腳本調用a_start.sh腳本完成對監聽器和資料庫的自動啟動。

<b>2).自動停止腳本。</b>

root使用者下的腳本:

## stop oracle server

echo "`hostname`:The ORACLE Server typt is stopping,Please Waiting."

su - oracle -c "./a_stop.sh"

sleep 5

echo "`hostname`:The ORACLE Server typt is stoped."

oracle使用者下的腳本:

a_stop.sh

echo "Stop Oracle DataBase typt Begin"

sqlplus /nolog &lt;&lt;EOF

shutdown immediate

lsnrctl stop

echo "Stop Oracle DataBase typt End"

         以上是自動停止Oracle資料庫的腳本,HACMP通過root使用者下的腳本調用a_stop.sh腳本自動停止Oracle資料庫,兩個腳本都是通過設定ORACLE_SID環境變量來明确的啟動、停止資料庫。

--end--

       感謝同僚老譚對我的幫助!