使用Server-Side TAF必须配置Service。Service可以在创建数据库时创建,也可以在数据库创建之后修改;既可以通过配置向导也可以通过命令行方式配置。下面演示采用通过命令行手工配置Service的过程。
l 用srvctl命令配置Service
这种方法对于远程维护尤其有用。无论是创建还是维护都使用一个命令srvctl,先来看看有关srvctl命令的语法。
$ srvctl -h --这是我从真实的RAC系统中复制出来的,有时间的可以细细研究一下。
Usage: srvctl [-V]
Usage: srvctl add database -d <name> -o <oracle_home> [-m <domain_name>] [-p <spfile>] [-A <name|ip>/netmask] [-r {PRIMARY | PHYSICAL_STANDBY | LOGICAL_STANDBY}] [-s <start_options>] [-n <db_name>] [-y {AUTOMATIC | MANUAL}]
Usage: srvctl add instance -d <name> -i <inst_name> -n <node_name>
Usage: srvctl add service -d <name> -s <service_name> -r "<preferred_list>" [-a "<available_list>"] [-P <TAF_policy>]
Usage: srvctl add service -d <name> -s <service_name> -u {-r "<new_pref_inst>" | -a "<new_avail_inst>"}
Usage: srvctl add nodeapps -n <node_name> -o <oracle_home> -A <name|ip>/netmask[/if1[|if2|...]]
Usage: srvctl add asm -n <node_name> -i <asm_inst_name> -o <oracle_home> [-p <spfile>]
Usage: srvctl config database
Usage: srvctl config database -d <name> [-a] [-t]
Usage: srvctl config service -d <name> [-s <service_name>] [-a] [-S <level>]
Usage: srvctl config nodeapps -n <node_name> [-a] [-g] [-o] [-s] [-l]
Usage: srvctl config asm -n <node_name>
Usage: srvctl config listener -n <node_name>
Usage: srvctl disable database -d <name>
Usage: srvctl disable instance -d <name> -i "<inst_name_list>"
Usage: srvctl disable service -d <name> -s "<service_name_list>" [-i <inst_name>]
Usage: srvctl disable asm -n <node_name> [-i <inst_name>]
Usage: srvctl enable database -d <name>
Usage: srvctl enable instance -d <name> -i "<inst_name_list>"
Usage: srvctl enable service -d <name> -s "<service_name_list>" [-i <inst_name>]
Usage: srvctl enable asm -n <node_name> [-i <inst_name>]
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 modify database -d <name> [-n <db_name] [-o <ohome>] [-m <domain>] [-p <spfile>] [-r {PRIMARY | PHYSICAL_STANDBY | LOGICAL_STANDBY}] [-s <start_options>] [-y {AUTOMATIC | MANUAL}]
Usage: srvctl modify instance -d <name> -i <inst_name> -n <node_name>
Usage: srvctl modify instance -d <name> -i <inst_name> {-s <asm_inst_name> | -r}
Usage: srvctl modify service -d <name> -s <service_name> -i <old_inst_name> -t <new_inst_name> [-f]
Usage: srvctl modify service -d <name> -s <service_name> -i <avail_inst_name> -r [-f]
Usage: srvctl modify service -d <name> -s <service_name> -n -i <prefered_inst> [-a <available_list>] [-f]
Usage: srvctl modify asm -n <node_name> -i <asm_inst_name> [-o <oracle_home>] [-p <spfile>]
Usage: srvctl relocate service -d <name> -s <service_name> -i <old_inst_name> -t <new_inst_name> [-f]
Usage: srvctl remove database -d <name> [-f]
Usage: srvctl remove instance -d <name> -i <inst_name> [-f]
Usage: srvctl remove service -d <name> -s <service_name> [-i <inst_name>] [-f]
Usage: srvctl remove nodeapps -n "<node_name_list>" [-f]
Usage: srvctl remove asm -n <node_name> [-i <asm_inst_name>] [-f]
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 start database -d <name> [-o <start_options>]
Usage: srvctl start instance -d <name> -i "<inst_name_list>" [-o <start_options>]
Usage: srvctl start service -d <name> [-s "<service_name_list>" [-i <inst_name>]] [-o <start_options>]
Usage: srvctl start nodeapps -n <node_name>
Usage: srvctl start asm -n <node_name> [-i <asm_inst_name>] [-o <start_options>]
Usage: srvctl start listener -n <node_name> [-l <lsnr_name_list>]
Usage: srvctl status database -d <name> [-f] [-v] [-S <level>]
Usage: srvctl status instance -d <name> -i "<inst_name_list>" [-f] [-v] [-S <level>]
Usage: srvctl status service -d <name> [-s "<service_name_list>"] [-f] [-v] [-S <level>]
Usage: srvctl status nodeapps -n <node_name>
Usage: srvctl status asm -n <node_name>
Usage: srvctl stop database -d <name> [-o <stop_options>]
Usage: srvctl stop instance -d <name> -i "<inst_name_list>" [-o <stop_options>]
Usage: srvctl stop service -d <name> [-s "<service_name_list>" [-i <inst_name>]] [-f]
Usage: srvctl stop nodeapps -n <node_name> [-r]
Usage: srvctl stop asm -n <node_name> [-i <asm_inst_name>] [-o <stop_options>]
Usage: srvctl stop listener -n <node_name> [-l <lsnr_name_list>]
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>"
下面介绍一下srvctl命令和Service相关的语法如下。
(1) 创建service
srvctl add service -d <name> -s <service_name> -r "<preferred_list>" [-a "<available_list>"] [-P <TAF_policy>]
其中TAF_policy选项可以为BASIC或者PRECONNECT。例如:
srvctl add service -d RAC -s service2 -r "RAC1,RAC2" -a "RAC3,RAC4"
(2) 查看配置
srvctl config service -d <name> [-s <service_name>] [-a] [-S <level>]
如果不指定“-s service_name”,就会显示所有的service配置,这些配置只包括preferred和available instance。使用-a选项,还会显示TAF的信息。
(3) 是否自动运行service
srvctl enable service -d <name> -s "<service_name_list>" [-i <inst_name>]
srvctl disable service -d <name> -s "<service_name_list>" [-i <inst_name>]
数据库启动时,会自动启动所有的service,有时为了维护的需要,需要禁用这个特性。在维护完成后再启动这个特性。
(4) 启动service
srvctl start service -d <name> [-s "<service_name_list>" [-i <inst_name>]] [-o <start_options>]
如果不指定service_name,则所有的service都会被启动;可以使用逗号分隔方式,同时启动多个service,-i指定在哪个实例上启动service。
(5) 停止service
srvctl stop service -d <name> [-s "<service_name_list>" [-i <inst_name>]] [-f]
启动-f选项可以强制关闭service,并中断其所有的用户连接。
(6) 查看service状态
srvctl status service -d <name> [-s "<service_name_list>"] [-f] [-v] [-S <level>]
其中-f选项可以显示被disable的instance信息,而-v选项表示详细输出。
(7) 删除service
srvctl remove service -d <name> -s <service_name> [-i <inst_name>] [-f]
使用srvctl创建service时,需要注意TAF策略选项必须通过dbms_service包来配置,下面的案例演示如何创建一个名为OLTP的Service。
(1) 添加之前,查看系统现有的Service
$ crs_stat.sh
Name Target State Host
------------------------------ ---------- --------- -------
ora.nodea.ASM1.asm ONLINE ONLINE nodea
ora.nodea.LISTENER_NODEA.lsnr ONLINE ONLINE nodea
ora.nodea.gsd ONLINE ONLINE nodea
ora.nodea.ons ONLINE ONLINE nodea
ora.nodea.vip ONLINE ONLINE nodea
ora.nodeb.ASM2.asm ONLINE ONLINE nodeb
ora.nodeb.LISTENER_NODEB.lsnr ONLINE ONLINE nodeb
ora.nodeb.gsd ONLINE ONLINE nodeb
ora.nodeb.ons ONLINE ONLINE nodeb
ora.nodeb.vip ONLINE ONLINE nodeb
ora.orcl.db ONLINE ONLINE nodea
ora.orcl.orcl1.inst ONLINE ONLINE nodea
ora.orcl.orcl2.inst ONLINE ONLINE nodeb
sys@ORCL> show parameter service
NAME TYPE VALUE
------------------------------------ ----------- -----------------------------
service_names string orcl.saci.com.cn
(2) 使用srvctl命令创建OLTP服务
$ srvctl add service -d orcl -s oltp -r orcl1 -a orcl2 -P basic
(3) 确认服务创建成功,以下黑体部分说明服务已经作为一项CRS Resource注册到CRS中,但是目前还没有启动
$ crs_stat.sh
Name Target State Host
------------------------------ ---------- --------- -------
ora.nodea.ASM1.asm ONLINE ONLINE nodea
ora.nodea.LISTENER_NODEA.lsnr ONLINE ONLINE nodea
ora.nodea.gsd ONLINE ONLINE nodea
ora.nodea.ons ONLINE ONLINE nodea
ora.nodea.vip ONLINE ONLINE nodea
ora.nodeb.ASM2.asm ONLINE ONLINE nodeb
ora.nodeb.LISTENER_NODEB.lsnr ONLINE ONLINE nodeb
ora.nodeb.gsd ONLINE ONLINE nodeb
ora.nodeb.ons ONLINE ONLINE nodeb
ora.nodeb.vip ONLINE ONLINE nodeb
ora.orcl.db ONLINE ONLINE nodea
ora.orcl.oltp.cs OFFLINE OFFLINE
ora.orcl.oltp.orcl1.srv OFFLINE OFFLINE
ora.orcl.orcl1.inst ONLINE ONLINE nodea
ora.orcl.orcl2.inst ONLINE ONLINE nodeb
(4) 配置这个服务自动启动
$ srvctl enable service -d orcl -s oltp
(5) 启动这个服务
$ srvctl start service -d orcl -s oltp
(6) 确认服务状态,状态时ONLINE说明已经启动
$ crs_stat.sh
Name Target State Host
------------------------------ ---------- --------- -------
ora.nodea.ASM1.asm ONLINE ONLINE nodea
ora.nodea.LISTENER_NODEA.lsnr ONLINE ONLINE nodea
ora.nodea.gsd ONLINE ONLINE nodea
ora.nodea.ons ONLINE ONLINE nodea
ora.nodea.vip ONLINE ONLINE nodea
ora.nodeb.ASM2.asm ONLINE ONLINE nodeb
ora.nodeb.LISTENER_NODEB.lsnr ONLINE ONLINE nodeb
ora.nodeb.gsd ONLINE ONLINE nodeb
ora.nodeb.ons ONLINE ONLINE nodeb
ora.nodeb.vip ONLINE ONLINE nodeb
ora.orcl.db ONLINE ONLINE nodea
ora.orcl.oltp.cs ONLINE ONLINE nodea
ora.orcl.oltp.orcl1.srv ONLINE ONLINE nodea
ora.orcl.orcl1.inst ONLINE ONLINE nodea
ora.orcl.orcl2.inst ONLINE ONLINE nodeb
新创建的服务会自动添加到初始化参数中:
sys@ORCL> show parameter service
NAME TYPE VALUE
------------------------------------ ----------- -----------------------------
service_names string oltp
(7) 修改Service的TAF配置,需要使用dbms_service.modify_service
sys@ORCL> begin
2 dbms_service.modify_service (
3 service_name=>'oltp',
4 failover_method=>dbms_service.failover_method_basic,
5 failover_type=>dbms_service.failover_type_select,
6 failover_retries=>180,
7 failover_delay=>5
8 );
9 end;
10 /
PL/SQL procedure successfully completed.
(8) 确认修改已经生效
sys@ORCL> select name,failover_method,failover_type,goal,clb_goal from dba_services;
NAME FAILOVER_M FAILOVER_T GOAL CLB_G
------------------------- ---------- ---------- ------------ -----
oltp BASIC SELECT NONE LONG
(9) 用户也可以使用srvctl命令查看配置
$ srvctl config service -d orcl -s oltp -a
oltp PREF: orcl1 AVAIL: orcl2 TAF: basic
l 配置Service的注意事项
无论通过DBCA图形工具还是srvctl手工方式创建Service,有以下几点需要注意。
a. 数据库的服务名使用service_name参数指定的,一个数据库可以有多个服务名,但是service_name最长4KB,不要手工修改这个参数。
b. 最多可以创建64个Service,每个数据库有两个隐含的Service,因此留给用户的就只有62个service。不能修改这两个隐含Service的配置,并且也不能手工启动或者停止这两个服务。这两个隐含的Service分别是SYS$BACKGROUND和SYS$USERS。
c. 当使用DBCA配置Service时,DBCA会自动更新OCR、启动Service,当删除Service时,会停止Service并更新OCR。
d. 使用srvctl这个工具时,命令只更新OCR中的配置,不会更新data dictionary和listener中的信息;因此还需要使用dbms_service包来更新data dictionary,手工更改listener配置文件。这里推荐使用DBCA工具还配置更改Service配置。
e. 如果客户端想要通过Service方式连接数据库,需要在TNS条目中使用service_name方式引用数据库,见下面例子中的粗体字部分。
SERVICE1 =
(DESCRIPTION =
(LOAD_BALANCE = yes)
(ADDRESS = (PROTOCOL = TCP)(HOST = nodea-vip)(PORT = 1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = nodeb-vip)(PORT = 1521))
(CONNECT_DATA =
(SERVICE_NAME = SERVICE1)
)
)
注意:无论使用DBCA还是srvctl命令来配置Service,都无法配置TAF的type、delay、retries 3个属性,必须使用dbms_service包来修改这些属性。
l 清理环境,删除Service
(1)删除Service之前,需要先关闭Service
$ srvctl stop service -d orcl -s oltp
$ srvctl disable service -d orcl -s oltp
删除服务:
$ srvctl remove service -d orcl -s oltp
oltp PREF: orcl1 AVAIL: orcl2
Service oltp is disabled.
Remove service oltp from the database orcl? (y/[n]) y
OCR中的内容已经被删除了,但是数据字典里还有该Service的内容,继续清除数据字典里的内容。
查看数据字典内容:
sys@ORCL> select name,failover_method,failover_type,goal,clb_goal from dba_services;
NAME FAILOVER_METHOD FAILOVER_TYPE GOAL CLB_G
-------------------- -------------------- -------------------- ------------ ---
SYS$BACKGROUND NONE SHORT
SYS$USERS NONE SHORT
orclXDB LONG
orcl.saci.com.cn LONG
oltp BASIC SELECT NONE LONG
删除数据字典内容:
sys@ORCL> begin
2 dbms_service.delete_service(service_name=>'oltp');
3 end;
4 /
PL/SQL procedure successfully completed.
再次查询数据字典,确认删除干净了。
sys@ORCL> select name,failover_method,failover_type,goal,clb_goal from dba_services;
NAME FAILOVER_METHOD FAILOVER_TYPE GOAL CLB_G
-------------------- -------------------- -------------------- ------------ ---
SYS$BACKGROUND NONE SHORT
SYS$USERS NONE SHORT
orclXDB LONG
orcl.saci.com.cn LONG
现在,已经清除干净。