当前位置:操作系统 > Unix/Linux >>

写个脚本来启动数据库

写个脚本来启动数据库
 
每次打开Linux总要做写重复的动作,那就是启动监听,启动数据库,查看数据库状态。麻烦
还是写个shell脚本来控制方便些:
在$ORACLE_HOME/bin下有个dbstart脚本可以启动数据库,但是执行之后提示:
Failed to auto-start Oracle Net Listene using /ade/vikrkuma_new/oracle/bin/tnslsnr
我们来查看下dbstart是怎么写的,直接查询查找文件中的报错来自哪里:
/Failed to auto-start Oracle Net
[oracle@localhost bin]$ grep "Failed to auto-start Oracle Net" dbstart
    echo "Failed to auto-start Oracle Net Listene using $ORACLE_HOME_LISTNER/bin/tnslsnr"
在这之前有一句:
ORACLE_HOME_LISTNER=/ade/vikrkuma_new/oracle
开来是路径设错了,修改为:
ORACLE_HOME_LISTNER=$ORACLE_HOME
报错重新执行下,没报错,但是数据库却没有起来,修改文件:
vi /etc/oratab
修改为:
orclsid:/home/oracle/oracle/product/10.2.0/db_1:Y
记住,后面为Y
再起,报错:
查看启动日志:
$ORACLE_HOME/startup.log
[oracle@localhost bin]$ cat $ORACLE_HOME/startup.log
/home/oracle/oracle/product/10.2.0/db_1/bin/dbstart: Starting up database "orclsid"
Sat Apr  6 21:58:21 CST 2013
SQL*Plus: Release 10.2.0.1.0 - Production on Sat Apr 6 21:58:21 2013
Copyright (c) 1982, 2005, Oracle.  All rights reserved.
SQL> ERROR:
ORA-01031: insufficient privileges
SQL> ORA-01031: insufficient privileges
SQL> 
/home/oracle/oracle/product/10.2.0/db_1/bin/dbstart: Database instance "orclsid" warm started.
看来是权限问题,突然想到我之前修改了Oracle的默认认证方式,禁用了操作系统登录,估计在脚本里面使用的是操作系统登录:
一查,果然是这个问题:
修改为:conn sys/lubinsu as sysdba
重启下:
[oracle@localhost bin]$ dbstart
Processing Database instance "orclsid": log file /home/oracle/oracle/product/10.2.0/db_1/startup.log
/home/oracle/oracle/product/10.2.0/db_1/bin/dbstart: Starting up database "orclsid"
Sat Apr  6 22:25:27 CST 2013
 
 
SQL*Plus: Release 10.2.0.1.0 - Production on Sat Apr 6 22:25:27 2013
 
Copyright (c) 1982, 2005, Oracle.  All rights reserved.
 
SQL> Connected to an idle instance.
SQL> ORACLE instance started.
 
Total System Global Area  285212672 bytes
Fixed Size                  1218968 bytes
Variable Size             100664936 bytes
Database Buffers          176160768 bytes
Redo Buffers                7168000 bytes
Database mounted.
Database opened.
SQL> Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
 
/home/oracle/oracle/product/10.2.0/db_1/bin/dbstart: Database instance "orclsid" warm started.
看来已经起起来了,查看进程是否存在
[oracle@localhost bin]$ ps -ef | grep ora_pmon
oracle   14658     1  0 22:25 ?        00:00:00 ora_pmon_orclsid
oracle   14806  9470  0 22:26 pts/1    00:00:00 grep ora_pmon
NICE
我们继续来写脚本:
#!/bin/bash 
# chkconfig: 2345 89 20 
# description: starts the oracle listener and instance 
 
status() {
    pid=`ps -ef | grep ora_pmon | grep -v grep | awk '{print $8}'`
    if [ "X$pid" = "X" ]
    then
        echo "oracle10g is not running."
        exit 1
    else
        echo "oracle10g is running."
        exit 0
    fi
}
 
case "$1" in 
    start) 
        #startup the listener and instance 
        echo -n "oracle begin to startup: " 
        su - oracle -c "lsnrctl start" 
        su - oracle -c dbstart 
        echo "oracle10g started" 
        ;; 
    stop) 
        # stop listener, apache and database 
        echo -n "oracle begin to shutdown:" 
        su - oracle -c "lsnrctl stop" 
        su - oracle -c dbshut 
        echo "oracle10g shutdowned" 
        ;; 
    reload|restart) 
        $0 stop 
        $0 start 
        ;; 
    'status')
        status
        ;;
     *) 
        echo "Usage: ora10g [start|stop|reload|restart]" 
        exit 1 
esac 
exit 0 
 
赋给执行权限chmod a+x oracle10g
我们可以通过这个脚本来查看oracle的状态,启动,关闭或者重启数据库,方便许多。
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,