您的位置:首页 > 数据库 > Oracle

自动启动和关闭Oracle 脚本

2011-06-02 10:14 316 查看
配置完ASM实例后,每次启动oracle数据库服务器的时候要先启动ASM实例,然后启动数据库实例;关闭的时候,需要先关闭oracle数据库实例,然后关闭ASM实例;敲起命来来麻烦的很,因而改进了原来的shell脚本来更好的实现自动化,为了加快启动和关闭的速度,该脚本不包含dbconsole的启动和关闭,脚本同样适用于非ASM环境。

一:总脚本调用/usr/bin/startdb和/usr/bin/stopdb脚本,使用chkconfig命令创建相关的符号链接后就可以使用service命令调用

[root@ora10g ~]# cat /etc/init.d/oracle
#!/bin/sh
#chkconfig: 35 85 15
#description:oracle
#function: start .. stop the oracle and asm instance on 10g R2 64bit
#author:lw.yang
#version: V.2.0

# Source function library.
. /etc/rc.d/init.d/functions

case "$1" in
start)
/usr/bin/startdb
;;

stop)
/usr/bin/stopdb
;;

*)
echo $"Usage: $0 {start|stop}"
exit 1

esac

二:/usr/bin/startdb脚本用来启动ASM实例和ORACLE数据库实例和监听器,启动前会进行判断是否配置了ASM以及实例是否已经启动,ASM实例需要ocssd.bin后台进程启动才可以运行

[root@ora10g ~]# cat /usr/bin/startdb
#!/bin/sh

#define variables

ASM_PID=$(pidof ocssd.bin)
ASM_PROC=$(ps -ef |grep asm_ |grep -v 'grep' |wc -l)

D_SID=$(su - oracle -c "env |grep ORACLE_SID|cut -d "=" -f 2")
DB_PROC=$(ps -ef |grep ora_ |grep -E 'smon|pmon|ckpt|lgwr' |wc -l)

#startup asm instance

if [ -z $ASM_PID ];then
echo "Not configure use ASM"
continue
elif [ $ASM_PROC -gt "7" ];then
echo "ASM instance already running"
else
su - oracle  -c "export ORACLE_SID=+ASM && sqlplus /nolog<<EOF
conn /as sysdba
startup
exit
EOF"
fi

#startup database instance

if [ $DB_PROC -eq "4" ];then
echo "Database instance already running,shutdown it firtst"
else
su - oracle -c "lsnrctl start"
su - oracle -c "export ORACLE_SID=$D_SID && sqlplus /nolog<<EOF
conn /as sysdba
startup
exit
EOF"
touch /var/lock/subsys/oracle
fi

三:/usr/bin/stopdb脚本用来关闭监听器,oracle数据库实例和ASM实例,关闭之前会先进行判断是否使用ASM以及相关实例是否已经关闭

#!/bin/sh

#define variables

ASM_PID=$(pidof ocssd.bin)
ASM_PROC=$(ps -ef |grep asm_ |grep -v 'grep' |wc -l)

D_SID=$(su - oracle -c "env |grep ORACLE_SID|cut -d "=" -f 2")
DB_PROC=$(ps -ef |grep ora_ |grep -E 'smon|pmon|ckpt|lgwr' |wc -l)

#stop database instance

if [ $DB_PROC -eq "0" ];then
echo "Database instance already shutdown"
else
su - oracle -c "lsnrctl stop"
su - oracle -c "export ORACLE_SID=$D_SID && sqlplus /nolog<<EOF
conn /as sysdba
shutdown immediate
exit
EOF"
rm -f /var/lock/subsys/oracle
fi

#stop asm instance

if [ -z $ASM_PID ];then
echo "Not configure use ASM"
elif [ $ASM_PROC -eq "0" ];then
echo "ASM instance already shutdown"
exit
else
su - oracle  -c "export ORACLE_SID=+ASM && sqlplus /nolog<<EOF
conn /as sysdba
shutdown immediate
exit
EOF"
fi

四:测试

非ASM环境下,数据库实例已经启动下测试:
[root@ora10g ~]# service oracle start
Not configure use ASM
Database instance already running,shutdown it firtst

[root@ora10g ~]# service oracle stop

LSNRCTL for Linux: Version 10.2.0.1.0 - Production on 02-JUN-2011 09:48:36

Copyright (c) 1991, 2005, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost.localdomain)(PORT=1521)))
The command completed successfully

SQL*Plus: Release 10.2.0.1.0 - Production on Thu Jun 2 09:48:40 2011

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

SQL> Connected.
SQL> Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options
Not configure use ASM

非ASM环境下,数据库实例已经关闭下测试:
[root@ora10g ~]# service oracle stop
Database instance already shutdown
Not configure use ASM

[root@ora10g ~]# service oracle start
Not configure use ASM

LSNRCTL for Linux: Version 10.2.0.1.0 - Production on 02-JUN-2011 09:50:25

Copyright (c) 1991, 2005, Oracle.  All rights reserved.

Starting /u01/app/oracle/bin/tnslsnr: please wait...

TNSLSNR for Linux: Version 10.2.0.1.0 - Production
System parameter file is /u01/app/oracle/network/admin/listener.ora
Log messages written to /u01/app/oracle/network/log/listener.log
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=ora10g.766.com)(PORT=1521)))
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC0)))

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost.localdomain)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 10.2.0.1.0 - Production
Start Date                02-JUN-2011 09:50:25
Uptime                    0 days 0 hr. 0 min. 0 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u01/app/oracle/network/admin/listener.ora
Listener Log File         /u01/app/oracle/network/log/listener.log
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=ora10g.766.com)(PORT=1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC0)))
Services Summary...
Service "PLSExtProc" has 1 instance(s).
Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully

SQL*Plus: Release 10.2.0.1.0 - Production on Thu Jun 2 09:50:25 2011

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

SQL> Connected to an idle instance.
SQL> ORACLE instance started.

Total System Global Area  629145600 bytes
Fixed Size                  2022824 bytes
Variable Size             230687320 bytes
Database Buffers          390070272 bytes
Redo Buffers                6365184 bytes
Database mounted.
Database opened.
SQL> Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options

五:设置操作系统环境变量

设置NLS_DATE_FORMAT和NLS_LANG环境变量,这两个变量需要同时设定,否则执行select sysdate from dual的时候,输出格式DD-MON-YY

SQL> select sysdate from dual;

SYSDATE

---------

02-JUN-11

[oracle@ora10g ~]$ env |grep NLS

NLS_LANG=american_america.UTF8

NLS_DATE_FORMAT=YYYY-MM-DD HH24:MI:SS

SQL> select sysdate from dual;

SYSDATE

-------------------

2011-06-02 10:04:49

设置默认编辑器为vim,若不设置该变量,在sqlplus中将无法使用ed命令

SQL> ed

Wrote file afiedt.buf

27

[oracle@ora10g ~]$ env |grep EDITOR

EDITOR=vim
本文出自 “斩月” 博客,谢绝转载!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: