Oracle 数据库和监听器开机自启动两种实现方法

时间:2022-10-22 08:40:23
数据库和监听器开机自启动
 
编辑oratab文件:
修改:orcl:/u01/app/oracle/product/11.2.0/db_1:N
           orcl:/u01/app/oracle/product/11.2.0/db_1:Y
[oracle@ocptest bin]$ vi /etc/oratab
#
 
 
# This file is used by ORACLE utilities.  It is created by root.sh
# and updated by the Database Configuration Assistant when creating
# a database.
 
 
# A colon, ':', is used as the field terminator.  A new line terminates
# the entry.  Lines beginning with a pound sign, '#', are comments.
#
# Entries are of the form:
#   $ORACLE_SID:$ORACLE_HOME:<N|Y>:
#
# The first and second fields are the system identifier and home
# directory of the database respectively.  The third filed indicates
# to the dbstart utility that the database should , "Y", or should not,
# "N", be brought up at system boot time.
#
# Multiple entries with the same $ORACLE_SID are not allowed.
#
#
orcl:/u01/app/oracle/product/11.2.0/db_1:Y

  

  

执行dbstart和dbshut(数据库启动而监听器没有自启动)
[oracle@ocptest ~]$ dbstart
ORACLE_HOME_LISTNER is not SET, unable to auto-start Oracle Net Listener
Usage: /u01/app/oracle/product/11.2.0/db_1/bin/dbstart ORACLE_HOME
Processing Database instance "orcl": log file /u01/app/oracle/product/11.2.0/db_1/startup.log

  

 
修改dbstart和dbshut文件:
[oracle@ocptest ~]$ cd $ORACLE_HOME/bin
[oracle@ocptest bin]$ vi dbstart

  

 
找到这行修改为:
ORACLE_HOME_LISTNER=$1
ORACLE_HOME_LISTNER=$ORACLE_HOME
同上修改dbshut文件。
 
dbstart验证:
[oracle@ocptest bin]$ dbstart
Processing Database instance "orcl": log file /u01/app/oracle/product/11.2.0/db_1/startup.log
[oracle@ocptest bin]$ ps -ef|grep smon
oracle     4125      1  0 23:46 ?        00:00:00 ora_smon_orcl
oracle     4241   3254  0 23:46 pts/0    00:00:00 grep smon
[oracle@ocptest bin]$ lsnrctl status
 
 
LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 21-APR-2019 23:47:11
 
 
Copyright (c) 1991, 2009, Oracle.  All rights reserved.
 
 
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=ocptest)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 11.2.0.1.0 - Production
Start Date                21-APR-2019 23:46:20
Uptime                    0 days 0 hr. 0 min. 51 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora
Listener Log File         /u01/app/oracle/diag/tnslsnr/ocptest/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=ocptest)(PORT=1521)))
Services Summary...
Service "orcl" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
Service "orclXDB" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...

  

 
dbshut验证:
[oracle@ocptest bin]$ dbshut
Processing Database instance "orcl": log file /u01/app/oracle/product/11.2.0/db_1/shutdown.log
[oracle@ocptest bin]$ ps -ef|grep smon
oracle     4369   3254  0 23:48 pts/0    00:00:00 grep smon
[oracle@ocptest bin]$ lsnrctl status
 
 
LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 21-APR-2019 23:48:32
 
 
Copyright (c) 1991, 2009, Oracle.  All rights reserved.
 
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=ocptest)(PORT=1521)))
TNS-12541: TNS:no listener
TNS-12560: TNS:protocol adapter error
  TNS-00511: No listener
   Linux Error: 111: Connection refused

  

 
添加开机自启动:
编辑/etc/rc.d/rc.local添加su oracle -lc /u01/app/oracle/product/11.2.0/db_1/bin/dbstart 到最后一行
[oracle@ocptest ~]$ vi /etc/rc.d/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
 
 
 
 
touch /var/lock/subsys/local
su oracle -lc /u01/app/oracle/product/11.2.0/db_1/bin/dbstart
如果上面没有编辑dbstart和dbshut文件添加侦听器参数:
则还需添加 su oracle -lc "/u01/oracle/bin/lsnrctl start"到/etc/rc.d/rc.local文件中;
 
 
第二种方法:
在oracle家目录创建两个文件:
oracle.start
oracle.stop
编辑:
vi oracle.start
[oracle@ocptest ~]$ vi oracle.start
#!/bin/bash
source /u01/app/oracle/product/11.2.0/db_1
lsnrctl start;
sqlplus / as sysdba <<EOF
startup
EOF

  

vi oracle.stop
[oracle@ocptest ~]$ vi oracle.stop
#!/bin/bash
source /u01/app/oracle/product/11.2.0/db_1
sqlplus / as sysdba <<EOF
startup
EOF
lsnrctl stop;

 

权限:
chmod +x /etc/init.d/oracle
chmod a+x oracle.start oracle.stop
 
单独执行这两个脚本进行验证;
 
通过copy一个network模板
cp /etc/init.d/network /etc/init.d/oracle
[root@ocptest ~]# vi /etc/init.d/oracle
#! /bin/bash
#
# oracle       Bring up/down oracle
#
# chkconfig: 2345 90 1
# description: Activates/Deactivates all oracle configured to \
#              start at boot time.
#
# Source function library.
. /etc/init.d/functions
 
 
# See how we were called.
case "$1" in
  start)
        su - oracle -c "/home/oracle/oracle.start"
        ;;
  stop)
        su - oracle -c "/home/oracle/oracle.stop"
        ;;
  *)
        echo $"Usage: $0 {start|stop}"
        exit 2
esac
chkconfig oracle on
 然后重启服务器测试下,我这里测试都没问题。
 
验证端口:
[oracle@ocptest ~]$ netstat -an|grep :1521
tcp        0      0 192.168.181.2:62747         192.168.181.2:1521          ESTABLISHED
tcp        0      0 :::1521                     :::*                        LISTEN
tcp        0      0 ::ffff:192.168.181.2:1521   ::ffff:192.168.181.2:62747  ESTABLISHED