When working with Oracle Database, configuring environment variables is essential. By leveraging .bash_profile, you can manage settings efficiently and prevent trouble. This article provides detailed explanations and practical advice that will satisfy beginners through advanced users.
1. What is .bash_profile?
.bash_profile is the login-shell configuration file for Bash on Linux. By writing environment variables into this file, they are automatically loaded at shell login, enabling Oracle-related commands to run smoothly.
2. The role and necessity of environment variables
Key environment variables and their meanings
Environment Variable Description ExampleORACLE_SID The system identifier (SID) of the database to connect to. Important when managing multiple DBs. ORCLORACLE_BASE The base directory for Oracle Database. All Oracle products are placed relative to this. /u01/app/oracleORACLE_HOME The directory where Oracle software is installed. /u01/app/oracle/product/19.0.0/dbhome_1PATH Directories searched for executables. Makes binary operations smoother. $ORACLE_HOME/bin:$ORACLE_HOME/OPatch:$PATHNLS_LANG Specifies language, territory, and character set. Essential to prevent mojibake. AMERICAN_AMERICA.AL32UTF8DISPLAY Target display for GUI applications (e.g., OUI). 192.168.56.1:0.0
If these variables are not configured correctly, you may encounter operational errors or performance degradation.
3. How to set environment variables
Step 1: Move to your home directory
First, move to your home directory.
cd ~
Step 2: Edit .bash_profile
Open .bash_profile with vi.
vi .bash_profile
Append the following at the end of the file.
# Oracle environment variable settings
export ORACLE_SID=ORCL
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/19.0.0/dbhome_1
export PATH=$ORACLE_HOME/bin:$ORACLE_HOME/OPatch:$PATH
export NLS_LANG=AMERICAN_AMERICA.AL32UTF8
export DISPLAY=192.168.56.1:0.0
Step 3: Apply the settings
After editing, apply the changes with the following command.
source ~/.bash_profile
4. Verify your settings
Confirm that the settings have been applied correctly.
echo $ORACLE_SID
echo $ORACLE_BASE
echo $ORACLE_HOME
echo $PATH
echo $NLS_LANG
echo $DISPLAY
If each variable returns the expected value, configuration is complete.
5. Practical example: Connect to Oracle using environment variables
Run the following command to connect to the database.
sqlplus / as sysdba
This lets you confirm that the variables set in .bash_profile work correctly.
6. Troubleshooting
Issue 1: Environment variables are not applied
- Cause:
.bash_profilemay not have been loaded. - Solution: Check and apply with:
source ~/.bash_profile
Issue 2: GUI applications do not work
- Cause: Incorrect
DISPLAYsetting. - Solution: Run an X server locally and execute:
xhost +
Issue 3: Mojibake (garbled characters) occurs in SQL*Plus
- Cause:
NLS_LANGis not set appropriately. - Solution: Change it to match your database.
- Example:
export NLS_LANG=AMERICAN_AMERICA.AL32UTF8
- Example:
7. Best practices you should know
Persisting settings
.bash_profile is limited to login shells. To apply the same settings to non-login shells, also write them into .bashrc.
echo "source ~/.bash_profile" >> ~/.bashrc
Don’t forget backups
Incorrectly editing environment variables can cause trouble. Always take a backup before editing.
cp ~/.bash_profile ~/.bash_profile.bak
8. Pro tips
Switching between multiple Oracle versions
If you use multiple Oracle Database versions, it’s recommended to create a script that lets you switch environment variables easily.
Example:
#!/bin/bash
if [ "$1" == "19c" ]; then
export ORACLE_HOME=/u01/app/oracle/product/19.0.0/dbhome_1
elif [ "$1" == "12c" ]; then
export ORACLE_HOME=/u01/app/oracle/product/12.2.0/dbhome_1
fi
export PATH=$ORACLE_HOME/bin:$PATH
echo "Switched to Oracle version $1"
Save the script and run it like this to switch versions:
./switch_oracle.sh 19c
Summary
Using .bash_profile to configure environment variables is indispensable for operating Oracle Database efficiently. Refer to this article to set up accurate and efficient configurations, and be sure to practice backups and troubleshooting.
[reference]
Oracle Database Database Installation Guide, 19c for Linux


コメント