linux下Oracle自动启动与停止(一)

时间:2021-01-08 07:49:36
  1. 修改Oracle系统配置文件/etc/oratab
    /etc/oratab 格式为: SID:ORACLE_HOME:AUTO
    把AUTO域设置为Y(大写),只有这样,oracle 自带的dbstart和dbshut才能够发挥作用。我的为:
    $ORACLE_SID:$ORACLE_HOME:Y
    这儿的ORACLE_SID和ORACLE_HOME是oracle用户下的环境变量,在不同的电脑上有不同的值.当你打开/etc/oratab的时候,修改一下就行了.
  2. 编写服务脚本
    在/etc/rc.d/init.d目录下创建oracle文件作为启动脚本,内容如下:
    #!/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 - ora -c dbstart >> /var/log/oracle
            echo "Done."
            echo -n "Starting Oracle Listeners: "
            su - ora -c "lsnrctl start" >> /var/log/oracle
            #su - oracle  "sqlplus /nolog" <
            #connect /as sysdba
            #startup
            #exit
            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 - ora -c "lsnrctl stop" >> /var/log/oracle
            echo "Done."
            rm -f /var/lock/subsys/oracle
            echo -n "Shutting Down Oracle Databases: "
            su - ora -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 - ora -c dbstop >> /var/log/oracle
            su - ora -c dbstart >> /var/log/oracle
            echo "Done."
            echo -n "Restarting Oracle Listeners: "
            su - ora -c "lsnrctl stop" >> /var/log/oracle
            su - ora -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
    在oracle用户下输入如上的内容,然后保存退出,由于是启动脚本,需要执行权限;执行命令chmod a+x oracle授予oracle执行权限.
  3. 建立服务连接
    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
  4. 测试
    运行./oracle start 命令,看是否能启动数据库,如果不能,查看日志,查找原因
    我在执行得时候遇到了如下得错误信息
    Can't find init file for Database "linuxdb".
    Database "linuxdb" NOT started.
    通过查看${ORACLE_HOME}/bin/dbstart中的脚本,根据错误信息查找到语句,得知系统中缺少
    ${ORACLE_HOME}/dbs/init${ORACLE_SID}.ora配置文件
    解决办法
    sqlplus "/as sysdba"
    create pfile from spfile;
    然后再执行就成功了.

    重新启动linux主机,看数据库是否自动启动了.
    另外也可以通过别的方式启动,系统在启动的时候会遍历/etc/profile.d目录,把所有的.sh文件都执行一遍.
    也可以通过修改/etc/rc.d/rc.local或/etc/rc.d/rc文件来达到开机启动程序的目的,但是局限是不能指定启动用户.
    如果在unix下,启动脚本应该建立在/sbin/init.d下,然后在/sbin/rc0~6.d下面建立连接;