Thứ Hai, 30 tháng 6, 2014

[Oracle Database] Tự động Start hoặc Shutdown database instance

Mình làm trên công ty, máy server database cùi nên hay bị tắt giữa chừng. Mà mỗi lúc như vậy thì phải vào start listener rồi start database instance lên thấy mất công. Hôm nay post một bài mình tham khảo được về việc cấu hình tự động start 2 thứ đó lên cùng server, hoặc khi server shutdown thì nó tự động shutdown luôn.

Link tham khảo:

Đầu tiên, tạo thư mục chứa các script và cấp quyền sở hữu cho thư mục đó
mkdir -p /home/oracle/scripts
chown oracle.oracle/home/oracle/scripts 
Trong đó, user mình đang dùng là oracle, group là oracle luôn.
Dùng user root tạo file "/etc/init.d/dbora"
touch /etc/init.d/dbora
Sau đó chép đoạn nội dung này vào file vừa tạo
#!/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
        # Remove "&" if you don't want startup as a background process.
        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

Trong đó ORA_OWNER là user linux sở hữu database, của mình là user oracle (đa phần là vậy).

Cấp quyền cho file vừa tạo

chmod 750 /etc/init.d/dbora

Cấu hình để chạy file tự động khi khởi động cùng server
chkconfig --add dbora

Tạo script startup
touch /home/oracle/scripts/startup.sh
Chép nội dung này vào file vừa tạo
#!/bin/bash 
 
# cấu hình biến môi trường, giống file .bash_profile của user oracle
PATH=$PATH:$HOME/bin
export PATH
export ORACLE_BASE=/home/oracle/app/oracle
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/dbhome_1
export NLS_LANG=american_america.al32utf8
export PATH=$ORACLE_HOME/bin:$PATH
export ORACLE_SID=orcl

ORAENV_ASK=NO
. oraenv
ORAENV_ASK=YES

# Start Listener
lsnrctl start

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

Tạo script shutdown
touch /home/oracle/scripts/shutdown.sh
Chép nội dung này vào file vừa tạo
#!/bin/bash

# cấu hình biến môi trường, giống file .bash_profile của user oracle
PATH=$PATH:$HOME/bin
export PATH
export ORACLE_BASE=/home/oracle/app/oracle
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/dbhome_1
export NLS_LANG=american_america.al32utf8
export PATH=$ORACLE_HOME/bin:$PATH
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

Cấp quyền thực thi và quyền sở hữu 2 file này cho user oracle, group oracle
chmod u+x /home/oracle/scripts/startup.sh /home/oracle/scripts/shutdown.sh
chown oracle.oracle /home/oracle/scripts/startup.sh /home/oracle/scripts/shutdown.sh

Ok xong rồi, có thể test thử lại bằng lệnh sau
Start database instance
service dbora start
Stop database instance
service dbora stop

Hoặc reboot lại server sẽ thấy sau khi server reboot, database instance sẽ tự động start lên

Chúc vui !

Không có nhận xét nào:

Đăng nhận xét