SUSE11 Oracle服务启停脚本&开机自启动

时间:2024-04-07 21:58:08

一、在SUSE11下手动启动Oracle

登录到suse11,切换到oracle用户权限

# su – oracle

接着输入:

$ sqlplus / as sysdba

继续输入:

SQL> startup

启动成功,如图所示:

SUSE11 Oracle服务启停脚本&开机自启动

停止命令,如下所示:

立即关闭,工作状态会回滚到一致状态:SQL> shutdown immediate;

等待用户完成工作后关闭:SQL>shutdowm normal;

强制关闭,下次重启要回滚日志:SQL>shutdown abort;

二、Oracle DB手动启动监听器

输入命令:

$ lsnrctl status

如图所示:

SUSE11 Oracle服务启停脚本&开机自启动

如果没有启动,可以输入:

$ lsnrctl start

SUSE11 Oracle服务启停脚本&开机自启动

手动启动Oracle数据库完毕,下面创建系统自行启动Oracle的脚本。

三、Oracle自启脚本

1. root用户下创建并编辑文件:/etc/oratab,使Oracle 自带的dbstart发挥作用。

输入命令:

 # vim  /etc/oratab

编辑内容:AMFEDB:/u01/oracle:Y

SUSE11 Oracle服务启停脚本&开机自启动

此内容路径可根据自身Oracle安装生成的oratab文件复制过来,查找方式如下图所示:

SUSE11 Oracle服务启停脚本&开机自启动

2. 在 /etc/init.d/ 下创建文件oracle,内容如下:

#!/bin/sh
#数据库所在路径
ORA_HOME=/opt/oracle/

#oracle用户
ORA_OWNER=oracle
if [ ! -f $ORA_HOME/bin/dbstart ]
then
    echo "Oracle startup: cannot start"
    exit
fi
case "$1" in
'start')
# 启动实例:
echo "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 - $ORA_OWNER -c "$ORA_HOME/bin/dbstart" >>/var/log/oracle
echo "Done"

# 启动监听:
echo "Starting Oracle Listeners ... "
echo "-------------------------------------------------" >> /var/log/oracle
date +" %T %a %D : Starting Oracle Listeners as part of system up." >> /var/log/oracle
echo "-------------------------------------------------" >> /var/log/oracle
su - $ORA_OWNER -c "$ORA_HOME/bin/lsnrctl start" >>/var/log/oracle
echo "Done."
echo "-------------------------------------------------" >> /var/log/oracle
date +" %T %a %D : Finished." >> /var/log/oracle
echo "-------------------------------------------------" >> /var/log/oracle
touch /var/lock/subsys/oracle
;;

'stop')
# 关闭监听:
echo "Stoping Oracle Listeners ... "
echo "-------------------------------------------------" >> /var/log/oracle
date +" %T %a %D : Stoping Oracle Listener as part of system down." >> /var/log/oracle
echo "-------------------------------------------------" >> /var/log/oracle
su - $ORA_OWNER -c "$ORA_HOME/bin/lsnrctl stop" >>/var/log/oracle
echo "Done."
rm -f /var/lock/subsys/oracle

# 关闭实例:
echo "Stoping Oracle Databases ... "
echo "-------------------------------------------------" >> /var/log/oracle
date +" %T %a %D : Stoping Oracle Databases as part of system down." >> /var/log/oracle
echo "-------------------------------------------------" >> /var/log/oracle
su - $ORA_OWNER -c "$ORA_HOME/bin/dbshut" >>/var/log/oracle
echo "Done."
echo ""
echo "-------------------------------------------------" >> /var/log/oracle
date +" %T %a %D : Finished." >> /var/log/oracle
echo "-------------------------------------------------" >> /var/log/oracle
;;

'restart')
$0 stop
$0 start
;;
esac

3. 改变文件权限
# chmod 755 /etc/init.d/oracle

4. 添加服务
# chkconfig --level 35 oracle on

5. 如果需要在关机或重启机器之前停止数据库执行如下命令
# ln -s /etc/init.d/oracle /etc/rc.d/rc0.d/K01oracle   //关机
# ln -s /etc/init.d/oracle /etc/rc.d/rc6.d/K01oracle   //重启 

6. 测试脚本创建成功
# service oracle start        //脚本启动oracle
# service oracle stop        //脚本关闭oracle
# service oracle restart     //脚本重启oracle

执行脚本无异常,即可重启系统实现Oracle自启动,如图所示:

SUSE11 Oracle服务启停脚本&开机自启动