How to Auto Start Oracle Database in Linux
In this blog post, I will show you how to configure your Oracle database to start automatically when you boot up your Linux server. This is useful if you want to avoid manually starting the database every time you restart the server.
Step 1: Edit the oratab file
The oratab file is located in the /etc directory and contains information about the Oracle databases installed on the server. Each line in the file has the following format:
<database name>:<Oracle home directory>:<auto start option>
The auto start option can be either Y or N, indicating whether the database should be started automatically or not.
To enable auto start for your database, you need to edit the oratab file and change the N to Y at the end of the line. For example, if your database name is ORCL and your Oracle home directory is /u01/app/oracle/product/11.2.0/dbhome_1, you need to change the line from:
ORCL:/u01/app/oracle/product/11.2.0/dbhome_1:N
to:
ORCL:/u01/app/oracle/product/11.2.0/dbhome_1:Y
You can use any text editor to edit the file, such as vi. To edit the file using vi, run the following command as the oracle user:
[oracle@test ~]$ vi /etc/oratab
Note: Before editing the file, make sure you have backed up the original file and checked the location of the database files.
Step 2: Create an init script for Oracle service
The next step is to create an init script that will run the Oracle commands to start and stop the database. The init script is a shell script that is executed when the server changes its run level. The run level is a mode of operation that defines what services and processes are running on the server.
The init script for Oracle service should be placed in the /etc/init.d directory and have the following content:
#!/bin/sh
ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
ORACLE_OWNER=oracle
case "$1" in
'start') # Start the Oracle databases and listeners
su - $ORACLE_OWNER -c "$ORACLE_HOME/bin/dbstart $ORACLE_HOME"
;;
'stop') # Stop the Oracle databases and listeners
su - $ORACLE_OWNER -c "$ORACLE_HOME/bin/dbshut $ORACLE_HOME"
;;
esac
The script defines the ORACLE_HOME and ORACLE_OWNER variables and uses the dbstart and dbshut commands to start and stop the database and the listener. The script takes one argument, which can be either start or stop, and executes the corresponding case.
To create the script, run the following commands as the root user:
[root@test ~]# cd /etc/init.d
[root@test init.d]# vi dbora
Paste the script content and save the file. Then, change the group and permissions of the file as follows:
[root@test init.d]# chgrp dba dbora
[root@test init.d]# chmod 750 dbora
Step 3: Create symbolic links for the init script
The final step is to create symbolic links for the init script in the /etc/rc.d directory. The /etc/rc.d directory contains subdirectories for different run levels, such as rc0.d, rc3.d, rc5.d, etc. Each subdirectory contains symbolic links to the init scripts that should be executed when the server enters that run level.
The symbolic links have the following naming convention:
<action><order><script name>
The action can be either S or K, indicating whether the script should be started or killed. The order is a two-digit number that determines the order of execution. The script name is the name of the init script.
To create the symbolic links for the dbora script, run the following commands as the root user:
[root@test init.d]# ln -s /etc/init.d/dbora /etc/rc.d/rc0.d/K01dbora
[root@test init.d]# ln -s /etc/init.d/dbora /etc/rc.d/rc3.d/S99dbora
[root@test init.d]# ln -s /etc/init.d/dbora /etc/rc.d/rc5.d/S99dbora
This will create the following links:
- /etc/rc.d/rc0.d/K01dbora: This will stop the database when the server enters run level 0, which is the halt mode.
- /etc/rc.d/rc3.d/S99dbora: This will start the database when the server enters run level 3, which is the multi-user mode with networking.
- /etc/rc.d/rc5.d/S99dbora: This will start the database when the server enters run level 5, which is the multi-user mode with networking and graphical user interface.
Step 4: Test the script
To test the script, you can restart the server and check the status of the database. To restart the server, run the following command as the root user:
[root@test init.d]# init 6
This will reboot the server and enter the default run level, which is usually 3 or 5. After the server is up, you can check the status of the database by running the following commands as the oracle user:
[oracle@test ~]$ ps -ef | grep smon | grep -v grep
oracle 3991 1 0 19:20 ? 00:00:00 ora_smon_ORCL
[oracle@test ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Thu May 24 19:21:39 2018
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> select name, open_mode from v$database;
NAME OPEN_MODE
--------- --------------------
ORCL READ WRITE
The output shows that the database is running and open for read and write operations.
I hope this blog post was helpful for you. If you have any questions or feedback, please leave a comment below. Thank you for reading! 😊