Sunday, May 4, 2014

เปิด Oracle อัตโนมัติตอนบูธเครื่อง Solaris

โพสต์ครั้งแรก: 18 ธันวาคม 2011

ปกติเมื่อเราติดตั้ง Oracle บนเครื่องตระกูล Unix และใช้งานไปได้สบาย ๆ สักพัก พอวันหนึ่งเกิดเราต้อง Restart เครื่อง Server แต่ปรากฎว่าเราพบว่า Database ที่เราติดตั้งไว้ไม่ได้ Start ขึ้นมาด้วยทำให้เราต้อง Restart ฐานข้อมูลขึ้นมาเอง บทความวันนี้จะขอพูดถึงวิธีการทำให้ Oracle Start โดยอัตโนมัติเมื่อเราเปิดเครื่อง Server ขึ้นมา ซึ่งรวมถึงการ Start Listener โดยอัตโนมัติด้วย

Oracle ได้เตรียมสคริปต์ไว้สองตัวที่ใช้ในการ Start และ Stop Oracle บน OS โดยไม่ต้องล็อกอินเข้าไปในฐานข้อมูลได้แก่

$ORACLE_HOME/bin/dbstart
$ORACLE_HOME/bin/dbshut


คราวนี้พอเรา Restart เครื่อง Server ตอนที่เครื่องกำลัง Restart ขึ้นมา มันจะไปอ่านสคริปต์ในพาธ /etc/rc3.d ชื่อสคริปต์นี้คือ Snnname (S ย่อมาจาก Start) โดย "nn" หมายถึงลำดับของการรันเช่น S01script1 จะรันก่อน S03script3

ขณะเดียวกันตอนปิดเครื่อง Server มันจะไปอ่านสคริปต์ในพาธ /etc/rc0.d โดยอ่านสคริปต์ชื่อ Knnname (K ย่อมาจาก Kill)

เช่นถ้าเราต้องการให้รันสคริปต์ชื่อ oracle ขึ้นเป็นตัวสุดท้ายตอน Start Server และให้รันเป็นตัวแรกตอน Shutdown Server ไฟล์สคริปต์บน OS จะเป็นแบบนี้ /etc/rc3.d/S99oracle และ /etc/rc0.d/K01oracle ตามลำดับ ปกติในพาธ /etc/rc3.d/ จะมีไฟล์สคริปต์อื่น ๆ อีก มารวม ๆ กันเพื่อให้ระบบอ่านตอน Startup เรามักจะกำหนดตัวขึ้นต้นชื่อว่า S99 เพื่อให้ Oracle ถูกเปิดเป็นตัวสุดท้าย (ไฟล์ S99... เป็น Shortcut ที่จะอ้างไปถึงไฟล์สคริปต์ dbora ซึ่งจะไปเรียกสคริปต์ dbstart ของ Oracle อีกที

อีกประการหนึ่งสคริปต์ dbstart และ dbshut จะไปอ่านว่าจะ Startup และ Shutdown ฐานข้อมูลตัวไหนจากไฟล์ชื่อ /var/opt/oracle/oratab

เรามาเริ่มกันเลยดีกว่าครับ ในตัวอย่างข้างล่างผมทำบนเครื่อง VMWare ที่มี OS เป็น Solaris10 ใช้ Oracle10g Release 10.2.0.2

1) ตรวจสอบไฟล์ oratab
เข้าไปที่ Terminal เปิดไฟล์ oratab เพือกำหนดว่าจะให้ฐานข้อมูลตัวใดเปิดบ้างตอน Start เครื่อง
$ vi /var/opt/oracle/oratab


หาบรรทัดที่มีลักษณะคล้าย ๆ ข้างล่างนี้
$ORACLE_SID:$ORACLE_HOME:[Y|N]
ให้ตรวจสอบว่าตรง [Y|N] นั้นมีค่าเป็น Y สำหรับฐานข้อมูลตัวที่เราต้องการให้ขึ้นโดยอัตโนมัติตอน Start เครื่อง เช่น PAO1:/opt/oracle/102:Y หมายความว่าถ้า Start เครื่องให้เอาฐานข้อมูลชื่อ PAO1 ที่มี Oracle Home อยู่ที่ "/opt/oracle/102" ขึ้นมาด้วย (Y) ถ้าเรามีมากกว่า 1 ฐานข้อมูล เราก็เพิ่มบรรทัดเข้าไปได้เลย เช่น


PAO1:/opt/oracle/102:Y
ORCL:/opt/oracle/102:Y

2) สร้างสคริปต์ไฟล์ dbora เพื่อเปิดปิดฐานข้อมูลอัตโนมัติ
ไฟล์นี้จะไปเรียกสคริปต์ของ Oracle ชื่อ dbstart (เพื่อเปิด) และ dbshut (เพื่อปิด) เราจะสร้างไฟล์นี้ไว้ที่พาธ "etc/init.d"

a) ล็อกอินเป็น root
b)เปลี่ยนไดเรคทอรีไปที่ /etc/init.d
c) สร้างไฟล์ dbora และ chmod เป็น 750
# touch dbora
# chmod 750 dbora

d) Copy และ Paste ข้อมูลข้างล่างลงในไฟล์ dbora และแก้ส่วนที่กำหนดพารามิเตอร์ ORACLE_HOME และ ORA_OWNER ให้ถูกต้อง

