利用dbstart和dbshut脚本自动启动和停止数据库的问题


客户的两台IBM Power 740小型机使用HACMP软件创建互备关系的数据库服务器,每台小型机运行一个数据库,任何一台服务器出现故障宕机,另一台小型机应该立即接管,且要一并接管数据库,这时在一台小型机上就运行了两个数据库,故障服务器恢复正常之后,相应的数据库会自动切换回主机。

在出现故障和恢复时,HACMP必须在两台小型机上调用启动和停止Oracle数据库的脚本完成切换过程,大部分的朋友应该和我一样应该会考虑使用Oracle的dbstart和dbshut脚本来生成相应的脚本。

我也是这么做的,但这么做了之后在测试过程中发现了如下一个问题:

        1).正常启动HACMP之后,两个数据库正常的在两个小型机上运行。
        2).小型机A出现故障,数据库A被正常的切换到了小型机B上运行。
        3).小型机A恢复正常,原有数据库正常的切换了回来,但这时运行在小型机B上的数据库宕机了。

上面的现象是什么原因喃,我查看了相关的文档,下面是一段很重要的文字描述:

Oracle recommends that you configure your system to automatically start Oracle Database when the system starts up, and to automatically shut it down when the system shuts down. Automating database startup and shutdown guards against incorrect database shutdown.
      
Oracle推荐配置在系统启动的时候自动启动Oracle数据库,当系统关闭的时候自动关闭数据库,自动化数据库的启动和关闭是为了防止不正确的数据库关闭。

To automate database startup and shutdown, use the dbstart and dbshut scripts, which are located in the $ORACLE_HOME/bin directory. The scripts refer to the same entries in the oratab file, which are applied on the same set of databases. You cannot, for example, have the dbstart script. automatically start sid1, sid2, and sid3, and have the dbshut script. shut down only sid1. However, you can specify that the dbshut script. shuts down a set of databases while the dbstart script. is not used at all. To do this, include a dbshut entry in the system shutdown file, but do not include the dbstart entry from the system startup files.

为了实现自动化启动和关闭数据库,需要使用存放在$ORACLE_HOME/bin目录下的dbstart和dbshut脚本,这两个脚本引用/etc/oratab文件中的相同条目,应用于在相同的数据库集。不能有如下的情况,例如,使用dbstart脚本自动的开始sid1,sid2和sid3数据库,同时只使用dbshut脚本停止sid1数据库,这是做不到的。然而可以在不使用dbstart脚本启动数据库的情况下,指定使用dbshut脚本来停止数据库集。如果你想这么做,将包含dbshut脚本的条目写入操作系统停止文件,但是不要将包含dbstart脚本的条目写入操作系统的启动文件中。

在使用dbstart和dbshut脚本启动和停止数据库之前需要先将存储在/etc/oratab配置文件中的数据库属性修改为Y,格式如下:sid:oracle_home_directory:[Y|N],然后就可以在系统启动配置文件和系统停止配置文件中加入dbstart和dbshut脚本使得系统在启动和异常关闭的情况下先启动和正常关闭数据库,避免数据库不正常的关闭带来的损失。脚本标准的用法是:

dbstart $ORACLE_HOME
dbshut $ORACLE_HOME

从上面的描述我们可以了解到,如果一台服务器上有多套Oracle数据库,那么没法控制使用dbstart和dbshut脚本启动和停止某一个数据库,两个脚本会将/etc/oratab配置文件中属性修改为Y的数据库都启动和停止,这就是出现最开始描述的问题的原因。

为了解决这个问题,只有手动写脚本来固定启动和停止某个数据库,下面是一个例子:

1).自动启动脚本。
root用户下面的脚本:
 
##############################################################
## start oracle server
echo "`hostname`:The ORACLE Server typt is starting,Please Waiting."
sleep 3
su - oracle -c "./a_start.sh"
sleep 3
echo "`hostname`:The ORACLE Server typt is started."
##############################################################
oracle用户下面的脚本:
a_start.sh
echo "Switch To typt"
export ORACLE_SID=typt
 
lsnrctl start
echo "Start Oracle DataBase typt Begin"
sqlplus /nolog 
connect /as sysdba
startup
exit
EOF
sleep 3
echo "Start Oracle DataBase typt End"
        HACMP通过root用户下的脚本调用a_start.sh脚本完成对监听器和数据库的自动启动。
2).自动停止脚本。
root用户下的脚本:
 
##############################################################
## stop oracle server
echo "`hostname`:The ORACLE Server typt is stopping,Please Waiting."
su - oracle -c "./a_stop.sh"
sleep 5
echo "`hostname`:The ORACLE Server typt is stoped."
##############################################################
oracle用户下的脚本:
a_stop.sh
 
echo "Switch To typt"
export ORACLE_SID=typt
echo "Stop Oracle DataBase typt Begin"
sleep 5
sqlplus /nolog
connect /as sysdba
shutdown immediate
exit
lsnrctl stop
EOF
echo "Stop Oracle DataBase typt End"

以上是自动停止Oracle数据库的脚本,HACMP通过root用户下的脚本调用a_stop.sh脚本自动停止Oracle数据库,两个脚本都是通过设置ORACLE_SID环境变量来明确的启动、停止数据库。
--end--

相关内容