在某些特定的情形下,有时候需要从客户端连接到RAC中指定的实例,而不是由客户端Load_balance来动态选择或者是通过服务器端的监听器根据负载情形来转发。对此我们可以通过为tnsnames.ora中特定的网络服务名添加instance_name子项,或者是单独建立一个指向所需实例的网络服务名,下面描述这两种情形。
一、测试环境
-->Oracle 版本
SQL> select * from v$version where rownum<2;
BANNER
----------------------------------------------------------------
Oracle Database 10g Release 10.2.0.3.0 - 64bit Production
-->OS 版本
SQL> ho cat /etc/issue
Welcome to SUSE Linux Enterprise Server 10 SP3 (x86_64) - Kernel \r (\l).
-->RAC 相关信息
SQL> ho /users/oracle/crs_stat.sh
Resource name Target State
-------------- ------ -----
ora.GOBO4.GOBO4A.inst ONLINE ONLINE on bo2dbp
ora.GOBO4.GOBO4B.inst ONLINE ONLINE on bo2dbs
ora.GOBO4.db ONLINE ONLINE on bo2dbp
ora.bo2dbp.ASM1.asm ONLINE ONLINE on bo2dbp
ora.bo2dbp.LISTENER_BO2DBP.lsnr ONLINE ONLINE on bo2dbp
ora.bo2dbp.gsd ONLINE ONLINE on bo2dbp
ora.bo2dbp.ons ONLINE ONLINE on bo2dbp
ora.bo2dbp.vip ONLINE ONLINE on bo2dbp
ora.bo2dbs.ASM2.asm ONLINE ONLINE on bo2dbs
ora.bo2dbs.LISTENER_BO2DBS.lsnr ONLINE ONLINE on bo2dbs
ora.bo2dbs.gsd ONLINE ONLINE on bo2dbs
ora.bo2dbs.ons ONLINE ONLINE on bo2dbs
ora.bo2dbs.vip ONLINE ONLINE on bo2dbs
-->实例GOBO4A上的listener参数
SQL> show parameter instance_name
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
instance_name string GOBO4A
SQL> show parameter listener
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
local_listener string
remote_listener string remote_lsnr_gobo4
-->实例GOBO4B上的listener参数
SQL> show parameter instance_name
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
instance_name string GOBO4B
SQL> show parameter listener
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
local_listener string
remote_listener string remote_lsnr_gobo4
-->服务器端的tnsnames.ora
SQL> ho more $ORACLE_HOME/network/admin/tnsnames.ora
# tnsnames.ora Network Configuration File: /u01/oracle/db/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.
remote_lsnr_gobo4 =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.61)(PORT = 1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.62)(PORT = 1521))
)
-->客户端的tnsnames.ora(客户端为suse 10)
oracle@SZDB:~> tail -10 $ORACLE_HOME/network/admin/tnsnames.ora
GOBO4 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.61)(PORT = 1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.62)(PORT = 1521))
(LOAD_BALANCE = yes)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = GOBO4)
)
)
二、测试登陆到指定实例
1、使用多ip登陆网络服务时,需要增加instance_name项
--为GOBO4网络服务名添加instance_name项,修改之后的情形如下
GOBO4 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.61)(PORT = 1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.62)(PORT = 1521))
(LOAD_BALANCE = yes)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = GOBO4)
(INSTANCE_NAME=GOBO4B) #此项为新增的项
)
)
oracle@SZDB:~> more ./silent_login.sh
#!/bin/bash
sqlplus -S system/oracle@${1} <<EOF
select instance_name from v\$instance;
EOF
exit
oracle@SZDB:~> ./silent_login.sh GOBO4
INSTANCE_NAME
----------------
GOBO4B
oracle@SZDB:~> !!
./silent_login.sh GOBO4
INSTANCE_NAME
----------------
GOBO4B
oracle@SZDB:~> !!
./silent_login.sh GOBO4
INSTANCE_NAME
----------------
GOBO4B
2、为各个instance建立单独的网络连接服务名
--如下,将下面的网络服务名添加到客户端的tnsnames.ora中
oracle@SZDB:~> echo "
> GOBO4A =
> (DESCRIPTION =
> (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.61)(PORT = 1521))
> (CONNECT_DATA =
> (SERVER = DEDICATED)
> (SERVICE_NAME = GOBO4)
> (INSTANCE_NAME = GOBO4A)
> )
> )">>$ORACLE_HOME/network/admin/tnsnames.ora
oracle@SZDB:~> tnsping GOBO4A
Used parameter files:
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.61)(PORT = 1521))
(CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = GOBO4) (INSTANCE_NAME = GOBO4A)))
OK (70 msec)
oracle@SZDB:~> ./silent_login.sh GOBO4A
INSTANCE_NAME
----------------
GOBO4A
oracle@SZDB:~> !!
./silent_login.sh GOBO4A
INSTANCE_NAME
----------------
GOBO4A
oracle@SZDB:~> !!
./silent_login.sh GOBO4A
INSTANCE_NAME
----------------
GOBO4A
三、总结
1、在多ip服务的情形下,可以通过添加INSTANCE_NAME项来指向特定的实例
2、可以为特定的实例单独建立一个网络服务名
3、个人更倾向于使用单独的网络服务名来实现连接到指定的实例
oracle视频教程请关注:http://u.youku.com/user_video/id_UMzAzMjkxMjE2.html