linux下设置自动启动和关闭oracle实例、监听
1.设置/etc/oratab vi /etc/oratab
testdb2:/opt/oracle/product/9.2.0:Y
要把最后一个选项从N改为Y
2.测试dbstart和dbshut命令
dbstart和dbshut会检查/etc/oratab的设置,如果设置为Y,则执行启动或关闭,如果设置成N,则不做任何操作
测试方法:su - oracle
dbstart or dbshut
我在测试过程中,报错,意思是找不到init文件,进入sqlplus创建一个init文件即可:
sqlplus /nolog
conn /as sysdba
create pfile from spfile;
再次测试则成功
3.创建操作系统启动和关闭时调用的文件
3.1)修改/etc/rc.d/init.d/oracle文件
su - root
要在开机关机时自动启动和关闭数据库的话,需要修改 Linux 的启动脚本文件,这里要创建/etc/rc.d/init.d/oracle文件,这个文件的脚本会调用 dbshut 及 dbstart,内容如下:
# !/bin/sh
#
# /etc/rc.d/init.d/oracle
# 解说∶启动及停止 Oracle 数据库和监听程序
# 检查如何执行脚本文件
case "$1" in
start)
echo -n "Starting Oracle Databases: "
echo "----------------------------------------------------" >> /var/log/oracle
date +"! %T %a %D : Starting Oracle Databases as part of system up." >> /var/log/oracle
echo "----------------------------------------------------" >> /var/log/oracle
su - oracle -c dbstart >> /var/log/oracle
echo "Done."
echo -n "Starting Oracle Listeners: "
su - oracle -c "lsnrctl start" >> /var/log/oracle
echo "Done."
echo ""
echo "----------------------------------------------------" >> /var/log/oracle
date +"! %T %a %D : Finished." >> /var/log/oracle
echo "----------------------------------------------------" >> /var/log/oracle
touch /var/lock/subsys/oracle
;;
stop)
echo -n "Shutting Down Oracle Listeners: "
echo "----------------------------------------------------" >> /var/log/oracle
date +"! %T %a %D : Shutting Down Oracle Databases as part of system down." >> /var/log/oracle
echo "----------------------------------------------------" >> /var/log/oracle
su - oracle -c "lsnrctl stop" >> /var/log/oracle
echo "Done."
rm -f /var/lock/subsys/oracle
echo -n "Shutting Down Oracle Databases: "
su - oracle -c dbshut >> /var/log/oracle
echo "Done."
echo ""
echo "----------------------------------------------------" >> /var/log/oracle
date +"! %T %a %D : Finished." >> /var/log/oracle
echo "----------------------------------------------------" >> /var/log/oracle
;;
restart)
echo -n "Restarting Oracle Databases: "
echo "----------------------------------------------------" >> /var/log/oracle
date +"! %T %a %D : Restarting Oracle Databases as part of system up." >> /var/log/oracle
echo "----------------------------------------------------" >> /var/log/oracle
su - oracle -c dbshut >> /var/log/oracle
su - oracle -c dbstart >> /var/log/oracle
echo "Done."
echo -n "Restarting Oracle Listeners: "
su - oracle -c "lsnrctl stop" >> /var/log/oracle
su - oracle -c "lsnrctl start" >> /var/log/oracle
echo "Done."
echo ""
echo "----------------------------------------------------" >> /var/log/oracle
date +"! %T %a %D : Finished." >> /var/log/oracle
echo "----------------------------------------------------" >> /var/log/oracle
touch /var/lock/subsys/oracle
;;
*)
echo "Usage: oracle {start|stop|restart}"
exit 1
esac
编辑完这个文件后,要将其改为可执行
chmod +x oracle
chmod 777 oracle
然后测试一下,保证该脚本可以正常执行
cd /etc/rc.d/init.d
./oracle start
./oracle stop
./oracle restart
3.2)在相应的runlevel目录里建立符号连接
要在正确的执行等级目录 (runlevel ctories) /etc/rc.d/rcX.d 中建立及删除符号连结 (symbolic links)。
以下指令确保在执行等级 2, 3 及 5 下会启动数据库,级别4没有使用,所以不管。∶
ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc2.d/S99oracle
ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc3.d/S99oracle
ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc5.d/S99oracle
要在重新启动时停止数据库,我们需要以下连结∶
ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc0.d/K01oracle
ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc6.d/K01oracle
3.2)修改dbshut文件
因为dbshut命令默认是用shutdown normal方式关闭数据库,但实际情况下这种方式很难关掉数据库,因此需要更改dbshut脚本,将其中的shutdown命令改为 shutdown immediate
su - oracle
cd $ORACLE_HOME/bin
vi dbshut
按shift+: 输入 /shuwdown 查找 将其替换为shutdown immediate;保存退出 OK