在談到 Oracle DB 12c 的安裝 install on linux 如何建立資料庫後,手動啟動/關閉資料庫總是覺得煩索了點,可以像 Windows Service  一樣自動化嗎? 這是可行的,接著我們來實作看看。

以上先用帳號:oracle 來編輯

Step 01: 建立 /home/oracle/scripts 目錄及 /home/oracle/scripts/startup.sh & /home/oracle/scripts/shutdown.sh 檔案

startup.sh

#!/bin/bash

export TMP=/tmp
export TMPDIR=$TMP
export PATH=/usr/sbin:/usr/local/bin:$PATH
export ORACLE_HOSTNAME=oradb12clinux.globeunion.com
export ORACLE_UNQNAME=orcl

export ORACLE_SID=orcl
ORAENV_ASK=NO
. oraenv
ORAENV_ASK=YES

# Start Listener
lsnrctl start

# Start Database
sqlplus / as sysdba << EOF
STARTUP;
EXIT;
EOF

 

shutdown.sh

#!/bin/bash

export TMP=/tmp
export TMPDIR=$TMP
export PATH=/usr/sbin:/usr/local/bin:$PATH
export ORACLE_HOSTNAME=oradb12clinux.globeunion.com
export ORACLE_UNQNAME=orcl

export ORACLE_SID=orcl
ORAENV_ASK=NO
. oraenv
ORAENV_ASK=YES

# Stop Database
sqlplus / as sysdba << EOF
SHUTDOWN IMMEDIATE;
EXIT;
EOF

# Stop Listener
lsnrctl stop

 

編輯好 /home/oracle/scripts/startup.sh & /home/oracle/scripts/shutdown.sh 兩個檔案後,記得用指令 chmod u+x /home/oracle/scripts/startup.sh /home/oracle/scripts/shutdown.sh 讓這兩個檔案變成可執行檔,並用手動執行,測試一下是否可以正常開啟與關閉資料庫。

 

以下以用帳號:root來編輯檔案

Step 02 : 手動測試可以正常開啟與關閉資料庫無誤後,再來建立 /etc/init.d/dbora 檔案,這是用來建立 Linux 上自建的服務啟動檔,內容如下:

#!/bin/sh
# chkconfig: 345 99 10
# description: Oracle auto start-stop script.
#
# Set ORA_OWNER to the user id of the owner of the 
# Oracle database software.

ORA_OWNER=oracle

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 "/home/oracle/scripts/startup.sh >> /home/oracle/scripts/startup_shutdown.log 2>&1"
        touch /var/lock/subsys/dbora
        ;;
    '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 "/home/oracle/scripts/shutdown.sh >> /home/oracle/scripts/startup_shutdown.log 2>&1"
        rm -f /var/lock/subsys/dbora
        ;;
esac

 

記得用指令 chmod 750 /etc/init.d/dboradbora 它變成可執行檔案,這時 Linux 的 Service 應有一個 dbora 的服務。

linux-service-oradb  

 

最後,用指令 chkconfig --add dbora 將它與系統啟動與關閉作關連。

db-running-level  

 

您可以使用下列兩個指令,並用帳號:root 來作測試

# service dbora start
# service dbora stop

資料庫啟動與關閉的記錄,則可以查看 /home/oracle/scripts/startup_shutdown.log 這個記錄檔。

 

參考:

http://www.oracle-base.com/articles/linux/automating-database-startup-and-shutdown-on-linux.php#oracle-11gr2-update

arrow
arrow
    文章標籤
    oracle 12c service lsnrctl
    全站熱搜

    MIS 發表在 痞客邦 留言(0) 人氣()