在某些特定的情形下,有時候需要從用戶端串連到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