首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫

Linux 下 Oracle 10g/11g 開機自動啟動完整配置腳本

     編輯:Oracle 資料庫相關知識

Linux 安裝 Oracle 後預設不會隨系統啟動,需手動配置 dbstart/dbshut 與 systemd (或 chkconfig) 整合。本文提供經測試可用的完整腳本與步驟。

前置條件

  1. /etc/oratab 最後一欄設為 Y:
    orcl:/u01/app/oracle/product/11.2.0/dbhome_1:Y
  2. 確認 $ORACLE_HOME/bin/dbstart、dbshut、lsnrctl 存在且可執行。

建立 System V 啟動腳本 /etc/init.d/oracle

#!/bin/sh
# chkconfig: 35 80 10
# description: Oracle auto start-stop script

ORA_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
ORA_OWNER=oracle
LOG_FILE=/var/log/oracle_startup.log

[ -f $ORA_HOME/bin/dbstart ] || { echo "dbstart not found"; exit 1; }

case "$1" in
  start)
    echo "Starting Oracle..." | tee -a $LOG_FILE
    date '+%T %a %D : Starting Oracle' >> $LOG_FILE
    su - $ORA_OWNER -c "$ORA_HOME/bin/dbstart $ORA_HOME" >> $LOG_FILE 2>&1
    su - $ORA_OWNER -c "$ORA_HOME/bin/lsnrctl start" >> $LOG_FILE 2>&1
    touch /var/lock/subsys/oracle
    echo "Done." | tee -a $LOG_FILE
    ;;
  stop)
    echo "Stopping Oracle..." | tee -a $LOG_FILE
    su - $ORA_OWNER -c "$ORA_HOME/bin/lsnrctl stop" >> $LOG_FILE 2>&1
    su - $ORA_OWNER -c "$ORA_HOME/bin/dbshut $ORA_HOME" >> $LOG_FILE 2>&1
    rm -f /var/lock/subsys/oracle
    echo "Done." | tee -a $LOG_FILE
    ;;
  restart)
    $0 stop
    $0 start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
    ;;
esac

註冊與啟用

chmod 755 /etc/init.d/oracle
chkconfig --add oracle
chkconfig --level 35 oracle on

# 關機/重啟前正常關閉
ln -sf /etc/init.d/oracle /etc/rc0.d/K01oracle
ln -sf /etc/init.d/oracle /etc/rc6.d/K01oracle

Systemd 版(RHEL 7+/CentOS 7+)

# /etc/systemd/system/oracle.service
[Unit]
Description=Oracle Database
After=network.target

[Service]
Type=forking
User=oracle
Environment=ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
Environment=ORACLE_SID=orcl
ExecStart=$ORACLE_HOME/bin/dbstart $ORACLE_HOME
ExecStop=$ORACLE_HOME/bin/dbshut $ORACLE_HOME
TimeoutSec=300

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable oracle
systemctl start oracle

驗證

# 重啟後檢查
su - oracle -c "sqlplus / as sysdba <<< 'SELECT status FROM v$instance;'"
# 應回傳 OPEN

常見坑點

  • oratab 格式錯誤(缺少冒號、SID 拼寫不一致)。
  • dbstart 依賴 ORACLE_HOME_LISTNER 環境變數,腳本中建議顯式 export。
  • SELinux 阻擋腳本執行,需 restorecon -v /etc/init.d/oracle 或設定布林值。

配置完成後,Oracle 將隨作業系統自動啟動/關閉,零人工干預。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved