Linux下设置ORACLE自动启动与关闭
Automating Database Startup and Shutdown on LinuxIf you are using Oracle Clusterware 10gR2 or above for RAC or just for a single instance using ASM, the Clusterware automatically starts and stop
Automating Database Startup and Shutdown on Linux
If you are using Oracle Clusterware 10gR2 or above for RAC or just for a single instance using ASM, the Clusterware automatically starts and stops the Oracle database instances and listeners, so the following procedures are not necessary. Where the Clusterware is not being used, these methods allow you to automate the startup and shutdown of databases on Linux.
- The "su" Command
- The "rsh" Command
- The "runuser" Command
- Known Issues
- Oracle 11gR2+ Update
- Oracle 12c Update
- systemd Services
These methods work on all RHEL and Oracle Linux versions up to and including RHEL7/OL7.
The "su" Command
The following represents the Oracle recommended method for automating database startup and shutdown of Oracle 9i instances on Linux, but it works equally well for Oracle 10g, 11G and 12c also. It can be used on any RHEL-style distribution, including Oracle Linux, up to an including RHEL7.
Once the instance is created, edit the "/etc/oratab" file setting the restart flag for each instance to 'Y'.
TSH1:/u01/app/oracle/product/9.2.0:Y
Create a file called "/etc/init.d/dbora" as the root user, containing the following code.
#!/bin/sh # chkconfig: 345 99 10 # description: Oracle auto start-stop script. # # Set ORA_HOME to be equivalent to the $ORACLE_HOME # from which you wish to execute dbstart and dbshut; # # Set ORA_OWNER to the user id of the owner of the # Oracle database in ORA_HOME. ORA_HOME=/u01/app/oracle/product/9.2.0 ORA_OWNER=oracle if [ ! -f $ORA_HOME/bin/dbstart ] then echo "Oracle startup: cannot start" exit fi 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 - $,设置环境变量 su - $ORA_OWNER -c $ORA_HOME/bin/dbstart & su - $ORA_OWNER -c "$ORA_HOME/bin/lsnrctl start" & 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 "$ORA_HOME/bin/lsnrctl stop" su - $ORA_OWNER -c $ORA_HOME/bin/dbshut rm -f /var/lock/subsys/dbora ;; esac
Use the chmod
command to set the privileges to 750.
chmod 750 /etc/init.d/dbora
Associate the "dbora" service with the appropriate run levels and set it to auto-start using the following command.
chkconfig --add dbora
The relevant instances should now startup/shutdown automatically at system startup/shutdown.
This method can still be used under Oracle 10g, 11g and 12c, provided the "ORA_HOME" variable is amended to use the correct path and this is added to the end of the dbstart
anddbshut
lines. The lines to start and stop the listener can be removed under Oracle 10g Release 2 onward, as the dbstart
command includes an automatic start of the listener.
#!/bin/sh # chkconfig: 345 99 10 # description: Oracle auto start-stop script. # # Set ORA_HOME to be equivalent to the $ORACLE_HOME # from which you wish to execute dbstart and dbshut; # # Set ORA_OWNER to the user id of the owner of the # Oracle database in ORA_HOME. #ORA_HOME=/u01/app/oracle/product/10.2.0/db_1 #ORA_HOME=/u01/app/oracle/product/11.1.0/db_1 #ORA_HOME=/u01/app/oracle/product/11.2.0/db_1 ORA_HOME=/u01/app/oracle/product/12.1.0/db_1 ORA_OWNER=oracle export ORACLE_UNQNAME=db12c if [ ! -f $ORA_HOME/bin/dbstart ] then echo "Oracle startup: cannot start" exit fi 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 "$ORA_HOME/bin/dbstart $ORA_HOME" & 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 "$ORA_HOME/bin/dbshut $ORA_HOME" rm -f /var/lock/subsys/dbora ;; esac
The "rsh" Command
With Oracle 10g, Oracle switched from recommending the "su" command to the "rsh" command. In Oracle 10g release 2, the dbstart
command includes an automatic start of the listener, so there are some differences between the two versions, but the following represents Oracle's preferred method for Oracle 10g.
Once the instance is created, edit the "/etc/oratab" file setting the restart flag for each instance to 'Y'.
TSH1:/u01/app/oracle/product/10.2.0:Y
Create a file called "/etc/init.d/dbora" as the root user, containing the following.
#!/bin/sh # chkconfig: 345 99 10 # description: Oracle auto start-stop script. # # Change the value of ORACLE_HOME to specify the correct Oracle home # directory for your installation. ORACLE_HOME=/u01/app/oracle/product/10.2.0/db_1 # # Change the value of ORACLE to the login name of the # oracle owner at your site. # ORACLE=oracle PATH=${PATH}:$ORACLE_HOME/bin HOST=`hostname` PLATFORM=`uname` export ORACLE_HOME PATH # if [ ! "$2" = "ORA_DB" ] ; then if [ "$PLATFORM" = "HP-UX" ] ; then remsh $HOST -l $ORACLE -n "$0 $1 ORA_DB" exit else rsh $HOST -l $ORACLE $0 $1 ORA_DB exit fi fi # case $1 in 'start') $ORACLE_HOME/bin/dbstart $ORACLE_HOME touch /var/lock/subsys/dbora ;; 'stop') $ORACLE_HOME/bin/dbshut $ORACLE_HOME rm -f /var/lock/subsys/dbora ;; *) echo "usage: $0 {start|stop}" exit ;; esac # exit
Use the chmod
command to set the privileges to 750.
chmod 750 /etc/init.d/dbora
Associate the "dbora" service with the appropriate run levels and set it to auto-start using the following command.
chkconfig --add dbora
The relevant instances should now startup/shutdown automatically at system startup/shutdown.
This method relies on the presence of an RSH server, which requires additional packages and configuration.
# Install the rhs and rsh-server packages from the OS CD/DVD. rpm -Uvh --force rsh-* # Enable rsh and rlogin. chkconfig rsh on chkconfig rlogin on service xinetd reload
This can be quite problematic when attempting to use this method under later Linux distributions, where rsh is deprecated. As a result, I prefer to use the "su" command method.
This method can also be used for 11g databases that are not using ASM or RAC.
The "runuser" Command
The Oracle 12c documentation recommends using the "runuser" command in the "dbora" service.
Once the instance is created, edit the "/etc/oratab" file setting the restart flag for each instance to 'Y'.
DB12C:/u01/app/oracle/product/12.1.0.2/db_1:Y
Create a file called "/etc/init.d/dbora" as the root user, containing the following code, which is a modified version of the example from the documentation, which doesn't work.
#!/bin/sh # chkconfig: 345 99 10 # description: Oracle auto start-stop script. # # Change the value of ORACLE_HOME to specify the correct Oracle home # directory for your installation. ORACLE_HOME=/u01/app/oracle/product/12.1.0.2/db_1 # # Change the value of ORACLE to the login name of the # oracle owner at your site. # ORACLE=oracle PATH=${PATH}:$ORACLE_HOME/bin export ORACLE_HOME PATH # case $1 in 'start') runuser -l $ORACLE -c '$ORACLE_HOME/bin/dbstart $ORACLE_HOME &' touch /var/lock/subsys/dbora ;; 'stop') runuser -l $ORACLE -c '$ORACLE_HOME/bin/dbshut $ORACLE_HOME' rm -f /var/lock/subsys/dbora ;; *) echo "usage: $0 {start|stop}" exit ;; esac # exit
If you want the service to wait while the startup completes, remove the "&". This is especially important for shutdowns that take a long time, like when shutting down WebLogic and Cloud Control services.
Use the chmod
command to set the privileges to 750. Associate the "dbora" service with the appropriate run levels and set it to auto-start using the following command.
chmod 750 /etc/init.d/dbora chkconfig --add dbora
Known Issues
When using Oracle 10g Release 2, calling dbstart
without the "$ORACLE_HOME" might result in the following error message.
Failed to auto-start Oracle Net Listener using /ade/vikrkuma_new/oracle/bin/tnslsnr
This is due to a hard coded path in the dbstart
script. You should not see this error if you pass the "$ORACLE_HOME" as a parameter to dbstart and dbshut. To correct this, edit the "$ORACLE_HOME/bin/dbstart" script and replace the following line (approximately line 78).
ORACLE_HOME_LISTNER=/ade/vikrkuma_new/oracle
With this.
ORACLE_HOME_LISTNER=$ORACLE_HOME
The dbstart
script should now start the listener as expected.
Oracle 11gR2+ Update
Check out the Oracle 12c Update before considering the option.
The Oracle 11gR2 documentation states the use of the dbstart
and dbshut
scripts is deprecated. The preferred replacement is Oracle Restart.
Both dbstart
and dbshut
are still present in Oracle 11gR2 and Oracle 12cR1, so you can continue to use them for now (I still use them). In order to use Oracle Restart you must install Grid Infrastructure (GI), which you will already have if you are using RAC or ASM for a standalone instance. In these cases, Oracle Restart will already be present and running. For single instance databases that don't use ASM, I think it is unreasonable to expect people to install GI, so the following describes a method for those cases, while avoiding dbstart
anddbshut
.
Create a file called "/etc/init.d/dbora" as the root user, containing the following.
#!/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
Use the chmod
command to set the privileges to 750.
chmod 750 /etc/init.d/dbora
Associate the "dbora" service with the appropriate run levels and set it to auto-start using the following command.
chkconfig --add dbora
Next, we must create the "startup.sh" and "shutdown.sh" scripts in the "/home/oracle/scripts". First create the directory.
# mkdir -p /home/oracle/scripts # chown oracle.oinstall /home/oracle/scripts
The "/home/oracle/scripts/startup.sh" script should contain the following commands.
#!/bin/bash export TMP=/tmp export TMPDIR=$TMP export PATH=/usr/sbin:/usr/local/bin:$PATH export ORACLE_HOSTNAME=ol6-121.localdomain export ORACLE_UNQNAME=db12c export ORACLE_SID=db12c ORAENV_ASK=NO . oraenv ORAENV_ASK=YES # Start Listener lsnrctl start # Start Database sqlplus / as sysdba << EOF STARTUP; EXIT; EOF
The "/home/oracle/scripts/shutdown.sh" script is similar.
#!/bin/bash export TMP=/tmp export TMPDIR=$TMP export PATH=/usr/sbin:/usr/local/bin:$PATH export ORACLE_HOSTNAME=ol6-121.localdomain export ORACLE_UNQNAME=db12c export ORACLE_SID=db12c ORAENV_ASK=NO . oraenv ORAENV_ASK=YES # Stop Database sqlplus / as sysdba << EOF SHUTDOWN IMMEDIATE; EXIT; EOF # Stop Listener lsnrctl stop
You could move the environment settings into the "dbora" file or into a separate file that is sourced in the startup and shutdown script. I kept it local to the script so you could see the type of things that need to be set in case you have to write a script to deal with multiple installations, instances and listeners.
Make sure the permissions and ownership of the files is correct.
# chmod u+x /home/oracle/scripts/startup.sh /home/oracle/scripts/shutdown.sh # chown oracle.oinstall /home/oracle/scripts/startup.sh /home/oracle/scripts/shutdown.sh
The listener and database will now start and stop automatically with the machine. You can test them using the following command as the "root" user.
# service dbora start # service dbora stop
更多推荐
所有评论(0)