本文档主要讲述如何在Linux上配置Oracle数据库11gR2 和12c自动启动和关闭/配置EM12c自动启动和关闭。
1 配置数据库自动启动和关闭
如果你使用的是Oracle Clustwerware 10gR2及以上版本安装的Oracle RAC,或者是安装的ASM环境下的单实例数据库,那么Clusterware软件会自动启停数据库实例和监听器listener。如果没有安装Clusterware软件,请参照以下方法配置数据库随操作系统的启动自动启动,随操作系统的关闭自动关闭。
下面方法适用于所有的RHEL和OEL所有版本。
下面以Oracle12c版本为例,但是对9i,10g,11g,大同小异,具体请参考后面的参考文献。1. 1 修改 /etc/oratab文件
编辑/etc/oratab文件,将需要自动启动的实例的那一行的最后:N 修改为:Y
[root@x4270a201227 ~]# su - oracle
[oracle@x4270a201227 ~]$ ls -l /etc/oratab
-rw-rw-r-- 1 oracle oinstall 787 Jan 9 02:33 /etc/oratab
[oracle@x4270a201227 ~]$ vi /etc/oratab
EMREP:/u01/app/oracle/product/12.1.0.2/db_1:Y
## 这个默认是N,这里修改为Y,最前面的EMREP就是实例名称
:wq!
1. 2 创建/etc/init.d/dbora文件
以root用户创建/ec/init.d/dbora文件
[root@x4270a201227 ~]# vi /etc/init.d/dbora
#!/bin/sh
# chkconfig: 345 99 10
# description: Oracle auto start-stop script.
#
# Set ORA_OWNER to the user id of the owner of the
# Oracle database software.
ORA_OWNER=oracle
case "$1" in
'start')
# Start the Oracle databases:
# The following command assumes that the oracle login
# will not prompt the user for any values
# Remove "&" if you don't want startup as a background process.
su $ORA_OWNER -c "/home/oracle/scripts/startup.sh >> /home/oracle/scripts/startup_shutdown.log 2>&1" &
touch /var/lock/subsys/dbora
;;
'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 "/home/oracle/scripts/shutdown.sh >> /home/oracle/scripts/startup_shutdown.log 2>&1"
rm -f /var/lock/subsys/dbora
;;
esac
[root@x4270a201227 bin]#
1. 3 修改/etc/init.d/dbora权限
[root@x4270a201227 ~]#chmod 750 /etc/init.d/dbora
1. 4 设置dbora服务自动启动
将dbora服务设置成适当的运行级别,并设置为自动启动
[root@x4270a201227 ~]#chkconfig --add dbora
[root@mgtsvr ~]# chkconfig--list dbora
dbora 0:off 1:off 2:off 3:on 4:on 5:on 6:off
[root@mgtsvr ~]# chkconfigdbora on
[root@mgtsvr ~]# chkconfig--list dbora
dbora 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@x4270a201227 ~]# mkdir -p /home/oracle/scripts
[root@x4270a201227 ~]# chown oracle.oinstall /home/oracle/scripts
[root@x4270a201227 ~]# vi/home/oracle/scripts/startup.sh
#!/bin/bash
export TMP=/tmp
export TMPDIR=$TMP
exportPATH=/usr/sbin:/usr/local/bin:$PATH
exportORACLE_HOSTNAME=x4270a201227.osc.oracle.com
exportORACLE_UNQNAME=EMREP
export ORACLE_SID=EMREP
ORAENV_ASK=NO
. oraenv
ORAENV_ASK=YES
# Start Listener
lsnrctl start
# Start Database
sqlplus / as sysdba<< EOF
STARTUP;
EXIT;
EOF
[root@x4270a201227 ~]# vi/home/oracle/scripts/shutdown.sh
#!/bin/bash
export TMP=/tmp
export TMPDIR=$TMP
exportPATH=/usr/sbin:/usr/local/bin:$PATH
exportORACLE_HOSTNAME=x4270a201227.osc.oracle.com
exportORACLE_UNQNAME=EMREP
export ORACLE_SID=EMREP
ORAENV_ASK=NO
. oraenv
ORAENV_ASK=YES
# Stop Database
sqlplus / as sysdba<< EOF
SHUTDOWN IMMEDIATE;
EXIT;
EOF
# Stop Listener
lsnrctl stop
1. 6 设置startup.sh和shutdown权限
[root@x4270a201227 ~]# chmod u+x /home/oracle/scripts/startup.sh/home/oracle/scripts/shutdown.sh
[root@x4270a201227 ~]# chown oracle.oinstall/home/oracle/scripts/startup.sh /home/oracle/scripts/shutdown.sh
1. 7 测试
现在对应的数据库和listener就会随OS系统自动启停,可以用下面命令进行测试。
[root@x4270a201227 ~]# service dbora start
[root@x4270a201227 ~]# service dbora stop
现在,对应实例的数据库将会随OS自动启停
2 配置EM12c自动启动和关闭
2. 1 创建/etc/init.d/em12c文件
[root@x4270a201227 ~]# vi/etc/init.d/em12c
#!/bin/sh
# chkconfig: 345 99 10
# description: OracleOEM12c auto start-stop script.
#
# Set ORA_OWNER to theuser id of the owner of the
# Oracle EnterpriseManager Cloud Control 12c software.
ORA_OWNER=oracle
case "$1" in
'start')
# Start the OEM12c:
# The following command assumes thatthe oracle login
# will not prompt the user for anyvalues
# Remove "&" if you don'twant startup as a background process.
su $ORA_OWNER -c"/home/oracle/scripts/em12cstart.sh >>/home/oracle/scripts/em12cstart_stop.log 2>&1" &
touch /var/lock/subsys/em12c
;;
'stop')
# Stop the OEM12c:
# The following command assumes thatthe oracle login
# will not prompt the user for anyvalues
su $ORA_OWNER -c"/home/oracle/scripts/em12cstop.sh >>/home/oracle/scripts/em12cstart_stop.log 2>&1"
rm -f /var/lock/subsys/em12c
;;
esac
2. 2 修改/etc/init.d/em12c权限
[root@x4270a201227 ~]#chmod 750 /etc/init.d/em12c
2. 3 设置em12c服务自动启动
将em12c服务设置成适当的运行级别,并设置为自动启动
[root@x4270a201227 ~]#chkconfig --add em12c
[root@mgtsvr ~]# chkconfig--list em12c
em12c 0:off 1:off 2:off 3:on 4:on 5:on 6:off
[root@mgtsvr ~]# chkconfigem12c on
[root@mgtsvr ~]# chkconfig--list em12c
em12c 0:off 1:off 2:on 3:on 4:on 5:on 6:off
2. 4 创建em12cstart.sh和em12cstop.sh脚本
[root@x4270a201227 ~]# vi/home/oracle/scripts/em12cstart.sh
#!/bin/bash
exportOMS_HOME=/u01/app/oracle/oms12cr4/oms
exportAGENT_HOME=/u01/app/oracle/agent12cr4/core/12.1.0.4.0
$OMS_HOME/bin/emctl startoms
$AGENT_HOME/bin/emctlstart agent
[root@x4270a201227 ~]#
[root@x4270a201227 ~]# vi/home/oracle/scripts/em12cstop.sh
#!/bin/bash
exportOMS_HOME=/u01/app/oracle/oms12cr4/oms
exportAGENT_HOME=/u01/app/oracle/agent12cr4/core/12.1.0.4.0
$OMS_HOME/bin/emctl stopoms -all
$AGENT_HOME/bin/emctl stopagent
[root@x4270a201227 ~]#
2. 5 设置em12cstart.sh和em12cstop.sh权限
[root@x4270a201227 ~]# chmod u+x /home/oracle/scripts/em12cstart.sh/home/oracle/scripts/em12cstop.sh
[root@x4270a201227 ~]# chown oracle.oinstall/home/oracle/scripts/em12cstart.sh /home/oracle/scripts/em12cstop.sh
2. 6 测试
现在EM12c就会随OS系统自动启停,可以用下面命令进行测试。
[root@x4270a201227 ~]# service em12c start
[root@x4270a201227 ~]# service em12c stop
现在,EM12c将会随OS自动启停。前提是数据库已经随OS已经自动启动,如何数据库自动启动在EM自动启动之前,则EM自动启动就会报错,因为EM在启动的时候需要EMRepository的数据库和listener已经起来了。
可以将上面修改一下,在EM自动启动的脚本里面将数据库自动启动起来。在停止EM的时候将数据库也自动停止。