# Set ORA_HOME to be equivalent to the $ORACLE_HOME
# from which you wish to execute dbstart and dbshut;
#
# Set ORA_OWNER to the user id of the owner of the
# Oracle database in ORA_HOME.

ORA_HOME=/opt/oracle/102
ORA_OWNER=oracle

if [! -f $ORA_HOME/bin/dbstart]
then
echo "Oracle startup: cannot start"
exit
fi

case "" 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 "$ORA_HOME/bin/lsnrctl start"
su - $ORA_OWNER -c $ORA_HOME/bin/dbstart &
;;

'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 "$ORA_HOME/bin/lsnrctl stop"
su - $ORA_OWNER -c $ORA_HOME/bin/dbshut $ORACLE_HOME &
;;

esac

3) สร้าง Symbolic Links
การสร้าง Symbolic Links เป็นคล้าย ๆ การสร้าง Shortcut เพื่อให้ระบบไปอ่านไฟล์สคริปต์ตัวที่เราต้องการตอนที่ Startup ขึ้นมามีขั้นตอนดังนี้
ขณะที่ยังเป็น User root อยู่ให้สร้าง Symbolic Links ดังข้างล่าง การสร้าง Symbolic Links ดังข้างล่างนี้เป็นการ Register Service ให้กับ OS ตอนบูธเครื่อง โดยตอนเปิดจะไปอ่าน /etc/rc3.d/S99oracle ซึ่งเป็น Shortcut ที่ชี้ไปที่ dbora ที่เราสร้างไว้ในขั้นตอนก่อนหน้า ส่วนตอนปิดก็จะไปอ่าน /etc/rc0.d/K01oracle ซึ่งเป็น Shortcut ไปยัง dbora เช่นกัน
# ln -s /etc/init.d/dbora /etc/rc3.d/S99oracle
# ln -s /etc/init.d/dbora /etc/rc0.d/K01oracle
 

4) ทดสอบสคริปต์ที่สร้างขึ้น 
ล็อกอินเป็น root แล้วรัน

# /etc/init.d/dbora start (เพื่อ Startup)
# /etc/init.d/dbora stop (เพื่อ Shutdown)

ถ้าการเปิดปิดฐานข้อมูลทำได้ปกติ แสดงว่าน่าจะ OK แล้ว
เราเช็คว่าฐานข้อมูลเปิดอยู่ได้จากคำสั่ง ps บน Unix เช่น
# ps -ef | grep pmon <== Check Process ของ Database ชื่อ PMON
oracle 1148 1 0 11:07:10 ? 0:01 ora_pmon_PAO1 <== แสดงว่า Start แล้ว
root 1533 1529 0 11:13:40 pts/3 0:00 grep pmon
# ps -ef | grep lsnr <== Check Listener Process
oracle 918 1 0 11:06:48 ? 0:00 /opt/oracle/102/bin/tnslsnr L ISTENER -inherit <== Start แล้ว
root 1535 1529 0 11:13:49 pts/3 0:00 grep lsnr

อ้างอิง: Automatic startup and shutdown oracle on linux, Oracle in World, Arju, October 2008

1 comment:

Tanakorn Tavornsasnavong said...

# Original script created by Arju

#!/bin/bash
#
# chkconfig: 35 99 10
# description: Starts and stops Oracle processes
#
ORA_HOME=/var/opt/oracle/product/10.2.0/db_1
ORA_OWNER=oracle

case "$1" in
'start')

# Start the TNS Listener
su - $ORA_OWNER -c "$ORA_HOME/bin/lsnrctl start"
# Start the Oracle databases:
su - $ORA_OWNER -c $ORA_HOME/bin/dbstart
# Start the Intelligent Agent
if [ -f $ORA_HOME/bin/emctl ];
then
su - $ORA_OWNER -c "$ORA_HOME/bin/emctl start agent"
elif [ -f $ORA_HOME/bin/agentctl ]; then

su - $ORA_OWNER -c "$ORA_HOME/bin/agentctl start"
else
su - $ORA_OWNER -c "$ORA_HOME/bin/lsnrctl dbsnmp_start"
fi
# Start Management Server
if [ -f $ORA_HOME/bin/emctl ]; then
su - $ORA_OWNER -c "$ORA_HOME/bin/emctl start dbconsole"
elif [ -f $ORA_HOME/bin/oemctl ]; then
su - $ORA_OWNER -c "$ORA_HOME/bin/oemctl start oms"
fi
# Start HTTP Server
if [ -f $ORA_HOME/Apache/Apache/bin/apachectl]; then
su - $ORA_OWNER -c "$ORA_HOME/Apache/Apache/bin/apachectl start"
fi
touch /var/lock/subsys/dbora
;;
'stop')
# Stop HTTP Server
if [ -f $ORA_HOME/Apache/Apache/bin/apachectl ]; then

su - $ORA_OWNER -c "$ORA_HOME/Apache/Apache/bin/apachectl stop"
fi
# Stop the TNS Listener

su - $ORA_OWNER -c "$ORA_HOME/bin/lsnrctl stop"
# Stop the Oracle databases:
su - $ORA_OWNER -c $ORA_HOME/bin/dbshut
rm -f /var/lock/subsys/dbora
;;
esac
# End of script dbora

Post a Comment