The srvctl utility can be used to set environment variables for a resource, with the “srvctl setenv” command. These environment variables are then used at resource startup. When setting an environment variable for the database with “srvctl setenv database”, it affects all the instances that belong to that database. To give a concrete example, if you run this command:
$ srvctl setenv database -d mydb -T "TNS_ADMIN=/oracle/102/asm/network/admin"
Then, this setting of TNS_ADMIN will be used when starting ANY instance of that database, regardless of which node you run that command from. To put it another way, there is no concept of the database in a node; database relates to cluster as a node relates to an instance. So the database setting takes effect for all the nodes on which the instances of the database reside.
When you set an environment variable via srvctl, it only affects the startup with srvctl, it does not affect when you start it manually. If you need an environment variable set in manual startup then set it in the shell as per normal, eg:
$ export ORACLE_SID=mydb1 $ export TNS_ADMIN=/etc $ sqlplus "/ as sysdba" SQL> startup open
Effect of setting environment variables through srvctl
Using srvctl to set an environment variable for a database, instance, service or nodeapps has the effect of adding that environment variable to the OCR for that resource. Then, when you use srvctl to start the resource, srvctl fetches the environment variables from the OCR and uses them in the session which starts the resource.
For example, if you use srvctl to set the variable TNS_ADMIN for a database, then the next time you start the database (or any instance of the database), it will behave as though you were starting it from sqlplus with the host environment variable TNS_ADMIN set.
For example:
$ srvctl setenv database -d mydb -T "TNS_ADMIN=/etc" $ srvctl start instance -d mydb -i mydb1
will start the database instance mydb1 with the same TNS_ADMIN setting as if you had run these commands:
$ export ORACLE_SID=mydb1 $ export TNS_ADMIN=/etc $ sqlplus "/ as sysdba" SQL> startup open
However, unlike setting environment variables in the shell, “srvctl setenv” settings are persistent. That is, the environment variables set with srvctl will still be set even after you have logged out, or restarted the node, or restarted the cluster. They are set in the OCR. To clear an environment variable set in the OCR you must use “srvctl unsetenv“.
Viewing all the environment variables set in the OCR
To show all environment variables set for a resource, use srvctl getenv with no arguments, eg.
$ srvctl getenv database -d SPRD
To show the value of particular environment variables that are set for a resource, use -t, eg:
$ srvctl getenv database -d V102 -t "TNS_ADMIN" TNS_ADMIN=/u01/app/oracle/product/10gR2/db/network/admin
What variables can be set with srvctl setenv
Any variable that you could set in the UNIX shell, you can set with srvctl setenv/unsetenv. For example:
$ srvctl setenv database -d V102 -T "AIQNDY389SZ=WQOIUN38" $ srvctl getenv database -d V102 TNS_ADMIN=/u01/app/oracle/product/10gR2/db/network/admin AIQNDY389SZ=WQOIUN38
$ srvctl unsetenv database -d V102 -t "AIQNDY389SZ" $ srvctl getenv database -d V102 TNS_ADMIN=/u01/app/oracle/product/10gR2/db/network/admin
10.2 and 11.1: You can set environment variables with srvctl for databases, instances, services & nodeapps.
11.2: You can set environment variables with srvctl for databases, nodeapps, ASM, VIP &listener.
Syntax of srvctl setenv/unsetenv/getenv commands
It depends on the version.
For 10.2 and 11.1:
Usage: srvctl getenv database -d [name] [-t "[name_list]"] Usage: srvctl getenv instance -d [name] -i [inst_name] [-t "[name_list]"] Usage: srvctl getenv service -d [name] -s [service_name] [-t "[name_list]"] Usage: srvctl getenv nodeapps -n [node_name] [-t "[name_list]"]
Usage: srvctl setenv database -d [name] {-t [name]=[val][,[name]=[val],...] | -T [name]=[val]} Usage: srvctl setenv instance -d [name] [-i [inst_name]] {-t "[name]=[val][,[name]=[val],...]" | -T "[name]=[val]"} Usage: srvctl setenv service -d [name] [-s [service_name]] {-t "[name]=[val][,[name]=[val],...]" | -T "[name]=[val]"} Usage: srvctl setenv nodeapps -n [node_name] {-t "[name]=[val][,[name]=[val],...]" | -T "[name]=[val]"}
Usage: srvctl unsetenv database -d [name] -t "[name_list]" Usage: srvctl unsetenv instance -d [name] [-i [inst_name]] -t "[name_list]" Usage: srvctl unsetenv service -d [name] [-s [service_name]] -t "[name_list]" Usage: srvctl unsetenv nodeapps -n [node_name] -t "[name_list]"
For 11.2:
Usage: srvctl getenv database -d [db_unique_name] [-t "[name_list]"] Usage: srvctl getenv nodeapps [-a] [-g] [-s] [-t "[name_list]"] Usage: srvctl getenv vip -i [vip_name] [-t "[name_list]"] Usage: srvctl getenv listener [-l [lsnr_name]] [-t [name][, ...]] Usage: srvctl getenv asm [-t [name][, ...]]
Usage: srvctl setenv database -d [db_unique_name] {-t [name]=[val][,[name]=[val],...] | -T [name]=[val]} Usage: srvctl setenv nodeapps {-t "[name]=[val][,[name]=[val],...]" | -T "[name]=[val]"} [-v] Usage: srvctl setenv vip -i [vip_name] {-t "[name]=[val][,[name]=[val],...]" | -T "[name]=[val]"} [-v] Usage: srvctl setenv listener [-l [lsnr_name]] -t "[name]=[val] [,...]" | -T "[name]=[value]" Usage: srvctl setenv asm -t "[name]=[val] [,...]" | -T "[name]=[value]"
Usage: srvctl unsetenv database -d [db_unique_name] -t "[name_list]" Usage: srvctl unsetenv nodeapps -t "[name_list]" [-v] Usage: srvctl unsetenv vip -i [vip_name] -t "[name_list]" [-v] Usage: srvctl unsetenv listener [-l [lsnr_name]] -t "[name][, ...]" Usage: srvctl unsetenv asm -t "[name][, ...]"
In the above commands:
-d [name] – Unique name for the database
-i [inst] – Instance name
-t “[name]=[val],…” – Names and values of environment variables
-T “[name]=[val]” – Name and value of a single environment variable
-s [service] – Service name
To print out the syntax for your version, use “srvctl [command] -h”. For example:
$ srvctl setenv -h
To get detailed syntax, use “srvctl [command] [object] -h”. For example:
$ srvctl setenv database -h
Example: Setting, Viewing, and Unsetting TNS_ADMIN for a Database
srvctl is commonly used to set the TNS_ADMIN environment variable for the database. To set TNS_ADMIN for the database “orcl” to /u01/etc:
$ srvctl setenv database -d orcl -T "TNS_ADMIN=/u01/etc"
To show all of the environment variable settings in the OCR for orcl:
$ srvctl getenv database -d orcl orcl: TNS_ADMIN=/u01/etc NLS_LANG=AMERICAN_AMERICA.AL32UTF8
To show only the environment variable TNS_ADMIN for orcl:
$ srvctl getenv database -d orcl -t "TNS_ADMIN" orcl: TNS_ADMIN=/u01/etc
To unset the TNS_ADMIN variable only:
$ srvctl unsetenv database -d orcl -t "TNS_ADMIN"
And then checking the environment variables again shows only TNS_ADMIN was unset by the above command:
$ srvctl getenv database -d orcl orcl: NLS_LANG=AMERICAN_AMERICA.AL32UTF8