【RAC】RAC中的負載均衡和故障切換--TAF配置

lhrbest發表於2017-07-15

【RAC】RAC中的負載均衡和故障切換--TAF配置




涉及到的內容包括:





 

Oracle RAC 客戶端連線負載均衡(Load Balance)


     實現負載均衡(Load Balance)是Oracle RAC最重要的特性之一,主要是把負載平均分配到叢集中的各個節點,以提高系統的整體吞吐能力。通常情況下有兩種方式來實現負載均衡,一個是基於客戶端連線的負載均衡,一個是基於伺服器端監聽器(Listener)收集到的資訊來將新的連線請求分配到連線數較少例項上的實現方式。本文主要討論的是基於客戶端連線的負載均衡,並給出演示。

        與負載均衡配置之前的監聽配置請參考
              ORACLE RAC 監聽配置 (listener.ora tnsnames.ora) 
              ORACLE RAC 下非預設埠監聽配置(listener.ora tnsnames.ora)

 

一、客戶端的負載均衡
         客戶端的負載均衡主要是透過為tnsnames.ora增加load_balance=yes條目來實現,下面看看oracle(Note:226880.1)的解釋

        The client load balancing feature enables clients to randomize connection requests among the listeners. Oracle Net progresses through the list of
        protocol addresses in a random sequence, balancing the load on the various listeners. Without client load balancing, Oracle Net progresses through the
        list of protocol addresses sequentially until one succeeds.  This normally is referred to connect-time load balance.

        從上面的描述中可以得知,如果未開啟load_balance=yes時,Oracle Net會根據地址列表按順序來選擇一個進行連線,直到連線成功為止。   
        如果第一個host主機連線失敗,在有多個地址的情形下,接下來選擇第二個地址連線,依此類推,直到連線成功為止。
        當開啟了load_balance=yes時,則Oracle Net會從多個地址中隨機地選擇一個地址進行連線,直到連線成功為止。
        注意,此連線方式僅根據地址列表隨機選擇,並不考慮到各個例項上當前真正連線數量的多少,也即是沒有考慮各個節點真實的連線負載情況。

 

二、伺服器與客戶端的配置情況

[sql] view plain copy
 print?
  1. 1、伺服器端監聽器配置  
  2. oracle@bo2dbp:~> more $ORACLE_HOME/network/admin/listener.ora  --#節點bo2dbp上的listener  
  3. # listener.ora.bo2dbp Network Configuration File: /u01/oracle/db/network/admin/listener.ora.bo2dbp  
  4. # Generated by Oracle configuration tools.  
  5.   
  6. LISTENER_NEW_BO2DBP =  
  7.   (DESCRIPTION_LIST =  
  8.     (DESCRIPTION =  
  9.       (ADDRESS = (PROTOCOL = TCP)(HOST = bo2dbp-vip.2gotrade.com)(PORT = 1314)(IP = FIRST))  
  10.       (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.51)(PORT = 1314)(IP = FIRST))  
  11.     )  
  12.   )  
  13. ...........................--#其餘部分省略,注意此處的配置是使用了1314的非預設監聽器埠號     
  14.    
  15. oracle@bo2dbs:/u01/oracle/db/network/admin> more listener.ora  --#節點bo2dbs上的listener  
  16. # listener.ora.bo2dbs Network Configuration File: /u01/oracle/db/network/admin/listener.ora.bo2dbs  
  17. # Generated by Oracle configuration tools.  
  18.   
  19. LISTENER_NEW_BO2DBS =  
  20.   (DESCRIPTION_LIST =  
  21.     (DESCRIPTION =  
  22.       (ADDRESS = (PROTOCOL = TCP)(HOST = bo2dbs-vip.2gotrade.com)(PORT = 1314)(IP = FIRST))  
  23.       (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.52)(PORT = 1314)(IP = FIRST))  
  24.     )  
  25.   )  
  26. ...............  
  27.   
  28. 2、引數配置  
  29. -->instrance ora10g1上的引數配置  
  30. SQL> show parameter instance_na  
  31.   
  32. NAME                                 TYPE        VALUE  
  33. ------------------------------------ ----------- ------------------------------  
  34. instance_name                        string      ora10g1  
  35.   
  36. SQL> show parameter listener  
  37.   
  38. NAME                                 TYPE        VALUE  
  39. ------------------------------------ ----------- ------------------------------  
  40. local_listener                       string      local_lsnr_ora10g1  
  41. remote_listener                      string      remote_lsnr_ora10g  
  42.   
  43. -->instrance ora10g2上的引數配置  
  44. SQL> show parameter instance_name  
  45.   
  46. NAME                                 TYPE        VALUE  
  47. ------------------------------------ ----------- ------------------------------  
  48. instance_name                        string      ora10g2  
  49.   
  50. SQL> show parameter listener  
  51.   
  52. NAME                                 TYPE        VALUE  
  53. ------------------------------------ ----------- ------------------------------  
  54. local_listener                       string      local_lsnr_ora10g2  
  55. remote_listener                      string      remote_lsnr_ora10g  
  56.   
  57. -->instrance ora10g2上的監聽資訊  
  58. SQL> ho ps -ef | grep lsnr  
  59. oracle   17372     1  0 11:00 ?        00:00:00 /u01/oracle/db/bin/tnslsnr LISTENER_NEW_BO2DBS -inherit  
  60. oracle   17502 24301  0 12:10 pts/0    00:00:00 /bin/bash -c ps -ef | grep lsnr  
  61. oracle   17504 17502  0 12:10 pts/0    00:00:00 grep lsnr  
  62.   
  63. SQL> ho lsnrctl status LISTENER_NEW_BO2DBS  
  64. Listening Endpoints Summary...  
  65.   (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.7.62)(PORT=1314)))  
  66.   (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.7.52)(PORT=1314)))  
  67. Services Summary...  
  68. Service "ora10g" has 2 instance(s).  
  69.   Instance "ora10g1", status READY, has 1 handler(s) for this service...  
  70.   Instance "ora10g2", status READY, has 2 handler(s) for this service...  
  71. ..........  
  72.     
  73. -->由於只是測試基於客戶端的load balance,因此移出remote_listener引數  
  74. SQL> alter system reset remote_listener scope=both sid='*';  
  75. alter system reset remote_listener scope=both sid='*'  
  76. *  
  77. ERROR at line 1:  
  78. ORA-32009: cannot reset the memory value for instance * from instance ora10g2  
  79.   
  80. SQL> alter system reset remote_listener scope=spfile sid='*';  
  81.   
  82. System altered.  
  83.   
  84. SQL> ho srvctl stop database -d ora10g  -->關閉資料庫ora10g  
  85.   
  86. SQL> ho srvctl start database -d ora10g  -->啟動資料庫ora10g使得剛剛修改的remote_listener生效  
  87.   
  88. SQL> ho lsnrctl status LISTENER_NEW_BO2DBS  -->此時可以看到只有ora10g2註冊到監聽器  
  89.   
  90. Listening Endpoints Summary...  
  91.   (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.7.62)(PORT=1314)))  
  92.   (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.7.52)(PORT=1314)))  
  93. Services Summary...  
  94. Service "PLSExtProc" has 1 instance(s).  
  95.   Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...  
  96. Service "ora10g" has 1 instance(s).  
  97.   Instance "ora10g2", status READY, has 1 handler(s) for this service...  
  98. ..........    
  99.   
  100. oracle@bo2dbp:~> lsnrctl status LISTENER_NEW_BO2DBP  #同樣在節點bo2dbp也只有ora10g1註冊到監聽器  
  101.   
  102. Listening Endpoints Summary...  
  103.   (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.7.61)(PORT=1314)))  
  104.   (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.7.51)(PORT=1314)))  
  105. Services Summary...  
  106. Service "ora10g" has 1 instance(s).  
  107.   Instance "ora10g1", status READY, has 1 handler(s) for this service...  
  108. .........  
  109.   
  110. 3、客戶端配置  
  111. SZDB:~ # ifconfig eth1 | grep "inet addr"|cut -d " " -f12|cut -d : -f2  #客戶端主機的ip  
  112. 192.168.7.2  
  113.   
  114. SZDB:~ # su - oracle  
  115. oracle@SZDB:~> cat /etc/hosts     --#客戶端主機新增了RAC上的兩個虛擬節點的ip資訊  
  116. 192.168.7.61   bo2dbp-vip.2gotrade.com    bo2dbp-vip  
  117. 192.168.7.62   bo2dbs-vip.2gotrade.com    bo2dbs-vip  
  118.   
  119. oracle@SZDB:~> tail -12 $ORACLE_HOME/network/admin/tnsnames.ora  
  120.   
  121. ORA10G =  
  122.   (DESCRIPTION =  
  123.     (ADDRESS = (PROTOCOL = TCP)(HOST = bo2dbp-vip.2gotrade.com)(PORT = 1314))  
  124.     (ADDRESS = (PROTOCOL = TCP)(HOST = bo2dbs-vip.2gotrade.com)(PORT = 1314))  
  125.     (LOAD_BALANCE = yes)  
  126.     (CONNECT_DATA =  
  127.       (SERVER = DEDICATED)  
  128.       (SERVICE_NAME = ora10g)  
  129.     )  
  130.   )  

三、測試負載均衡(load balance)

[sql] view plain copy
 print?
  1. 1、啟用load balance的測試  
  2.    
  3. oracle@SZDB:~> more load_balance.sh   
  4. #!/bin/bash  
  5. for i in {1..100}  
  6. do  
  7. echo $i  
  8. sqlplus -S system/oracle@ORA10G <<EOF  
  9. select instance_name from v\$instance;  
  10. EOF  
  11. sleep 1  
  12. done  
  13. exit 0    
  14.   
  15. # Author: Robinson Cheng  
  16. # Blog :  http://blog.csdn.net/robinson_0612    
  17. oracle@SZDB:~> ./load_balance.sh >load_bal.log  
  18. oracle@SZDB:~> head -20 load_bal.log  
  19. 1  
  20.   
  21. INSTANCE_NAME  
  22. ----------------  
  23. ora10g2  
  24.   
  25. 2  
  26.   
  27. INSTANCE_NAME  
  28. ----------------  
  29. ora10g1  
  30.   
  31. 3  
  32.   
  33. INSTANCE_NAME  
  34. ----------------  
  35. ora10g2  
  36.   
  37. 4  
  38.   
  39. oracle@SZDB:~> grep ora10g1 load_bal.log |wc -l  
  40. 47  
  41. oracle@SZDB:~> grep ora10g2 load_bal.log |wc -l  
  42. 53  
  43.   
  44. 從上面的log日誌中可以看出啟用客戶端的負載均衡基本上使得從客戶端發起連線的能夠保持均衡。  
  45.     
  46. 2、未啟用load balance的測試    
  47. 從客戶端的tnsnames.ora中移出(LOAD_BALANCE = yes)選項,然後繼續使用上面的指令碼來測試  
  48. oracle@SZDB:~> grep ora10g1 no_load_bal.log |wc -l  
  49. 100  
  50. oracle@SZDB:~> grep ora10g2 no_load_bal.log |wc -l  
  51. 0  
  52.   
  53. 從上面的日誌中可以看出當移出LOAD_BALANCE = yes項後,所有的使用者連線請求都被定為到ora10g1,這是因為連線請求從tnsnames.ora中選擇  
  54. 列在ADDRESS項中排在第一行的位置。  
  55. 下面我們關閉例項ora10g1,再來測試連線情形  
  56.   
  57. oracle@bo2dbp:~> srvctl stop instance -d ora10g -i ora10g1  
  58.   
  59. oracle@SZDB:~> ./load_balance.sh >no_load_bal_new.log  
  60. oracle@SZDB:~> grep ora10g1 no_load_bal_new.log |wc -l  
  61. 0  
  62. oracle@SZDB:~> grep ora10g2 no_load_bal_new.log |wc -l  
  63. 100    
  64.   
  65. 由於例項ora10g1已經關閉,因此所有的連線請求都被分配到ora10g2。  

 四、總結  
1、客戶端的負載均衡配置較為簡單,僅僅是在客戶端的tnsnames.ora新增 LOAD_BALANCE = yes |on 
2、其連線分配原則是根據tnsnames.ora中連線識別符號下的ADDRESS列表隨機選擇來進行與伺服器之間的連線
3、如果選擇列表中的某個節點listener或instance不可用,則再從剩餘的ADDRESS列表隨機選擇,直到成功為止





Oracle RAC 伺服器端連線負載均衡(Load Balance)




  Oracle RAC伺服器端的負載均衡是根據RAC中各節點的連線負荷數情況,將新的連線請求分配到負荷最小的節點上去。當資料庫處於執行時,RAC中各節點的PMON程式每3秒會將各自節點的連線負荷數更新到service_register。而對於節點中任意監聽器故障或監聽器意外失敗時,PMON程式會每1秒鐘檢查當前節點上的監聽是否重啟,以獲得最新的負載資訊來及時調整負載均衡。本文主要演示suse 10 + oracle 10g rac下的伺服器端的負載均衡。

 

有關客戶端的負載均衡可參考
      Oracle RAC 客戶端連線負載均衡(Load Balance)
      配置RAC負載均衡與故障轉移
 
有關Oracle 網路配置相關基礎以及概念性的問題請參考:
        配置ORACLE 客戶端連線到資料庫 
        配置非預設埠的動態服務註冊 
        配置sqlnet.ora限制IP訪問Oracle 
       Oracle 監聽器日誌配置與管理 
       設定 Oracle 監聽器密碼(LISTENER)
       Oracle RAC 監聽配置
       ORACLE RAC 下非預設埠監聽配置(listener.ora tnsnames.ora)

 

一、伺服器端負載均衡配置

[sql] view plain copy
 print?
  1. 1、為tnsnames.ora 新增相應的網路服務名(每個節點配置)  
  2.   oracle@bo2dbp:~> more $ORACLE_HOME/network/admin/tnsnames.ora  
  3.   # tnsnames.ora Network Configuration File: /u01/oracle/db/network/admin/tnsnames.ora  
  4.   # Generated by Oracle configuration tools.  
  5.     
  6.   remote_lsnr_gobo4 =  
  7.     (ADDRESS_LIST =  
  8.       (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.61)(PORT = 1521))  
  9.       (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.62)(PORT = 1521))  
  10.     )  
  11.     
  12.   local_lsnr_gobo4a =  
  13.     (ADDRESS_LIST =  
  14.       (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.61)(PORT = 1521))  
  15.     )  
  16.     
  17.   local_lsnr_gobo4b =  
  18.     (ADDRESS_LIST =  
  19.       (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.62)(PORT = 1521))  
  20.     )  
  21.       
  22. 2、設定remote_listener引數  
  23.   alter system set remote_listener='<net_service_name>' scope=both sid='*';  
  24.     
  25.   SQL> show parameter instance_name  
  26.     
  27.   NAME                                 TYPE        VALUE  
  28.   ------------------------------------ ----------- ------------------------------  
  29.   instance_name                        string      GOBO4A  
  30.   SQL> show parameter listener  
  31.     
  32.   NAME                                 TYPE        VALUE  
  33.   ------------------------------------ ----------- ------------------------------  
  34.   local_listener                       string  
  35.   remote_listener                      string  
  36.     
  37.   SQL> alter system set remote_listener='remote_lsnr_gobo4' scope=both sid='*';  
  38.     
  39.   System altered.  
  40.   
  41. 3、配置客戶端tnsnames.ora  
  42.   -->客戶端為suse 10  
  43.   SZDB:~ # ifconfig eth1 | grep "inet addr"|cut -d " " -f12|cut -d : -f2  #--客戶端主機的ip  
  44.   192.168.7.2  
  45.   SZDB:~ # su - oracle  
  46.   oracle@SZDB:~> tail -10 $ORACLE_HOME/network/admin/tnsnames.ora  
  47.   GOBO4 =  
  48.     (DESCRIPTION =  
  49.       (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.61)(PORT = 1521))  
  50.       (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.62)(PORT = 1521))  
  51.       (LOAD_BALANCE = off)   #--由於僅僅測試寄予伺服器端的負載均衡,因此關閉客戶端負載均衡選項  
  52.       (CONNECT_DATA =  
  53.         (SERVER = DEDICATED)  
  54.         (SERVICE_NAME = GOBO4)  
  55.       )  
  56.     )  
  57.   #Author : Robinson Cheng  
  58.   #Blog :   http://blog.csdn.net/robinson_0612    
  59.    
  60. 4、檢查監聽情況  
  61.   oracle@bo2dbp:~> lsnrctl status  
  62.   Listening Endpoints Summary...  
  63.     (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.7.61)(PORT=1521)))  
  64.     (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.7.51)(PORT=1521)))  
  65.   Services Summary...  
  66.   Service "+ASM" has 1 instance(s).  
  67.     Instance "+ASM1", status BLOCKED, has 1 handler(s) for this service...  
  68.   Service "GOBO4" has 2 instance(s).  
  69.     Instance "GOBO4A", status READY, has 2 handler(s) for this service...  
  70.     Instance "GOBO4B", status READY, has 1 handler(s) for this service..  
  71.   .......  
  72.     
  73.   oracle@bo2dbs:~> lsnrctl status  
  74.   Listening Endpoints Summary...  
  75.     (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.7.62)(PORT=1521)))  
  76.     (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.7.52)(PORT=1521)))  
  77.   Services Summary...  
  78.   Service "+ASM" has 1 instance(s).  
  79.     Instance "+ASM2", status BLOCKED, has 1 handler(s) for this service...  
  80.   Service "GOBO4" has 2 instance(s).  
  81.     Instance "GOBO4A", status READY, has 1 handler(s) for this service...  
  82.     Instance "GOBO4B", status READY, has 2 handler(s) for this service...  
  83.   ..........  
  84.   
  85.   #--如果監聽或資料庫需要重啟異常請考慮重新啟動監聽器或資料庫  
  86.   #--下面清空監聽日誌以便於後續統計連線資訊  
  87.   oracle@bo2dbp:/u01/oracle/db/network/log> cat /dev/null>listener_bo2dbp.log  
  88.   oracle@bo2dbs:/u01/oracle/db/network/log> cat /dev/null>listener_bo2dbs.log  

  二、測試伺服器端的負載均衡

[python] view plain copy
 print?
  1. 1、從客戶端建立連線  
  2.   oracle@SZDB:~> more load_balance.sh  
  3.   #!/bin/bash  
  4.   for i in {1..1000}  
  5.   do  
  6.   echo $i  
  7.   sqlplus -S system/oracle@GOBO4 <<EOF  
  8.   select instance_name from v\$instance;  
  9.   EOF  
  10.   sleep 1  
  11.   done  
  12.   exit 0  
  13.   oracle@SZDB:~> ./load_balance.sh >srv_load_bal.log  
  14.   
  15. 2、分析監聽日誌  
  16.   oracle@bo2dbp:/u01/oracle/db/network/log> more listener_bo2dbp.log  
  17.   12-OCT-2012 12:00:10 * service_update * GOBO4B * 0   #節點bo2dbs上的例項GOBO4B的更新到bo2dbp上監聽器的更新資訊  
  18.   12-OCT-2012 12:00:35 * service_update * GOBO4B * 0  
  19.   12-OCT-2012 12:01:04 * (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=GOBO4)(CID=(PROGRAM=sqlplus@SZDB)(HOST=SZDB)  
  20.   (USER=oracle))) * (ADDRESS=(PROTOCOL=tcp)(HOST=192.168.7.2)(PORT=50322)) * establish * GOBO4 * 0  
  21.   12-OCT-2012 12:01:05 * (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=GOBO4)(CID=(PROGRAM=sqlplus@SZDB)(HOST=SZDB)  
  22.   (USER=oracle))) * (ADDRESS=(PROTOCOL=tcp)(HOST=192.168.7.2)(PORT=50325)) * establish * GOBO4 * 0  
  23.   12-OCT-2012 12:01:07 * (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=GOBO4)(CID=(PROGRAM=sqlplus@SZDB)(HOST=SZDB)  
  24.   (USER=oracle))) * (ADDRESS=(PROTOCOL=tcp)(HOST=192.168.7.2)(PORT=50328)) * establish * GOBO4 * 0  
  25.   12-OCT-2012 12:01:08 * (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=GOBO4)(CID=(PROGRAM=sqlplus@SZDB)(HOST=SZDB)  
  26.   (USER=oracle))) * (ADDRESS=(PROTOCOL=tcp)(HOST=192.168.7.2)(PORT=50330)) * establish * GOBO4 * 0  
  27.   
  28.   #上面的日誌片斷中可以看出全部是客戶端發起的到bo2dbp節點上的建立連線的資訊  
  29.     
  30.   #下面來檢視bo2dbs上的監聽日誌  
  31.   oracle@bo2dbs:/u01/oracle/db/network/log> more listener_bo2dbs.log  
  32.   12-OCT-2012 12:00:10 * service_update * GOBO4B * 0  
  33.   12-OCT-2012 12:00:10 * service_update * GOBO4B * 0  
  34.   12-OCT-2012 12:00:14 * (CONNECT_DATA=(CID=(PROGRAM=)(HOST=bo2dbs)(USER=oracle))(COMMAND=status)(ARGUMENTS=64)  
  35.   (SERVICE=LISTENER_BO2DBS)(VERSION=169870080)) * status * 0  
  36.   12-OCT-2012 12:00:35 * service_update * GOBO4B * 0  
  37.   12-OCT-2012 12:00:35 * service_update * GOBO4B * 0  
  38.   12-OCT-2012 12:01:04 * (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=GOBO4)(CID=(PROGRAM=sqlplus@SZDB)(HOST=SZDB)  
  39.   (USER=oracle))(INSTANCE_NAME=GOBO4B)) * (ADDRESS=(PROTOCOL=tcp)(HOST=192.168.7.2)(PORT=61862)) * establish * GOBO4 * 0  
  40.   12-OCT-2012 12:01:07 * (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=GOBO4)(CID=(PROGRAM=sqlplus@SZDB)(HOST=SZDB)  
  41.   (USER=oracle))(INSTANCE_NAME=GOBO4B)) * (ADDRESS=(PROTOCOL=tcp)(HOST=192.168.7.2)(PORT=61868)) * establish * GOBO4 * 0  
  42.   12-OCT-2012 12:01:09 * (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=GOBO4)(CID=(PROGRAM=sqlplus@SZDB)(HOST=SZDB)  
  43.   (USER=oracle))(INSTANCE_NAME=GOBO4B)) * (ADDRESS=(PROTOCOL=tcp)(HOST=192.168.7.2)(PORT=61872)) * establish * GOBO4 * 0  
  44.     
  45.   #在12-OCT-2012 12:01:04時刻,連線資訊中有INSTANCE_NAME=GOBO4B的連線資訊,而節點bo2dbp上也有一條類似的資訊,因此該條連線  
  46.   #日誌是由節點bo2dbp轉發過來而建立的連線請求。  
  47.   #同樣在12-OCT-2012 12:01:07時刻,節點bo2dbp轉發過來而建立的連線請求。  
  48.     
  49.   #小結一下,  
  50.   #對於直接連線,監聽器日誌中將出現establish,且不含有INSTANCE_NAME=GOBO4B 字樣  
  51.   #而對於轉發的連線,則轉發節點與接收的節點同時存在連線資訊,轉發節點上存在連線資訊的與普通的連線請求一樣,  
  52.   #而接收的節點上存在INSTANCE_NAME=<instance_name> 資訊  
  53.   
  54. 3、檢查負載均衡結果  
  55.   oracle@SZDB:~> grep GOBO4A srv_load_bal.log |wc -l  
  56.   755  
  57.   oracle@SZDB:~> grep GOBO4B srv_load_bal.log |wc -l  
  58.   245  
  59.   #從上面的日誌檔案中可知總共有755個客戶端連線到了gobo4a,有245各客戶端連線到了gobo4b  
  60.     
  61.   #下面檢視監聽器日誌來獲得連線資訊  
  62.   #下面的查詢中在節點bo2dbp上總共有接受了1000個使用者連線  
  63.   oracle@bo2dbp:/u01/oracle/db/network/log> grep establish listener_bo2dbp.log |wc -l  
  64.   1000  
  65.     
  66.   #下面的查詢檢視是否有從節點bo2dbs轉發過來的連線,結果為0,說明沒有任何連線請求從bo2dbs轉發過來  
  67.   oracle@bo2dbp:/u01/oracle/db/network/log> grep INSTANCE_NAME=GOBO4 listener_bo2dbp.log |wc -l  
  68.   0  
  69.     
  70.   #接下來檢視節點bo2dbs的監聽日誌,可以看出總共接受了245個連線請求  
  71.   oracle@bo2dbs:/u01/oracle/db/network/log> grep establish listener_bo2dbs.log |wc -l  
  72.   245  
  73.     
  74.   #下面的過濾情況也表明在節點bo2dbs上的連線是從bo2dbp上轉發的連線,而非客戶端直接到bo2dbs的請求連線  
  75.   oracle@bo2dbs:/u01/oracle/db/network/log> grep INSTANCE_NAME=GOBO4 listener_bo2dbs.log |wc -l  
  76.   245  
  77.     
  78.   #從監聽器的日誌檢查可以,測試中的連線全部請求到節點bo2dbp,是由於tnsnames.ora中ADDRESS的第一個IP地址就是bo2dbp的IP  
  79.   #因此所有的連線都是請求到bo2dbp,而沒有客戶端發出到bo2dbs的連線請求  
  80.   #其次是儘管在bo2dbp有1000個連線請求,而真正建立連線的只有755個,有245轉發到了節點bo2dbs  

三、總結
      1、伺服器端的負載均衡需要配置remote_listener引數,而該引數的值依賴於tnsnames.ora的連線字串
      2、對於基於伺服器端的連線負載均衡,監聽器會根據當前節點、例項上的連線負載情況進行轉發到空閒的例項
      3、轉發的依據僅僅是當前節點監聽的連線數量的多少,而非當前例項的過度負載
      4、從上面的測試可以得出,各個節點的連線並不算均衡,是相對的均衡,因此應結合客戶端連線負載協同工作
      5、對於當前例項的過度負載的情形,應結合配置service方法來實現負載均衡  




配置 RAC 負載均衡與故障轉移

  Oracle負載均衡主要是指新會話連線到RAC資料庫時,如何判定這個新的連線要連到哪個節點進行工作?通常情況下,負載均衡分為客戶端負載
均衡與伺服器端負載均衡。客戶端負載均衡通常是在客戶端的tnsnames.ora中多新增一個連結地址以及LOAD_BALANCE與failover引數。而伺服器
端的負載均衡則相對複雜,下面具體描述伺服器端負載均衡。
    
一、負載均衡
    注意這裡的負載均衡指的是連線的負載均衡,即客戶可以隨機從不同的例項中連線到資料庫
    1.配置tnsnames.ora使得該檔案中包含如下全部內容:       
[sql] view plain copy
 print?
  1. # LISTENERS_DEVDB DEVDB是資料庫名,可以使用netmgr,netca編輯或直接使用Vim建立  
  2. LISTENERS_DEVDB =                                                             
  3.   (ADDRESS_LIST =                                                             
  4.     (ADDRESS = (PROTOCOL = TCP)(HOST = rac1-vip.robinson.com)(PORT = 1521))     
  5.     (ADDRESS = (PROTOCOL = TCP)(HOST = rac2-vip.robinson.com)(PORT = 1521))     
  6.   )                                                                           
    2.配置引數檔案remote_listener          
[sql] view plain copy
 print?
  1. SQL> alter system set remote_listener='LISTENERS_DEVDB' scope=both sid='*';   
    3.需要配置連線描述資訊的兩個IP地址、埠號、以及load_balance子項為yes (主要是load_balance子項)      
[sql] view plain copy
 print?
  1. DEVDB =                                                                         
  2.   (DESCRIPTION =                                                                
  3.     (ADDRESS = (PROTOCOL = TCP)(HOST = rac1-vip.robinson.com)(PORT = 1521))       
  4.     (ADDRESS = (PROTOCOL = TCP)(HOST = rac2-vip.robinson.com)(PORT = 1521))       
  5.     (LOAD_BALANCE = yes)                                                          
  6.     (CONNECT_DATA =                                                               
  7.       (SERVER = DEDICATED)                                                        
  8.       (SERVICE_NAME = devdb.robinson.com)                                         
  9.     )                                                                             
  10.   )                                                                             
    4.檢視偵聽器的狀態,從下面可以看到devdb.robinson.com服務中有兩個例項為其提供服務     
[sql] view plain copy
 print?
  1. [oracle@rac2 ~]$ lsnrctl status                                                  
  2.     Service "devdb.robinson.com" has 2 instance(s).                                
  3.       Instance "devdb1", status READY, has 1 handler(s) for this service...        
  4.       Instance "devdb2", status READY, has 2 handler(s) for this service...        
    5.測試負載均衡
    使用shell指令碼來進行測試負載均衡      
[sql] view plain copy
 print?
  1. --編輯TestLoadBalance.sh                                                                            
  2.     #!/bin/bash                                                                                       
  3.     #Usage: TestLoadBalance devdb 1000                                                                
  4.     count=0                                                                                           
  5.     while [ $count -lt $2 ]   # Set up a loop control                                                 
  6.     do                        # Begin the loop                                                        
  7.         count='expr $count + 1' # Increment the counter                                                 
  8.         sqlplus -s usr1/usr1pwd@$1 @TestLoadBalance.sql  # Connect instance and execute sql statement   
  9.         sleep 1                                                                                         
  10.     done                                                                                              
  11.                                                                                                     
  12. --TestLoadBalance.sql 指令碼                                                                          
  13.     col instance_name format a30                                                                      
  14.     select instance_name from v$instance;                                                             
  15.                                                                                                     
  16. --實施測試                                                                                          
  17.     ./TestLoadBalance.sh devdb 1000                                                                   
  18.                                                                                                       
  19. --檢視結果                                                                                          
  20.     SQL> select inst_id,count(1) from gv$instance group by inst_d;                                                    
  21.                                                                                                       
  22.     INST_ID       COUNT(1)                                                                            
  23.     ----------    ----------                                                                          
  24.     devdb1        446                                                                                 
  25.     devdb2        554                                                                                 
二、配置故障轉移
    負載均衡是用於實現基於連線的負載均衡,但不能解決節點是否可用,一旦一個節點損壞,已成功連線的客戶端並不能轉移到其他正常服務的
    例項中。而故障轉移功能則使得該功能得以實現。可以使用srvctl 和dbca來建立服務。下面使用dbca來建立一個新的服務,客戶端連線到實
    例後,對故障實現透明切換。
    
    1.配置故障轉移服務
        在節點rac1使用oracle帳戶啟動dbca工具,
            a.選擇 Oracle Real Application Clusters database
            b.選擇 Services Management
            c.叢集資料庫列表:單擊 Next。 
            d.資料庫服務:單擊 Add。 
                新增服務:輸入sales。 
                選擇 devdb1 作為首選例項。 
                選擇 devdb2 作為可用例項。 
                TAF 策略:選擇 Basic。 
                單擊 Finish。
            e.資料庫配置助手:單擊 No 退出。
        配置完畢後,在tnsnames.ora中多出了以下內容,注意是各個節點都會多出以下內容            
[sql] view plain copy
 print?
  1. SALES =                                                                      
  2.   (DESCRIPTION =                                                             
  3.     (ADDRESS = (PROTOCOL = TCP)(HOST = rac1-vip.robinson.com)(PORT = 1521))    
  4.     (ADDRESS = (PROTOCOL = TCP)(HOST = rac2-vip.robinson.com)(PORT = 1521))    
  5.     (LOAD_BALANCE = yes)                                                       
  6.     (CONNECT_DATA =                                                            
  7.       (SERVER = DEDICATED)                                                     
  8.       (SERVICE_NAME = sales.robinson.com)                                      
  9.       (FAILOVER =      --failover_mode是實現故障轉移的關鍵選項                 
  10.         (TYPE = SELECT)                                                          
  11.         (METHOD = BASIC)   --TAF 策略:此處當前為 Basic                          
  12.         (RETRIES = 180)                                                          
  13.         (DELAY = 5)                                                              
  14.       )                                                                        
  15.     )                                                                          
  16.   )                                                                          
        同時引數service_names會多出一個服務名,位於在配置資料庫服務時選擇的首選例項中      
[sql] view plain copy
 print?
  1. SQL> select instance_name from v$instance;                                          
  2.                                                                                     
  3. INSTANCE_NAME                                                                       
  4. ----------------                                                                    
  5. devdb1                                                                              
  6.                                                                                     
  7. SQL> show parameter service_names                                                   
  8.                                                                                     
  9. NAME                                 TYPE        VALUE                              
  10. ------------------------------------ ----------- ------------------------------     
  11. service_names                        string      devdb.robinson.com, sales          
  12.                                                                                     
  13. SQL> select instance_name from v$instance;                                          
  14.                                                                                     
  15. INSTANCE_NAME                                                                       
  16. ----------------                                                                    
  17. devdb2                                                                              
  18.                                                                                     
  19. SQL> show parameter service                                                         
  20.                                                                                     
  21. NAME                                 TYPE        VALUE                              
  22. ------------------------------------ ----------- ------------------------------     
  23. service_names                        string      devdb.robinson.com                 
        使用srvctl工具也可以看到該服務已經正常開始提供服務        
[sql] view plain copy
 print?
  1. SQL> ho srvctl status service -d devdb -s sales                              
  2. Service sales is running on instance(s) devdb1                               
  3.                                                                              
  4. SQL> ho lsnrctl status                                                       
  5.                                                                              
  6. Service "sales.robinson.com" has 1 instance(s).   --sales正常提供服務        
  7.   Instance "devdb1", status READY, has 2 handler(s) for this service...      
    2.實現故障轉移
        下面使用帳戶usr1,服務名sales從Windows客戶端來登陸,注意要配置好客戶端tnsnames,可以將伺服器sales項內容全部複製到客戶端tnsnames.ora中     
[sql] view plain copy
 print?
  1. C:\>sqlplus usr1/usr1@sales                                                                         
  2. SQL> col host_name format a20                                                                       
  3. SQL> select instance_number ins_no,instance_name,host_name,status from v$instance;                  
  4.                                                                                                     
  5.     INS_NO INSTANCE_NAME    HOST_NAME            STATUS                                               
  6. ---------- ---------------- -------------------- ------------                                       
  7.          1 devdb1           rac1.robinson.com    OPEN                                                   
  8. SQL> select failover_type,failover_method,failed_over from v$session                                
  9.   2  where username='USR1';                                                                         
  10.                                                                                                     
  11. FAILOVER_TYPE FAILOVER_M FAI                                                                        
  12. ------------- ---------- ---                                                                        
  13. SELECT        BASIC      NO                                                                         
  14.                                                                                                     
  15. --從其它會話使用sys帳戶登陸到crm 並關閉該例項                                                         
  16. SQL> show user;                                                                                     
  17. USER is "SYS"                                                                                       
  18. SQL> select instance_name from v$instance;                                                          
  19.                                                                                                     
  20. INSTANCE_NAME                                                                                       
  21. ----------------                                                                                    
  22. devdb1                                                                                              
  23.                                                                                                     
  24. SQL> shutdown abort                                                                                 
  25. ORACLE instance shut down.                                                                          
  26.                                                                                                     
  27. --從先前登陸到sales的會話中驗證會話故障切換功能                                                     
  28. SQL> select instance_number ins_no,instance_name,host_name,status from v$instance;                  
  29.                                                                                                     
  30.     INS_NO INSTANCE_NAME    HOST_NAME            STATUS                                               
  31. ---------- ---------------- -------------------- ------------                                       
  32.                  2 devdb2           rac2.robinson.com    OPEN                                               
  33.                                                                                                     
  34. SQL> select failover_type,failover_method,failed_over from v$session                                
  35.   2  where username='USR1';  --第3列顯示的為yes,也表明經過故障切換後提供的服務                     
  36.                                                                                                     
  37. FAILOVER_TYPE FAILOVER_M FAI                                                                        
  38. ------------- ---------- ---                                                                        
  39. SELECT        BASIC      YES                                                                            
  40.                                                                                                     
  41. --由下面的查詢中可以看到服務名sales被新增到可用例項devdb2,節點rac2 的service_names引數中           
  42. SQL> select instance_name from v$instance;                                                          
  43.                                                                                                     
  44. INSTANCE_NAME                                                                                       
  45. ----------------                                                                                    
  46. devdb2                                                                                              
  47.                                                                                                     
  48. SQL> show parameter service                                                                         
  49.                                                                                                     
  50. NAME                                 TYPE        VALUE                                              
  51. ------------------------------------ ----------- ------------------------------                     
  52. service_names                        string      devdb.robinson.com, sales                            
    3.重新定位故障轉移服務到首選例項
        對於首選例項從故障中恢復後,需要手動來重新定位到首選例項        
[sql] view plain copy
 print?
  1. SQL> startup  --啟動devdb1                                                           
  2. SQL> show parameter service_names                                                    
  3.                                                                                      
  4. NAME                                 TYPE        VALUE                               
  5. ------------------------------------ ----------- ------------------------------      
  6. service_names                        string      devdb.robinson.com                  
  7.                                                                                      
  8. SQL> ho srvctl relocate service -d devdb -s sales -i devdb2 -t devdb1                
  9.                                                                                      
  10. SQL> show parameter service_names                                                    
  11.                                                                                      
  12. NAME                                 TYPE        VALUE                               
  13. ------------------------------------ ----------- ------------------------------      
  14. service_names                        string      devdb.robinson.com, sales           
    4.DML故障轉移(不同於DQL,因此單獨列出)
        使用Windows客戶端透過sales服務名登陸        
[sql] view plain copy
 print?
  1. C:\>sqlplus usr1/usr1@sales                                                            
  2.                                                                                        
  3. SQL> show user;                                                                        
  4. USER is "USR1"                                                                         
  5. SQL> create table tb_temp (id int,ename varchar2(20)) tablespace tbs1;                 
  6.                                                                                          
  7. Table created.                                                                         
  8.                                                                                        
  9. SQL> insert into tb_temp                                                               
  10.   2  select 1,'Robinson' from dual                                                     
  11.   3  union all                                                                         
  12.   4  select 2,'Jackson'  from dual;                                                    
  13.                                                                                        
  14. rows created.                                                                        
  15.                                                                                        
  16. SQL> commit;                                                                           
  17.                                                                                        
  18. Commit complete.                                                                       
  19.                                                                                        
  20. SQL> select * from tb_temp;                                                            
  21.                                                                                        
  22.         ID ENAME                                                                           
  23. ---------- --------------------                                                        
  24.          1 Robinson                                                                        
  25.          2 Jackson                                                                         
  26.                                                                                        
  27. SQL> delete from tb_temp;                                                              
  28.                                                                                        
  29. rows deleted.                                                                        
  30.                                                                                        
  31. SQL> select * from tb_temp;                                                            
  32.                                                                                        
  33. no rows selected                                                                       
        從另一個會話中使用sysdba關閉devdb1(shutdown abort)
        再在剛剛執行表建立的會話中查詢記錄,收到提示,事務必須被回滾     
[sql] view plain copy
 print?
  1. SQL> select * from tb_temp;                                                  
  2. select * from tb_temp                                                        
  3. *                                                                            
  4. ERROR at line 1:                                                             
  5. ORA-25402: transaction must roll back                                        
  6.                                                                              
  7. SQL> rollback;                                                               
  8.                                                                              
  9. Rollback complete.                                                           
  10. SQL> select * from tb_temp;                                                  
  11.                                                                              
  12.         ID ENAME                                                                 
  13. ---------- --------------------                                              
  14.          1 Robinson                                                              
  15.          2 Jackson                                                               
  16.                                                                              
  17. SQL> select failover_type,failover_method,failed_over from v$session         
  18.   2  where username='USR1';                                                  
  19.                                                                              
  20. FAILOVER_TYPE FAILOVER_M FAI                                                 
  21. ------------- ---------- ---                                                 
  22. SELECT        BASIC      YES                                                 
        總結:對於DML 操作在實現故障轉移時,將嚴格按照ACID原則來執行,大部分情況需要回滾事務。    



TAF配置


11g RAC環境下客戶端配置TAF




TAF是Transparent Application Failover的英文縮寫,顧名思義就是對應用透明的故障轉移,舉個例子,當應用連線某個oracle資料庫的執行查詢操作的時候,資料庫伺服器網路中斷或者例項崩潰,在經過delay引數設定的值之後,將自動連線到其他可用的例項,繼續進行查詢。(前提是執行計劃,輸出結果集和輸出的順序不能發生變化)

TAF的特性:
1:TAF是ORACLE客戶端提供的一項特性,使用TAF,對客戶端的環境有一定的要求,比如JAVA的JDBC驅動、Oracle客戶端的版本等(8i開始支援TAF);
2:大致上TAF可以分為2種,連線時的TAF和會話建立後TAF;
3:TAF本身與是否RAC環境無關,但一般都用在RAC環境,最小程度的減少最應用的影響,單例項環境下也可以使用TAF,這樣使用PL/SQL developer連線資料庫,即使資料庫例項重啟,也不需要重新連線;
4:RAC環境下,還可以把TAF配置在伺服器端;
5:配置listener.ora檔案的GLOBAL_DBNAME引數後將會禁用TAF。

配置如下:

1、檢視服務端的service_name 和 RAC SCAN地址



[oracle@rac122 tnslsnr]$ cat /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1               localhost.localdomain localhost
::1             localhost6.localdomain6 localhost6

#Public
10.1.252.121            rac121
10.1.252.122            rac122
#Private
192.168.123.1           rac121-priv
192.168.123.2           rac122-priv

#Virtual IP
10.1.252.75             rac121-vip
10.1.252.76             rac122-vip 10.1.252.68             rac-scan [oracle@rac122 tnslsnr]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.3.0 Production on Tue Jun 26 15:33:29 2012

Copyright (c) 1982, 2011, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options

SQL> show parameter service_name

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
service_names                        string      racdb SQL>  


2、在客戶端tnsnames.ora檔案中新增如下內容:


racdb =  
  (DESCRIPTION =  
    (ADDRESS_LIST =  
      (ADDRESS = (PROTOCOL = TCP)(HOST = 10.1.252.68)(PORT = 1521))  
    )  
    (CONNECT_DATA =  
      (SERVICE_NAME = racdb)  
      (FAILOVER_MODE =  
       (TYPE = SELECT)  
       (METHOD = BASIC)  
       (RETRIES = 180)  
       (DELAY = 5))  
    )  
  ) 

         

    屬性說明 :
            TYPE    type屬性描述了故障轉移的型別,可能的值如下:
                         SESSION  指定發生故障轉換時,TAF應建立會話,不執行其他任何操作。
                         SELECT   指定執行故障轉換時,除了建立會話,TAF 也應該重新啟動故障轉移期間執行的任何SELECT語句,
                                            當SELECT 語句執行時,ORACLE Net 將返回故障轉移前沒有返回給使用者的行。
                          NONE     不執行TAF故障轉移
             METHOD  method特徵確定何時建立會話,可能的值如下 :
                         BASIC      在故障轉移期間建立會話
                         PRECONNECT 與資料庫的初始連線建立後,採用由BACKUP 特性指定的ORACLE Net別名建立故障轉移會話。
             RETRIES retries特性指Oracle Net 返回ADDRESS_LIST 並嘗試連線到倖存例項的次數。
             DELAY   delay特性指定每次重試之間等待的次數       




3、用客戶端登入作如下操作


C:\Documents and Settings\```>sqlplus /nolog
SQL*Plus: Release 10.2.0.1.0 - Production on 星期二 6月 26 15:00:56 2012
Copyright (c) 1982, 2005, Oracle.  All rights reserved.

SQL> conn sys/system@racdb as sysdba
已連線。
SQL> show parameter instance_name

NAME                      TYPE                    VALUE
------------------------- ----------------------  ---------------
instance_name             string racdb2 SQL> select sid from v$mystat where rownum=1;
       SID
----------
       163
SQL> select machine,failover_type,failover_method,failed_over from v$session where sid=163;
MACHINE           FAILOVER_TYPE       FAILOVER_METHOD       FAILED
----------------  ----------          --------------------  ------
AILK\TANWEI       SELECT              BASIC                 NO

SQL> create table taf_client_test(id int,vname varchar2(10));
表已建立。
SQL> insert into taf_client_test values(1,'taf');
已建立 1 行。
SQL> update taf_client_test set id=5;
已更新 1 行。
SQL> select * from taf_client_test;
        ID VNAME
---------- --------------------
         5 taf 


4、更新完畢不commit,停掉racdb2例項:


[oracle@rac122 tnslsnr]$ srvctl status instance -d racdb -i racdb2
Instance racdb2 is running on node rac122
[oracle@rac122 tnslsnr]$ srvctl stop instance -d racdb -i racdb2 -o abort
[oracle@rac122 tnslsnr]$ srvctl status instance -d racdb -i racdb2 Instance racdb2 is not running on node rac122 [oracle@rac122 tnslsnr]$ srvctl status instance -d racdb -i racdb1 Instance racdb1 is running on node rac121 --還有一個節點在工作



5、回到客戶端作如下操作:


SQL> select * from taf_client_test;
select * from taf_client_test
*
第 1 行出現錯誤:
ORA-25402: 事務處理必須重新執行


SQL> rollback;


回退已完成。


SQL> select instance_name from v$instance;


INSTANCE_NAME
--------------------------------
racdb1


SQL> show parameter instance_name




NAME                TYPE            VALUE
----------------    -------------   -----------
instance_name       string          racdb1 --發現自動切換到節點1, SQL> select * from taf_client_test;
未選定行
SQL> select * from v$mystat where rownum=1;


       SID STATISTIC#      VALUE
---------- ---------- ----------
       166          0          0


SQL> select failover_type,failover_method,failed_over from v$session where sid=166;

FAILOVER_TYPE              FAILOVER_METHOD      FAILED
-------------------------- -------------------- ------
SELECT                     BASIC                YES 


到此,完成了TAF的配置與驗證。






如何在RAC伺服器端配置TAF

服務端TAF配置會覆蓋客戶端TNS連線串中配置的TAF。如果客戶端沒有配置TAF,在最小配置模式,failover型別必須設定為啟用TAF。如果failover type是在服務端設定,那麼faliover method預設為BASIC。DELAY和RETRIES引數是可選的,可分別指定。

1在RAC上新增一個service

注意:service name是唯一的,並且不能與預設建立服務名一致。預設情況下,Oracle RAC資料庫會建立一個特殊的資料庫服務。這個預設的服務在RAC環境中對所有例項總是可用,除非例項在限制模式。並且,DBA不能修改這個預設的服務及其屬性。

新增service的語法:

srvctl add service -d orcl -s taf_orcl.oracle.com -r "orcl1,orcl2" -P BASIC

注:引數-d:資料庫名;-s:服務名;-r:首選例項名;-P:TAF策略

2啟動service

srvctl start service -d orcl -s taf_orcl.oracle.com

3檢查service執行狀態

srvctl config service -d orcl

4獲取建立的service_id

select name,service_id from dba_services where name = 'taf_orcl.oracle.com';

5檢查service的標準配置

col failover_method format a11 heading 'METHOD' 
col failover_type format a10 heading 'TYPE' 
col failover_retries format 9999999 heading 'RETRIES' 
col goal format a10 
col clb_goal format a8 
col AQ_HA_NOTIFICATIONS format a5 heading 'AQNOT' 

SQL>select name, failover_method, failover_type, failover_retries,goal, clb_goal,aq_ha_notifications  
from dba_services where service_id = X

注意:預設情況下,method,type,retries是沒有值的,這些值對於服務端的TAF是必須的。這是由於srvctl命令新增服務時,沒有修改DBMS_SERVICE引數,在11.2之後,該bug解決。

6新增failover引數

--11.2之前:

SQL> execute dbms_service.modify_service (service_name => 'taf_orcl.oracle.com' - 
, aq_ha_notifications => true - 
, failover_method => dbms_service.failover_method_basic - 
, failover_type => dbms_service.failover_type_select - 
, failover_retries => 180 - 
, failover_delay => 5 - 
, clb_goal => dbms_service.clb_goal_long); 

PL/SQL procedure successfully completed.

注:由於TAF的method為BASIC,BASIC是當前支援的唯一值,不再支援PRECONNECT。

--11.2開始:

srvctl add service -d db_unique_name -s service_name -t edition_name{-r preferred_list [-a available_list]} | {-g server_pool[-c {UNIFORM | SINGLETON}] [-k net_number]}
[-P {BASIC | NONE}] [-l {[PRIMARY] | [PHYSICAL_STANDBY] | [LOGICAL_STANDBY] |
[SNAPSHOT_STANDBY]}] [-y {AUTOMATIC | MANUAL}] [-q {TRUE | FALSE}]
[-x {TRUE | FALSE}] [-j {SHORT | LONG}] [-B {NONE | SERVICE_TIME | THROUGHPUT}]
 [-e {NONE |SESSION | SELECT}] [-m {NONE | BASIC}] [-z failover_retries]
 [-w failover_delay]
7檢查service及method、retries
SQL>select name, failover_method, failover_type, failover_retries,goal, clb_goal,aq_ha_notifications 
from dba_services where service_id = X
8檢查監聽是否已註冊新建立的service
lsnrctl services

當然,還可以透過DBCA或者Oracle OEM來配置RAC Server。本文不再詳述。



配置Server Side TAF

實驗環境:Oracle 11.2.0.4 RAC
參考MOS文件:
How To Configure Server Side Transparent Application Failover (文件 ID 460982.1)

1.為設定TAF在RAC叢集上新建服務

eg: srvctl add service -d rac -s server_taf -r "rac1,rac2" -P BASIC

使用oracle使用者在RAC叢集上新建服務server_taf:

[oracle@jyrac1 ~]$ srvctl add service -d jyzhao -s server_taf -r "jyzhao1,jyzhao2" -P BASIC
[oracle@jyrac1 ~]$ 

注意不能使用grid使用者操作,如果使用grid 使用者執行的話,會報錯:

[grid@jyrac1 ~]$ srvctl add service -d jyzhao -s server_taf -r "jyzhao1,jyzhao2" -P BASIC
PRCD-1288 : User is not authorized to create service server_taf for database jyzhao
PRKH-1014 : Current user "grid" is not the oracle owner user "oracle" of oracle home "/opt/app/oracle/product/11.2.0/dbhome_1"

2.啟動server_taf服務

eg: srvctl start service -d rac -s server_taf

啟動server_taf服務

[oracle@jyrac1 ~]$ srvctl start service -d jyzhao -s server_taf

3.檢查確認服務正在執行

eg: srvctl config service -d rac

檢查確認服務正在執行:

[oracle@jyrac1 ~]$ srvctl config service -d jyzhao
Service name: server_taf
Service is enabled
Server pool: jyzhao_server_taf Cardinality: 2 Disconnect: false Service role: PRIMARY
Management policy: AUTOMATIC
DTP transaction: false AQ HA notifications: false Failover type: NONE
Failover method: NONE
TAF failover retries: 0 TAF failover delay: 0 Connection Load Balancing Goal: LONG
Runtime Load Balancing Goal: NONE
TAF policy specification: BASIC Edition: Preferred instances: jyzhao1,jyzhao2
Available instances: 

4.找到剛建立服務的service_id

eg: select name,service_id from dba_services where name = 'server_taf';

找到剛建立服務的service_id

SQL> select name,service_id from dba_services where name = 'server_taf'; NAME SERVICE_ID
---------------------------------------------------------------- ----------
server_taf 7

5.根據service_id審查服務的資訊

col name format a15
col failover_method format a11 heading 'METHOD'
col failover_type format a10 heading 'TYPE'
col failover_retries format 9999999 heading 'RETRIES'
col goal format a10
col clb_goal format a8
col AQ_HA_NOTIFICATIONS format a5 heading 'AQNOT'

select name, failover_method, failover_type, failover_retries,goal, clb_goal,aq_ha_notifications
from dba_services where service_id = 7;

根據service_id審查服務的資訊:

SQL> col name format a15  
SQL> col failover_method format a11 heading 'METHOD' SQL> col failover_type format a10 heading 'TYPE' SQL> col failover_retries format 9999999 heading 'RETRIES' SQL> col goal format a10 
SQL> col clb_goal format a8 
SQL> col AQ_HA_NOTIFICATIONS format a5 heading 'AQNOT' SQL> select name, failover_method, failover_type, failover_retries,goal, clb_goal,aq_ha_notifications 2 from dba_services where service_id = 7 3 ; NAME METHOD TYPE RETRIES GOAL       CLB_GOAL AQNOT
--------------- ----------- ---------- -------- ---------- -------- -----
server_taf NONE NONE 0 NONE LONG     NO

SQL> 

6.給服務新增server side failover引數

execute dbms_service.modify_service (service_name => 'server_taf' -
, aq_ha_notifications => true -
, failover_method => dbms_service.failover_method_basic -
, failover_type => dbms_service.failover_type_select -
, failover_retries => 180 -
, failover_delay => 5 -
, clb_goal => dbms_service.clb_goal_long);

11.2版本可以使用srvctl 修改服務的資訊:
srvctl modify service -d RAC -s server_taf -m BASIC -e SELECT -q TRUE -j LONG

給服務新增server side failover引數:

SQL> execute dbms_service.modify_service (service_name => 'server_taf' - 
> , aq_ha_notifications => true - 
> , failover_method => dbms_service.failover_method_basic - 
> , failover_type => dbms_service.failover_type_select - 
> , failover_retries => 180 - 
> , failover_delay => 5 - 
> , clb_goal => dbms_service.clb_goal_long); 

PL/SQL procedure successfully completed.

7.再次審查服務可以看到Method, Type和Retries值

select name, failover_method, failover_type, failover_retries,goal, clb_goal,aq_ha_notifications
from dba_services where service_id = 7;

再次審查服務可以看到Method, Type和Retries值:

SQL> select name, failover_method, failover_type, failover_retries,goal, clb_goal,aq_ha_notifications 2 from dba_services where service_id = 7; NAME METHOD TYPE RETRIES GOAL       CLB_GOAL AQNOT
--------------- ----------- ---------- -------- ---------- -------- -----
server_taf      BASIC SELECT 180 NONE LONG     YES

8.檢查已註冊的服務的監聽資訊

lsnrctl services

Service "server_taf.za.oracle.com" has 2 instance(s).
Instance "rac1", status READY, has 2 handler(s) for this service...
Handler(s):
"DEDICATED" established:0 refused:0 state:ready
REMOTE SERVER
(ADDRESS=(PROTOCOL=TCP)(HOST=dell01)(PORT=1521))
"DEDICATED" established:0 refused:0 state:ready
LOCAL SERVER
Instance "rac2", status READY, has 1 handler(s) for this service...
Handler(s):
"DEDICATED" established:0 refused:0 state:ready
REMOTE SERVER
(ADDRESS=(PROTOCOL=TCP)(HOST=dell02)(PORT=1521))

我這裡版本差異,顯示有區別,分別在不同節點顯示自己的例項:

--node1: Service "server_taf" has 1 instance(s).
  Instance "jyzhao1", status READY, has 1 handler(s) for this service... Handler(s): "DEDICATED" established:0 refused:0 state:ready LOCAL SERVER The command completed successfully --node2: Service "server_taf" has 1 instance(s). Instance "jyzhao2", status READY, has 1 handler(s) for this service... Handler(s): "DEDICATED" established:0 refused:0 state:ready LOCAL SERVER The command completed successfully

9.建立網路服務名

SERVERTAF =
(DESCRIPTION =
(LOAD_BALANCE = yes)
(ADDRESS = (PROTOCOL = TCP)(HOST = dell01)(PORT = 1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = dell02)(PORT = 1521))
(CONNECT_DATA =
(SERVICE_NAME = server_taf.za.oracle.com)
)
)

服務端RAC所有節點配置tnsnames.ora,新增內容:

SERVERTAF =
  (DESCRIPTION =
    (LOAD_BALANCE = yes)
    (ADDRESS = (PROTOCOL = TCP)(HOST = jyrac1)(PORT = 1521))
    (ADDRESS = (PROTOCOL = TCP)(HOST = jyrac2)(PORT = 1521))
    (CONNECT_DATA =
      (SERVICE_NAME = server_taf)
    )
  )

sqlplus system/oracle@192.168.56.160/server_taf

10.測試TAF功能

select host_name,instance_name from v$instance;

SQL> select instance_name from V$instance;
INSTANCE_NAME
----------------
rac2

SQL> shutdown abort;
ORACLE instance shut down.

select host_name,instance_name from v$instance;

10.1 模擬客戶端使用scanVIP測試能否實現TAF
sqlplus system/oracle@192.168.56.160/server_taf

[grid@jyrac1 ~]$ sqlplus system/oracle@192.168.56.160/server_taf

SQL*Plus: Release 11.2.0.4.0 Production on Fri Mar 10 02:59:53 2017 Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options

SQL> select host_name,instance_name from v$instance;

HOST_NAME ---------------------------------------------------------------- INSTANCE_NAME ---------------- jyrac1
jyzhao1 --這裡強制關掉jyzhao1例項。 SQL> /

HOST_NAME ---------------------------------------------------------------- INSTANCE_NAME ---------------- jyrac2
jyzhao2

10.1 結論: 可以實現TAF功能,相當於客戶端不再需要配置,直接透過SCAN VIP連線。

10.2 模擬客戶端使用Public IP測試能否實現TAF
sqlplus system/oracle@192.168.56.150/server_taf

SQL> select host_name,instance_name from v$instance;

HOST_NAME ---------------------------------------------------------------- INSTANCE_NAME ---------------- jyrac1
jyzhao1 --這裡強制關掉jyzhao1例項。 SQL> / select host_name,instance_name from v$instance
*
ERROR at line 1:
ORA-12153: TNS:not connected
Process ID: 20116 Session ID: 24 Serial number: 7

如果客戶端配置tnsnames.ora,將publicIP配置

TAF =
  (DESCRIPTION =
    (LOAD_BALANCE = yes)
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.56.150)(PORT = 1521))
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.56.152)(PORT = 1521))
    (CONNECT_DATA =
      (SERVICE_NAME = server_taf)
    )
  )

再次測試:

[oracle@jyrac2 admin]$ sqlplus system/oracle@taf

SQL*Plus: Release 11.2.0.4.0 Production on Fri Mar 10 05:11:30 2017 Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options

SQL> select host_name,instance_name from v$instance;

HOST_NAME ---------------------------------------------------------------- INSTANCE_NAME ---------------- jyrac2
jyzhao2 --這裡強制關掉jyzhao2例項。 SQL> /

HOST_NAME ---------------------------------------------------------------- INSTANCE_NAME ---------------- jyrac1
jyzhao1

10.2 結論: 直接連線Public IP無法實現TAF功能。但客戶端配置Public IP列表,可以實現。

10.3 模擬客戶端使用VIP測試能否實現TAF
sqlplus system/oracle@192.168.56.151/server_taf

[grid@jyrac1 ~]$ sqlplus system/oracle@192.168.56.151/server_taf

SQL*Plus: Release 11.2.0.4.0 Production on Fri Mar 10 04:32:20 2017 Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options

SQL> select host_name,instance_name from v$instance;

HOST_NAME
----------------------------------------------------------------
INSTANCE_NAME
----------------
jyrac1
jyzhao1


SQL> / select host_name,instance_name from v$instance
* ERROR at line 1:
ORA-03113: end-of-file on communication channel
Process ID: 32459 Session ID: 159 Serial number: 3

如果客戶端配置tnsnames.ora,可以透過sqlplus 。

TAFVIP =
  (DESCRIPTION =
    (LOAD_BALANCE = yes)
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.56.151)(PORT = 1521))
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.56.153)(PORT = 1521))
    (CONNECT_DATA =
      (SERVICE_NAME = server_taf)
    )
  )

再次測試:

[oracle@jyrac2 admin]$ sqlplus system/oracle@tafvip

SQL*Plus: Release 11.2.0.4.0 Production on Fri Mar 10 05:15:32 2017 Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options

SQL> select host_name,instance_name from v$instance;

HOST_NAME ---------------------------------------------------------------- INSTANCE_NAME ---------------- jyrac2
jyzhao2 --這裡強制關掉jyzhao2例項。 SQL> /

HOST_NAME ---------------------------------------------------------------- INSTANCE_NAME ---------------- jyrac1
jyzhao1

10.3 結論: 直接連線VIP無法實現TAF功能。但客戶端配置VIP列表,可以實現。




官方文件:

About Transparent Application Failover

TAF is a client-side feature that allows clients to reconnect to surviving databases in the event of a failure of a database instance. Notifications are used by the server to trigger TAF callbacks on the client-side.

TAF is configured using either client-side specified Transparent Network Substrate (TNS) connect string or using server-side service attributes. If both methods are used to configure TAF, then the server-side service attributes supersede the client-side settings. Server-side service attributes are the preferred way to set up TAF.

TAF can operate in one of two modes, Session Failover and Select Failover. Session Failover re-creates lost connections and sessions. Select Failover replays queries that were in progress.

When there is a failure, callback functions are initiated on the client-side using Oracle Call Interface (OCI) callbacks. This works with standard OCI connections as well as Connection Pool and Session Pool connections.

TAF operates with Oracle Data Guard to provide automatic failover. TAF works with the following database configurations to effectively mask a database failure:

  • Oracle Real Application Clusters

  • Replicated systems

  • Standby databases

  • Single instance Oracle database

See Also:

What Transparent Application Failover Restores

TAF automatically restores some or all of the following elements associated with active database connections. Other elements may need to be embedded in the application code to enable TAF to recover the connection.

  • Client-server database connections: TAF automatically reestablishes the connection using the same connect string or an alternate connect string that you specify when configuring failover.

  • Users' database sessions: TAF automatically logs a user in with the same user ID as was used before the failure. If multiple users were using the connection, then TAF automatically logs them in as they attempt to process database commands. Unfortunately, TAF cannot automatically restore other session properties. These properties can be restored by invoking a callback function.

  • Completed commands: If a command was completed at the time of connection failure, and it changed the state of the database, then TAF does not resend the command. If TAF reconnects in response to a command that may have changed the database, then TAF issues an error message to the application.

  • Open cursors used for fetching: TAF allows applications that began fetching rows from a cursor before failover to continue fetching rows after failover. This is called select failover. It is accomplished by re-running a SELECT statement using the same snapshot, discarding those rows already fetched and retrieving those rows that were not fetched initially. TAF verifies that the discarded rows are those that were returned initially, or it returns an error message.

  • Active transactions: Any active transactions are rolled back at the time of failure because TAF cannot preserve active transactions after failover. The application instead receives an error message until a ROLLBACK is submitted.

  • Server-side program variables: Server-side program variables, such as PL/SQL package states, are lost during failures, and TAF cannot recover them. They can be initialized by making a call from the failover callback.

About FAILOVER_MODE Parameters

The FAILOVER_MODE parameter must be included in the CONNECT_DATA section of a connect descriptor. FAILOVER_MODE can contain the parameters described in .

Table 13-4 Additional Parameters of the FAILOVER_MODE Parameter

FAILOVER_MODE Parameters Description

BACKUP

A different net service name for backup connections. A backup should be specified when usingpreconnect to pre-establish connections.

DELAY

The amount of time in seconds to wait between connect attempts. If RETRIES is specified, thenDELAY defaults to one second.

If a callback function is registered, then this parameter is ignored.

METHOD

Setting for fast failover from the primary node to the backup node:

  • basic: Set to establish connections at failover time. This option requires almost no work on the backup server until failover time.

  • preconnect: Set to pre-established connections. This provides faster failover but requires that the backup instance be able to support all connections from every supported instance.

RETRIES

The number of times to attempt to connect after a failover. If DELAY is specified, then RETRIESdefaults to five retry attempts.

If a callback function is registered, then this parameter is ignored.

TYPE

The type of failover. Three types of Oracle Net failover functionality are available by default to Oracle Call Interface (OCI) applications:

  • session: Set to failover the session. If a user's connection is lost, then a new session is automatically created for the user on the backup. This type of failover does not attempt to recover select operations.

  • select: Set to enable users with open cursors to continue fetching on them after failure. However, this mode involves overhead on the client side in normal select operations.

  • none: This is the default. No failover functionality is used. This can also be explicitly specified to prevent failover from happening.


Note:

Oracle Net Manager does not provide support for TAF parameters. These parameters must be set manually.

Implementing Transparent Application Failover

Important:

Do not set the GLOBAL_DBNAME parameter in the SID_LIST_listener_name section of the listener.ora file. A statically configured global database name disables TAF.

Depending on the FAILOVER_MODE parameters, you can implement TAF in several ways. Oracle recommends the following methods:

TAF with Connect-Time Failover and Client Load Balancing

Implement TAF with connect-time failover and client load balancing for multiple addresses. In the following example, Oracle Net connects randomly to one of the protocol addresses on sales1-serveror sales2-server. If the instance fails after the connection, then the TAF application fails over to the other node's listener, reserving any SELECT statements in progress.

sales.us.example.com=
 (DESCRIPTION= (LOAD_BALANCE=on) (FAILOVER=on)  (ADDRESS=
       (PROTOCOL=tcp)  
       (HOST=sales1-server)  
       (PORT=1521)) 
  (ADDRESS=
       (PROTOCOL=tcp)  
       (HOST=sales2-server)  
       (PORT=1521)) 
  (CONNECT_DATA=
     (SERVICE_NAME=sales.us.example.com) (FAILOVER_MODE= (TYPE=select)  (METHOD=basic))))

TAF Retrying a Connection

TAF also provides the ability to automatically retry connecting if the first connection attempt fails with the RETRIES and DELAY parameters. In the following example, Oracle Net tries to reconnect to the listener on sales1-server. If the failover connection fails, then Oracle Net waits 15 seconds before trying to reconnect again. Oracle Net attempts to reconnect up to 20 times.

sales.us.example.com=
 (DESCRIPTION=
  (ADDRESS=
       (PROTOCOL=tcp)  
       (HOST=sales1-server)  
       (PORT=1521)) 
  (CONNECT_DATA=
     (SERVICE_NAME=sales.us.example.com) (FAILOVER_MODE= (TYPE=select)  (METHOD=basic) (RETRIES=20) (DELAY=15))))

TAF Pre-establishing a Connection

A backup connection can be pre-established. The initial and backup connections must be explicitly specified. In the following example, clients that use net service name sales1.us.example.com to connect to the listener on sales1-server are also preconnected to sales2-server. If sales1-server fails after the connection, then Oracle Net fails over to sales2-server, preserving anySELECT statements in progress. Similarly, Oracle Net preconnects to sales1-server for those clients that use sales2.us.example.com to connect to the listener on sales2-server.

sales1.us.example.com=
 (DESCRIPTION=
  (ADDRESS=
       (PROTOCOL=tcp)  
       (HOST=sales1-server)  
       (PORT=1521)) 
  (CONNECT_DATA=
     (SERVICE_NAME=sales.us.example.com) 
     (INSTANCE_NAME=sales1) (FAILOVER_MODE= (BACKUP=sales2.us.example.com) (TYPE=select)  (METHOD=preconnect))))
sales2.us.example.com=
 (DESCRIPTION=
  (ADDRESS=
       (PROTOCOL=tcp)  
       (HOST=sales2-server)  
       (PORT=1521)) 
  (CONNECT_DATA=
     (SERVICE_NAME=sales.us.example.com) 
     (INSTANCE_NAME=sales2) (FAILOVER_MODE= (BACKUP=sales1.us.example.com) (TYPE=select)  (METHOD=preconnect))))

Verifying Transparent Application Failover

You can query the FAILOVER_TYPE, FAILOVER_METHOD, and FAILED_OVER columns in the V$SESSION view to verify that TAF is correctly configured. To view the columns, use a query similar to the following:

SELECT MACHINE, FAILOVER_TYPE, FAILOVER_METHOD, FAILED_OVER, COUNT(*)
FROM V$SESSION
GROUP BY MACHINE, FAILOVER_TYPE, FAILOVER_METHOD, FAILED_OVER;

The output before failover looks similar to the following:

MACHINE              FAILOVER_TYPE FAILOVER_M FAI   COUNT(*)
-------------------- ------------- ---------- --- ----------
sales1               NONE          NONE       NO          11
sales2               SELECT        PRECONNECT NO           1

The output after failover looks similar to the following:

MACHINE              FAILOVER_TYPE FAILOVER_M FAI   COUNT(*)
-------------------- ------------- ---------- --- ----------
sales2               NONE          NONE       NO          10
sales2               SELECT        PRECONNECT YES          1

Note:

You can monitor each step of TAF using an appropriately configured OCI TAF CALLBACK function.

See Also:






Oracle RAC failover 測試(Server TAF方式)


  Oracle RAC中,除了基於客戶端的TAF方式之外,還有基於伺服器端的TAF方式,可以把服務端的TAF方式看作是客戶端TAF方式的一個升級版吧。伺服器端的TAF,當然是需要在伺服器端進行配置了,這個是透過Service來完成的。本文主要描述Oracle 10g rac 下透過service方式配置伺服器端的TAF。

  下面是一些關於這方面的基礎參考連結:
  有關負監聽配置,載均衡(load balance)請參考
    ORACLE RAC 監聽配置 (listener.ora tnsnames.ora) 
    ORACLE RAC 下非預設埠監聽配置(listener.ora tnsnames.ora) 
    Oracle RAC 客戶端連線負載均衡(Load Balance)
    Oracle RAC 伺服器端連線負載均衡(Load Balance)
    Oracle RAC 負載均衡測試(結合伺服器端與客戶端) 
    
  有關Oracle RAC failover 連線時故障轉移請參考
    Oracle RAC failover 測試(連線時故障轉移)
    Oracle RAC failover 測試(TAF方式)
    
  有關Services的建立請參考
    Services in Oracle Database 10g 
    再說 Oracle RAC services

[sql] view plain copy
 print?
  1. 1、伺服器端、客戶端的環境  
  2.   #伺服器端環境,host資訊  
  3.   oracle@bo2dbp:~> cat /etc/hosts |grep vip  
  4.   192.168.7.61   bo2dbp-vip.2gotrade.com    bo2dbp-vip  
  5.   192.168.7.62   bo2dbs-vip.2gotrade.com    bo2dbs-vip  
  6.     
  7.   #伺服器端環境,叢集資訊  
  8.   oracle@bo2dbp:~> ./crs_stat.sh   
  9.    Resource name                                Target     State               
  10.   --------------                                ------     -----               
  11.   ora.GOBO4.GOBO4A.inst                         ONLINE     ONLINE on bo2dbp    
  12.   ora.GOBO4.GOBO4B.inst                         ONLINE     ONLINE on bo2dbs    
  13.   ora.GOBO4.db                                  ONLINE     ONLINE on bo2dbp    
  14.   ora.bo2dbp.ASM1.asm                           ONLINE     ONLINE on bo2dbp    
  15.   ora.bo2dbp.LISTENER_BO2DBP.lsnr               ONLINE     ONLINE on bo2dbp    
  16.   ora.bo2dbp.LISTENER_ORA10G_BO2DBP.lsnr        ONLINE     ONLINE on bo2dbp    
  17.   ora.bo2dbp.gsd                                ONLINE     ONLINE on bo2dbp    
  18.   ora.bo2dbp.ons                                ONLINE     ONLINE on bo2dbp    
  19.   ora.bo2dbp.vip                                ONLINE     ONLINE on bo2dbp    
  20.   ora.bo2dbs.ASM2.asm                           ONLINE     ONLINE on bo2dbs    
  21.   ora.bo2dbs.LISTENER_BO2DBS.lsnr               ONLINE     ONLINE on bo2dbs    
  22.   ora.bo2dbs.LISTENER_ORA10G_BO2DBS.lsnr        ONLINE     ONLINE on bo2dbs    
  23.   ora.bo2dbs.gsd                                ONLINE     ONLINE on bo2dbs    
  24.   ora.bo2dbs.ons                                ONLINE     ONLINE on bo2dbs    
  25.   ora.bo2dbs.vip                                ONLINE     ONLINE on bo2dbs    
  26.   ora.ora10g.db                                 ONLINE     ONLINE on bo2dbp   
  27.   
  28.   #客戶端環境  
  29.   robin@SZDB:~> cat /etc/issue  
  30.     
  31.   Welcome to SUSE Linux Enterprise Server 10 SP3 (x86_64) - Kernel \r (\l).  
  32.     
  33.   robin@SZDB:~> sqlplus -v  
  34.     
  35.   SQL*Plus: Release 10.2.0.3.0 - Production  
  36.     
  37.   #客戶端tnsnames配置  
  38.   GOBO4 =  
  39.     (DESCRIPTION =  
  40.       (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.61)(PORT = 1521))  
  41.       (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.62)(PORT = 1521))  
  42.       (LOAD_BALANCE = yes)  
  43.       (CONNECT_DATA =  
  44.         (SERVER = DEDICATED)  
  45.         (SERVICE_NAME = TAF)  #注意我們客戶端的SERVICE_NAME,我們設定為TAF   
  46.       )  
  47.     )  
  48.   
  49. 2、在伺服器端配置service  
  50.   配置service有多種方式,如dbca,oem,srvctl命令列。下面直接以命令列方式配置  
  51.   關於什麼是service以及如何使用srvctl命令列建立service,請參考: http://blog.csdn.net/robinson_0612/article/details/8124232  
  52.   oracle@bo2dbp:~> srvctl add service -d GOBO4 -s TAF -r GOBO4A -a GOBO4B -P basic  
  53.   oracle@bo2dbp:~> srvctl start service -d GOBO4 -s TAF  
  54.   oracle@bo2dbp:~> ./crs_stat.sh | grep TAF  
  55.   ora.GOBO4.TAF.GOBO4A.srv                      ONLINE     ONLINE on bo2dbp    
  56.   ora.GOBO4.TAF.cs                              ONLINE     ONLINE on bo2dbp              
  57.   oracle@bo2dbp:~> srvctl config service -d GOBO4 -a  
  58.   TAF PREF: GOBO4A AVAIL: GOBO4B TAF: basic  
  59.   oracle@bo2dbp:~> export ORACLE_SID=GOBO4A  
  60.   oracle@bo2dbp:~> sqlplus / as sysdba  
  61.     
  62.   SQL*Plus: Release 10.2.0.3.0 - Production on Mon Dec 17 14:55:02 2012  
  63.     
  64.   Copyright (c) 1982, 2006, Oracle.  All Rights Reserved.  
  65.     
  66.   Connected to:  
  67.   Oracle Database 10g Release 10.2.0.3.0 - 64bit Production  
  68.   With the Real Application Clusters option  
  69.     
  70.   SQL> show parameter service  
  71.     
  72.   NAME                                 TYPE        VALUE  
  73.   ------------------------------------ ----------- ------------------------------  
  74.   service_names                        string      SYS$SYS.KUPC$S_1_2012102317304  
  75.                                                    4.GOBO4, SYS$SYS.KUPC$C_1_2012  
  76.                                                    1023173044.GOBO4, GOBO4, TAF  
  77.     
  78.   SQL> begin     
  79.     2  dbms_service.modify_service(     
  80.     3  service_name=>'TAF',     
  81.     4  failover_method =>dbms_service.failover_method_basic,     
  82.     5  failover_type =>dbms_service.failover_type_select,     
  83.     6  failover_retries =>180,     
  84.     7  failover_delay=>5);     
  85.     8  end;     
  86.     9  /  
  87.     
  88.   PL/SQL procedure successfully completed.  
  89.       
  90.   SQL> select name,failover_method,failover_type,goal,clb_goal from dba_services  
  91.     2  where name='TAF';  
  92.     
  93.   NAME                 FAILOVER_METHOD      FAILOVER_TYPE   GOAL         CLB_G  
  94.   -------------------- -------------------- --------------- ------------ -----  
  95.   TAF                  BASIC                SELECT                       LONG                                                     
  96.     
  97.   SQL> ho lsnrctl status  
  98.     ..........  
  99.   Service "TAF" has 1 instance(s).  
  100.     Instance "GOBO4A", status READY, has 2 handler(s) for this service...  
  101.   The command completed successfully  
  102.   
  103. 3、測試伺服器端TAF  
  104.   robin@SZDB:~> sqlplus fail_over/fail@gobo4  
  105.   fail_over@GOBO4> get verify.sql  
  106.     1  REM the following query is for TAF connection verification  
  107.     2  col sid format 99999  
  108.     3  col serial# format 9999999  
  109.     4  col failover_type format a13  
  110.     5  col failover_method format a15  
  111.     6  col failed_over format a11  
  112.     7  Prompt  
  113.     8  Prompt Failover status for current user  
  114.     9  Prompt ============================================  
  115.    10  SELECT   sid,  
  116.    11   serial#,  
  117.    12   failover_type,  
  118.    13   failover_method,  
  119.    14   failed_over  
  120.    15   FROM   v$session  
  121.    16   WHERE   username = 'FAIL_OVER';  
  122.    17  REM the following query is for load balancing verification  
  123.    18  col host_name format a20  
  124.    19  Prompt  
  125.    20  Prompt Current instance name and host name  
  126.    21  Prompt ========================================  
  127.    22* SELECT   instance_name,host_name FROM v$instance;  
  128.    23    
  129.      
  130.   #下面的連線查詢中表明客戶端當前連線到了節點bo2dbp,其例項名為GOBO4A   
  131.   fail_over@GOBO4> @verify          
  132.     
  133.   Failover status for current user  
  134.   ============================================  
  135.     
  136.      SID  SERIAL# FAILOVER_TYPE FAILOVER_METHOD FAILED_OVER  
  137.   ------ -------- ------------- --------------- -----------  
  138.     1073       48 SELECT        BASIC           NO  
  139.     
  140.     
  141.   Current instance name and host name  
  142.   ========================================  
  143.     
  144.   INSTANCE_NAME    HOST_NAME  
  145.   ---------------- --------------------  
  146.   GOBO4A           bo2dbp    
  147.     
  148.   #此時停止節點bo2dbp  
  149.   oracle@bo2dbp:~> srvctl stop instance -d GOBO4 -i GOBO4A  
  150.   #檢視停止節點bo2dbp即例項GOBO4A後的結果  
  151.   oracle@bo2dbp:~> ./crs_stat.sh |grep inst  
  152.   ora.GOBO4.GOBO4A.inst                         OFFLINE    OFFLINE             
  153.   ora.GOBO4.GOBO4B.inst                         ONLINE     ONLINE on bo2dbs    
  154.     
  155.   #此時回到客戶端再次執行查詢,FAILED_OVER的值已經變成YES,即表明當前的session為failover過來的  
  156.   #同時例項名和節點名也發生了變化  
  157.   fail_over@GOBO4> set timing on;  
  158.   fail_over@GOBO4> @verify  
  159.     
  160.   Failover status for current user  
  161.   ============================================  
  162.     
  163.      SID  SERIAL# FAILOVER_TYPE FAILOVER_METHOD FAILED_OVER  
  164.   ------ -------- ------------- --------------- -----------  
  165.     1082      396 SELECT        BASIC           YES  
  166.     
  167.   Elapsed: 00:00:04.19  
  168.     
  169.   Current instance name and host name  
  170.   ========================================  
  171.     
  172.   INSTANCE_NAME    HOST_NAME  
  173.   ---------------- --------------------  
  174.   GOBO4B           bo2dbs  
  175.     
  176.   Elapsed: 00:00:00.01  
  177.   
  178. 4、小結  
  179.   a、伺服器端的TAF方式的failover透過在伺服器端配置service來完成  
  180.   b、伺服器端的TAF方式與客戶端的TAF方式產生同樣的效果  
  181.   b、一旦在服務端配置了基於伺服器端的TAF,客戶端再無需透過在客戶端新增FAILOVER_MODE項  
  182.   c、該方式簡化客戶端配置,透過集中統一管理service實現failover  




Oracle RAC 負載均衡測試(結合伺服器端與客戶端)


      Oracle RAC 負載均衡使得從客戶端發起的連線能夠有效地分配到監聽器負載較小的例項上。有兩種方式實現客戶端負載均衡,一是透過配置客戶端的load_balance,一是透過配置伺服器端的remote_listener引數。兩種方式各有優劣,而且兩者並不相互排斥,因此可以結合兩種方式來更加有效的實現負載均衡。本文將描述兩者結合的使用情況(oralce 10g rac)。

        有關客戶端與服務端負載均衡的單獨測試請參考:
              Oracle RAC 客戶端連線負載均衡(Load Balance) 
              Oracle RAC 伺服器端連線負載均衡(Load Balance)

        本文的測試將結合前篇文章使用的指令碼與樣例,是前兩篇測試的一個總結。

 

一、配置需求

[sql] view plain copy
 print?
  1. 1、伺服器端各節點監聽器正常提供服務,如果使用非預設的1521埠,請參考 ORACLE RAC 下非預設埠監聽配置(listener.ora tnsnames.ora)   
  2.   oracle@bo2dbp:~> more $ORACLE_HOME/network/admin/listener.ora  
  3.   # listener.ora.bo2dbp Network Configuration File: /u01/oracle/db/network/admin/listener.ora.bo2dbp  
  4.   # Generated by Oracle configuration tools.  
  5.     
  6.   LISTENER_BO2DBP =  
  7.     (DESCRIPTION_LIST =  
  8.       (DESCRIPTION =  
  9.         (ADDRESS = (PROTOCOL = TCP)(HOST = bo2dbp-vip.2gotrade.com)(PORT = 1521)(IP = FIRST))  
  10.         (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.51)(PORT = 1521)(IP = FIRST))  
  11.       )  
  12.     )  
  13.     
  14.   SID_LIST_LISTENER_BO2DBP =  
  15.     (SID_LIST =  
  16.       (SID_DESC =  
  17.         (SID_NAME = PLSExtProc)  
  18.         (ORACLE_HOME = /u01/oracle/db)  
  19.         (PROGRAM = extproc)  
  20.       )  
  21.     )  
  22.     
  23.   oracle@bo2dbp:~> lsnrctl status  
  24.   Service "GOBO4" has 2 instance(s).  
  25.     Instance "GOBO4A", status READY, has 2 handler(s) for this service...  
  26.     Instance "GOBO4B", status READY, has 1 handler(s) for this service...  
  27.       
  28. 2、伺服器端的remote_listener引數設定  
  29.   要求remote_listener引數的連線識別符號在伺服器端的tnsnames.ora中有對應的條目  
  30.     
  31.   SQL> show parameter listener  
  32.     
  33.   NAME                                 TYPE        VALUE  
  34.   ------------------------------------ ----------- ------------------------------  
  35.   local_listener                       string  
  36.   remote_listener                      string      remote_lsnr_gobo4  
  37.   
  38.   oracle@bo2dbp:~> more $ORACLE_HOME/network/admin/tnsnames.ora  
  39.   # tnsnames.ora Network Configuration File: /u01/oracle/db/network/admin/tnsnames.ora  
  40.   # Generated by Oracle configuration tools.  
  41.   remote_lsnr_gobo4 =  
  42.     (ADDRESS_LIST =  
  43.       (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.61)(PORT = 1521))  
  44.       (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.62)(PORT = 1521))  
  45.     )  
  46.   
  47. 3、客戶端tnsnames.ora中啟用load_balance  
  48.   oracle@SZDB:~> tail -11 $ORACLE_HOME/network/admin/tnsnames.ora  
  49.     
  50.   GOBO4 =  
  51.     (DESCRIPTION =  
  52.       (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.61)(PORT = 1521))  
  53.       (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.62)(PORT = 1521))  
  54.       (LOAD_BALANCE = yes)  
  55.       (CONNECT_DATA =  
  56.         (SERVER = DEDICATED)  
  57.         (SERVICE_NAME = GOBO4)  
  58.       )  
  59.     )  

二、測試Load Balance

[python] view plain copy
 print?
  1. #還是使用之前的腳步來進行測試  
  2. #Author : Robinson  
  3. #Blog : http://blog.csdn.net/robinson_0612  
  4. oracle@SZDB:~> more load_balance.sh   
  5. #!/bin/bash  
  6. for i in {1..1000}  
  7. do  
  8. echo $i  
  9. sqlplus -S system/oracle@GOBO4 <<EOF  
  10. select instance_name from v\$instance;  
  11. EOF  
  12. sleep 1  
  13. done  
  14. exit 0  
  15.   
  16. oracle@SZDB:~> ./load_balance.sh >load_bal.log     
  17.   
  18. #檢視日誌  
  19. oracle@SZDB:~> grep GOBO4A load_bal.log |wc -l  
  20. 750  
  21. oracle@SZDB:~> grep GOBO4B load_bal.log |wc -l  
  22. 250  
  23.   
  24. #檢視監聽器的日誌  
  25. oracle@bo2dbp:/u01/oracle/db/network/log> grep establish listener_bo2dbp.log |wc -l  
  26. 894  
  27.   
  28. oracle@bo2dbp:/u01/oracle/db/network/log> grep INSTANCE_NAME=GOBO4 listener_bo2dbp.log |wc -l  
  29. 415  
  30.   
  31. #從上面的查詢中可以得知,節點bo2dbp總共接受了894個連線請求,而有415連線請求是由bo2dbs轉發過來的  
  32. #因此,實際上從客戶端發起到bo2dbp的實際連線請求數為894-415=479  
  33.   
  34. #下面來看在節點bo2dbs上的監聽日誌  
  35. oracle@bo2dbs:/u01/oracle/db/network/log> grep establish listener_bo2dbs.log |wc -l  
  36. 665  
  37.   
  38. oracle@bo2dbs:/u01/oracle/db/network/log> grep INSTANCE_NAME=GOBO4 listener_bo2dbs.log |wc -l   
  39. 144  
  40. #從上面的查詢中可知,節點bo2dbs總共接受了665個連線請求,而有144個連線請求是由bo2dbp轉發過來的  
  41. #因此,實際上從客戶端發起到bo2dbs的實際連線請求數為655-144=511  
  42.   
  43. #從上面的結果可知,  
  44. #基於客戶端的連線請求數為節點bo2dbp為479,節點bo2dbs為511  
  45. #監聽器路由到本地例項數目為,節點bo2dbp,479-144=335,節點bo2dbs,511-415=96  
  46. #遠端監聽器路由道本地例項的資料為,節點bo2dpb為415,節點bo2dbs為144  
  47. #監聽器路由的概念是指基於伺服器端的負載均衡  
  48. #即伺服器端的監聽器根據自身以及遠端監聽器的負載情況來確定將當前的連線請求轉發到本地或遠端,此即為路由。  




 

Oracle RAC failover 測試(連線時故障轉移)



  Oracle RAC 叢集最突出的表現就是高可用性,這些內容主要包括load balance以及failover,透過這些技術使得單點故障不影響客戶端端應用程式對資料庫的正常訪問,以及透過建立service實現節點間負載均衡。本文主要描述Oracle 10g rac環境下的Oracle failover測試。
  
  下面是一些關於這方面的基礎參考或相關連結:
  有關負監聽配置,載均衡(load balance)以及Oracle service請參考
    ORACLE RAC 監聽配置 (listener.ora tnsnames.ora) 
    ORACLE RAC 下非預設埠監聽配置(listener.ora tnsnames.ora) 
    Oracle RAC 客戶端連線負載均衡(Load Balance)
    Oracle RAC 伺服器端連線負載均衡(Load Balance)
    Oracle RAC 負載均衡測試(結合伺服器端與客戶端)
    Oracle RAC failover 測試(TAF方式) 
    Oracle RAC failover 測試(Server TAF方式)

[python] view plain copy
 print?
  1. 1、Oracle failover的幾種方式  
  2.   Oracle failover也叫故障轉移,從Oracle 10g開始,分為3種方式:  
  3.   a. Client-Side Connect time Failover  
  4.     客戶端連線failover模式,此方式較為簡單,只要安裝了rac叢集,預設情況下即被啟用。  
  5.   b. TAF  
  6.     透明故障轉移,此方式同樣基於客戶端完成,需要配置客戶端tnsnames.ora,連線故障發生時,無須重新連線  
  7.   c. Service-Side TAF  
  8.     伺服器端透明故障轉移,透過配置service來實現,客戶端無須任何配置。  
  9.     
  10.   本文主要演示第一種情形,即客戶端在發起連線請求時如何實現故障轉移      
  11.   注意事項: 不能在listener.ora 檔案中設定GLOBAL_NAME  
  12.          該引數會禁用Connect-time Failover 和 Transparent Application Failover  
  13.   
  14. 2、Client-Side Connect time Failover  
  15.   下面關於Client-Side Connect time Failover來自Oracle 的官方描述 ID 453293.1  
  16.   The connect-time failover enables clients to connect to another listener if the initial connection to the first   
  17.   listener fails. The number of listener protocol addresses determines how many listeners are tried. Without   
  18.   connect-time failover, Oracle Net attempts a connection with only one listener. The default is on.   
  19.   
  20.   Tnsnames Parameter: FAILOVER   
  21.     
  22.   (failover=on) is default for ADDRESS_LISTs, DESCRIPTION_LISTs, and a set of   
  23.   DESCRIPTIONs., therefore, you do not have to specify it explicitly.   
  24.   
  25.   基於客戶端的failover比較好理解。對於在客戶端tnsnames.ora有多個VIP的情形,客戶端會首先請求定位到第一個VIP,如果第一個VIP不  
  26.   可達,則繼續嘗試使用下一個VIP,直到成功建立連線,如果所有的VIP無法連線將收到錯誤訊息。  
  27.   通常情況下,我們使用vip作為tnsnames.ora中的連線地址  
  28.   
  29. 3、伺服器端、客戶端的環境  
  30.   #伺服器端環境,host資訊  
  31.   oracle@bo2dbp:~> cat /etc/hosts |grep vip  
  32.   192.168.7.61   bo2dbp-vip.2gotrade.com    bo2dbp-vip  
  33.   192.168.7.62   bo2dbs-vip.2gotrade.com    bo2dbs-vip  
  34.     
  35.   #伺服器端環境,叢集資訊  
  36.   oracle@bo2dbp:~> ./crs_stat.sh   
  37.    Resource name                                Target     State               
  38.   --------------                                ------     -----               
  39.   ora.GOBO4.GOBO4A.inst                         ONLINE     ONLINE on bo2dbp    
  40.   ora.GOBO4.GOBO4B.inst                         ONLINE     ONLINE on bo2dbs    
  41.   ora.GOBO4.db                                  ONLINE     ONLINE on bo2dbp    
  42.   ora.bo2dbp.ASM1.asm                           ONLINE     ONLINE on bo2dbp    
  43.   ora.bo2dbp.LISTENER_BO2DBP.lsnr               ONLINE     ONLINE on bo2dbp    
  44.   ora.bo2dbp.LISTENER_ORA10G_BO2DBP.lsnr        ONLINE     ONLINE on bo2dbp    
  45.   ora.bo2dbp.gsd                                ONLINE     ONLINE on bo2dbp    
  46.   ora.bo2dbp.ons                                ONLINE     ONLINE on bo2dbp    
  47.   ora.bo2dbp.vip                                ONLINE     ONLINE on bo2dbp    
  48.   ora.bo2dbs.ASM2.asm                           ONLINE     ONLINE on bo2dbs    
  49.   ora.bo2dbs.LISTENER_BO2DBS.lsnr               ONLINE     ONLINE on bo2dbs    
  50.   ora.bo2dbs.LISTENER_ORA10G_BO2DBS.lsnr        ONLINE     ONLINE on bo2dbs    
  51.   ora.bo2dbs.gsd                                ONLINE     ONLINE on bo2dbs    
  52.   ora.bo2dbs.ons                                ONLINE     ONLINE on bo2dbs    
  53.   ora.bo2dbs.vip                                ONLINE     ONLINE on bo2dbs    
  54.   ora.ora10g.db                                 ONLINE     ONLINE on bo2dbp   
  55.   
  56.   #客戶端環境  
  57.   robin@SZDB:~> cat /etc/issue  
  58.     
  59.   Welcome to SUSE Linux Enterprise Server 10 SP3 (x86_64) - Kernel \r (\l).  
  60.     
  61.   robin@SZDB:~> sqlplus -v  
  62.     
  63.   SQL*Plus: Release 10.2.0.3.0 - Production  
  64.     
  65.   #客戶端tnsnames配置  
  66.   GOBO4 =  
  67.     (DESCRIPTION =  
  68.       (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.61)(PORT = 1521))  
  69.       (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.62)(PORT = 1521))  
  70.       (LOAD_BALANCE = yes)  
  71.       (CONNECT_DATA =  
  72.         (SERVER = DEDICATED)  
  73.         (SERVICE_NAME = GOBO4)  
  74.       )  
  75.     )  
  76.   
  77. 4、連線測試    
  78.   #首次建立連線,此時客戶端從tnsnames配置的第一個IP建立連線,即192.168.7.61  
  79.   #VIP 192.168.7.61對應的hostname以及instance_name分別為bo2dbp,GOBO4A,所以我們獲得如下返回結果  
  80.   robin@SZDB:~> sqlplus fail_over/fail@gobo4  
  81.   
  82.   fail_over@GOBO4> get verify.sql  
  83.     1  REM the following query is for TAF connection verification  
  84.     2  col sid format 99999  
  85.     3  col serial# format 9999999  
  86.     4  col failover_type format a13  
  87.     5  col failover_method format a15  
  88.     6  col failed_over format a11  
  89.     7  Prompt  
  90.     8  Prompt Failover status for current user  
  91.     9  Prompt ============================================  
  92.    10  SELECT   sid,  
  93.    11   serial#,  
  94.    12   failover_type,  
  95.    13   failover_method,  
  96.    14   failed_over  
  97.    15   FROM   v$session  
  98.    16   WHERE   username = 'FAIL_OVER';  
  99.    17  REM the following query is for load balancing verification  
  100.    18  col host_name format a20  
  101.    19  Prompt  
  102.    20  Prompt Current instance name and host name  
  103.    21  Prompt ========================================  
  104.    22* SELECT   instance_name,host_name FROM v$instance;  
  105.    23    
  106.      
  107.   fail_over@GOBO4> @verify.sql  
  108.     
  109.   Failover status for current user  
  110.   ============================================  
  111.      SID  SERIAL# FAILOVER_TYPE FAILOVER_METHOD FAILED_OVER  
  112.   ------ -------- ------------- --------------- -----------  
  113.     1071      249 NONE          NONE            NO  
  114.     
  115.     
  116.   Current instance name and host name  
  117.   ========================================  
  118.   INSTANCE_NAME    HOST_NAME  
  119.   ---------------- --------------------  
  120.   GOBO4A           bo2dbp  
  121.   
  122.   #停止叢集資料庫的第一個instance,即GOBO4A  
  123.   oracle@bo2dbp:~> srvctl stop instance -d GOBO4 -i GOBO4A  
  124.   #校驗結果  
  125.   oracle@bo2dbp:~> ./crs_stat.sh | grep inst  
  126.   ora.GOBO4.GOBO4A.inst                         OFFLINE    OFFLINE             
  127.   ora.GOBO4.GOBO4B.inst                         ONLINE     ONLINE on bo2dbs   
  128.     
  129.   #回到客戶端原來的session,此時出現ORA-03114  
  130.   fail_over@GOBO4> /  
  131.   SELECT   instance_name,host_name FROM v$instance  
  132.   *  
  133.   ERROR at line 1:  
  134.   ORA-03113: end-of-file on communication channel  
  135.     
  136.   fail_over@GOBO4> /  
  137.   ERROR:  
  138.   ORA-03114not connected to ORACLE  
  139.     
  140.   ERROR:  
  141.   ORA-03114not connected to ORACLE  
  142.   
  143.   #下面嘗試重新建立連線  
  144.   fail_over@GOBO4> conn fail_over/fail@gobo4  
  145.   Connected.  
  146.   fail_over@GOBO4> @verify  
  147.     
  148.   Failover status for current user  
  149.   ============================================  
  150.      SID  SERIAL# FAILOVER_TYPE FAILOVER_METHOD FAILED_OVER  
  151.   ------ -------- ------------- --------------- -----------  
  152.     1062       94 NONE          NONE            NO  
  153.     
  154.     
  155.   Current instance name and host name  
  156.   ========================================  
  157.   INSTANCE_NAME    HOST_NAME  
  158.   ---------------- --------------------  
  159.   GOBO4B           bo2dbs  
  160.     
  161.   #Author : Robinson  
  162.   #Blog   : http://blog.csdn.net/robinson_0612  
  163.     
  164.   #從上面的查詢可知,當前的session已經連線到第二個例項。且FAILOVER_TYPE,FAILOVER_METHOD,FAILED_OVER幾個至沒有發生任何變化  
  165.   #因為這幾個引數主要是針對TAF。  
  166.   
  167. 5、小結  
  168.   a、客戶端連線時的故障轉移,伺服器端和客戶端無需任何配置,預設情況下即被開啟,即failover=on  
  169.   b、只要叢集環境存在(非單節點RAC),客戶端的連線請求會逐個嘗試列出的VIP,直到連線成功為止,如果所有不可連線,返回錯誤  
  170.   c、客戶端已經建立後,伺服器端例項或節點故障,都將導致客戶端必須重新發起新的連線請求  





Oracle RAC failover 測試(TAF方式)


 Oracle RAC 客戶端故障轉移(failover),當採用TAF方式時,對於已經建立連線的客戶端,在連線的例項或節點出現故障時,客戶端無需再次發出連線請求,仍然可以繼續之前的資料庫操作,此稱之為透明故障轉移。本文描述基於Oracle 10g rac,客戶端TAF方式的故障轉移並給出示例。

  下面是一些關於這方面的基礎參考連結:
  有關負監聽配置,載均衡(load balance)以及Oracle service請參考
    ORACLE RAC 監聽配置 (listener.ora tnsnames.ora) 
    ORACLE RAC 下非預設埠監聽配置(listener.ora tnsnames.ora) 
    Oracle RAC 客戶端連線負載均衡(Load Balance)
    Oracle RAC 伺服器端連線負載均衡(Load Balance)
    Oracle RAC 負載均衡測試(結合伺服器端與客戶端) 
    
  有關Oracle RAC failover 連線時故障轉移請參考
    Oracle RAC failover 測試(連線時故障轉移)
    Oracle RAC failover 測試(Server TAF方式)

[python] view plain copy
 print?
  1. 1、TAF描述      
  2.   #下面關於TAF來自Oracle 的官方描述 ID 453293.1  
  3.   Transparent Application Failover (TAF) is a feature of the Oracle Call Interface (OCI) driver at client side.   
  4.   It enables the application to automatically reconnect to a database, if the database instance to which the connection is   
  5.   made fails. In this case, the active transactions roll back.   
  6.   Tnsnames Parameter: FAILOVER_MODE   
  7.   
  8.      When an instance to which a connection is established fails or is shutdown, the connection on the client side becomes   
  9.   stale and would throw exceptions to the caller trying to use it. TAF enables the application to transparently reconnect   
  10.   to a preconfigured secondary instance creating a fresh connection, but identical to the connection that was established   
  11.   on the first original instance.  
  12.     
  13.   #簡單一點來說,就是說對於那些已經成功連線到特定例項的客戶端,如果該例項或節點異常當機,客戶端會自動重新發出到剩餘例項的連  
  14.   #接請求。使得客戶端感覺不到它所連線的例項或節點已經出現故障,這個就稱之為透明轉移。但其間的活動事務將被回滾。  
  15.   #透過在客戶端的tnsnames.ora中配置FAILOVER_MODE項實現TAF  
  16.   
  17. 2、伺服器端、客戶端的環境  
  18.   #伺服器端環境,host資訊  
  19.   oracle@bo2dbp:~> cat /etc/hosts |grep vip  
  20.   192.168.7.61   bo2dbp-vip.2gotrade.com    bo2dbp-vip  
  21.   192.168.7.62   bo2dbs-vip.2gotrade.com    bo2dbs-vip  
  22.     
  23.   #伺服器端環境,叢集資訊  
  24.   oracle@bo2dbp:~> ./crs_stat.sh   
  25.    Resource name                                Target     State               
  26.   --------------                                ------     -----               
  27.   ora.GOBO4.GOBO4A.inst                         OFFLINE    OFFLINE on bo2dbp  #此時節點1上的例項被關閉  
  28.   ora.GOBO4.GOBO4B.inst                         ONLINE     ONLINE on bo2dbs    
  29.   ora.GOBO4.db                                  ONLINE     ONLINE on bo2dbp    
  30.   ora.bo2dbp.ASM1.asm                           ONLINE     ONLINE on bo2dbp    
  31.   ora.bo2dbp.LISTENER_BO2DBP.lsnr               ONLINE     ONLINE on bo2dbp    
  32.   ora.bo2dbp.LISTENER_ORA10G_BO2DBP.lsnr        ONLINE     ONLINE on bo2dbp    
  33.   ora.bo2dbp.gsd                                ONLINE     ONLINE on bo2dbp    
  34.   ora.bo2dbp.ons                                ONLINE     ONLINE on bo2dbp    
  35.   ora.bo2dbp.vip                                ONLINE     ONLINE on bo2dbp    
  36.   ora.bo2dbs.ASM2.asm                           ONLINE     ONLINE on bo2dbs    
  37.   ora.bo2dbs.LISTENER_BO2DBS.lsnr               ONLINE     ONLINE on bo2dbs    
  38.   ora.bo2dbs.LISTENER_ORA10G_BO2DBS.lsnr        ONLINE     ONLINE on bo2dbs    
  39.   ora.bo2dbs.gsd                                ONLINE     ONLINE on bo2dbs    
  40.   ora.bo2dbs.ons                                ONLINE     ONLINE on bo2dbs    
  41.   ora.bo2dbs.vip                                ONLINE     ONLINE on bo2dbs    
  42.   ora.ora10g.db                                 ONLINE     ONLINE on bo2dbp   
  43.   
  44.   #客戶端環境  
  45.   robin@SZDB:~> cat /etc/issue  
  46.     
  47.   Welcome to SUSE Linux Enterprise Server 10 SP3 (x86_64) - Kernel \r (\l).  
  48.     
  49.   robin@SZDB:~> sqlplus -v  
  50.     
  51.   SQL*Plus: Release 10.2.0.3.0 - Production  
  52.     
  53.   #客戶端tnsnames配置    
  54.   GOBO4_TAF =  
  55.    (DESCRIPTION =  
  56.      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.61)(PORT = 1521))  
  57.      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.62)(PORT = 1521))  
  58.      (LOAD_BALANCE = yes)  
  59.      (CONNECT_DATA =  
  60.        (SERVER = DEDICATED)  
  61.        (SERVICE_NAME = GOBO4)  
  62.       (FAILOVER_MODE =       #FAILOVER_MODE項引數   
  63.        (TYPE = session)  
  64.        (METHOD = basic)  
  65.        (RETRIES = 180  
  66.        (DELAY = 5)  
  67.       )  
  68.      )  
  69.    )  
  70.     
  71. 3、FAILOVER_MODE項分析  
  72.   FAILOVER_MODE項是實現TAF的主要配置內容,下面對其進行描述.  
  73.     
  74.   METHOD: 使用者定義何時建立到其例項的連線,有BASIC 和 PRECONNECT 兩種可選值  
  75.     BASIC: 客戶端透過地址列表成功建立連線後,即僅當客戶端感知到節點故障時才建立到其他例項的連線  
  76.     PRECONNECT: 預連線模式,是在最初建立連線時就同時建立到所有例項的連線,當發生故障時,立刻就可以切換到其他鏈路上  
  77.       
  78.     上述兩種方式各有優劣,前者建立連線的開銷相對較小,但failover時會產生延遲,而後者正好與前者相反  
  79.       
  80.   TYPE: 用於定義發生故障時對完成的SQL 語句如何處理,其中有2種型別:session 和select  
  81.     select:使用select方式,Oracle net會跟蹤事務期間的所有select語句,並跟蹤每一個與當前select相關的遊標已返回多少行給客戶  
  82.       端。此時,假定select查詢已返回500行,客戶端當前連線的節點出現故障,Oracle Net自動建立連線到倖存的例項上並繼續返回  
  83.       剩餘的行數給客戶端。假定總行數為1500,行,則1000行從剩餘節點返回。  
  84.     session: 使用session方式,所有select查詢相關的結果在重新建立新的連線後將全部丟失,需要重新發布select命令。  
  85.       
  86.     上述兩種方式適用於不同的情形,對於select方式,通常使用與OLAP資料庫,而對於session方式則使用與OLTP資料庫。因為select   
  87.     方式,Oracle 必須為每個session儲存更多的內容,包括遊標,使用者上下文等,需要更多的資源。  
  88.       
  89.     其次,兩種方式期間所有未提交的DML事務將自動回滾且必須重啟啟動。alter session語句不會failover。  
  90.     臨時物件不會failover也不能被重新啟動。  
  91.       
  92.   RETRIES: 表示重試的次數  
  93.   DELAY:表示重試的間隔時間  
  94.     
  95. 4、測試TAF      
  96.   #首次建立連線,此時客戶端從tnsnames配置的第一個IP建立連線,由於第一個VIP所在的例項已經關閉,故連線到192.168.7.62  
  97.   #VIP 192.168.7.62對應的hostname以及instance_name分別為bo2dbs,GOBO4B,所以我們獲得如下返回結果  
  98.   #其次我們可以看到當前session failover的相關引數  
  99.   robin@SZDB:~> sqlplus <a href="mailto:fail_over/fail@gobo4_taf">fail_over/fail@gobo4_taf  
  100.   
  101.   fail_over@GOBO4> get verify.sql  
  102.     1  REM the following query is for TAF connection verification  
  103.     2  col sid format 99999  
  104.     3  col serial# format 9999999  
  105.     4  col failover_type format a13  
  106.     5  col failover_method format a15  
  107.     6  col failed_over format a11  
  108.     7  Prompt  
  109.     8  Prompt Failover status for current user  
  110.     9  Prompt ============================================  
  111.    10  SELECT   sid,  
  112.    11   serial#,  
  113.    12   failover_type,  
  114.    13   failover_method,  
  115.    14   failed_over  
  116.    15   FROM   v$session  
  117.    16   WHERE   username = 'FAIL_OVER';  
  118.    17  REM the following query is for load balancing verification  
  119.    18  col host_name format a20  
  120.    19  Prompt  
  121.    20  Prompt Current instance name and host name  
  122.    21  Prompt ========================================  
  123.    22* SELECT   instance_name,host_name FROM v$instance;  
  124.    23    
  125.      
  126.   fail_over@GOBO4> @verify  
  127.     
  128.   Failover status for current user  
  129.   ============================================  
  130.     
  131.      SID  SERIAL# FAILOVER_TYPE FAILOVER_METHOD FAILED_OVER  
  132.   ------ -------- ------------- --------------- -----------  
  133.     1063     1175 SESSION       BASIC           NO  
  134.     
  135.     
  136.   Current instance name and host name  
  137.   ========================================  
  138.     
  139.   INSTANCE_NAME    HOST_NAME  
  140.   ---------------- --------------------  
  141.   GOBO4B           bo2dbs  
  142.     
  143.   #此時啟動第一個例項GOBO4A,並停止第二個例項  
  144.   oracle@bo2dbp:~> srvctl start instance -d GOBO4 -i GOBO4A    
  145.   oracle@bo2dbp:~> srvctl stop instance -d GOBO4 -i GOBO4B  
  146.   #檢視兩個例項的狀態  
  147.   oracle@bo2dbp:~> ./crs_stat.sh | grep inst  
  148.   ora.GOBO4.GOBO4A.inst                         ONLINE     ONLINE on bo2dbp    
  149.   ora.GOBO4.GOBO4B.inst                         OFFLINE    OFFLINE   
  150.     
  151.   #在客戶端的session再次檢查連線狀態,即執行查詢,結果如下,我們收到了ORA-25408  
  152.   fail_over@GOBO4> @verify  
  153.     
  154.   Failover status for current user  
  155.   ============================================  
  156.   SELECT   sid,  
  157.   *  
  158.   ERROR at line 1:  
  159.   ORA-25408: can not safely replay call  
  160.     
  161.   Current instance name and host name  
  162.   ========================================  
  163.     
  164.   INSTANCE_NAME    HOST_NAME  
  165.   ---------------- --------------------  
  166.   GOBO4A           bo2dbp  
  167.     
  168.   #再次執行查詢,此時客戶端已經自動實現了重新連線,從查詢返回得到的INSTANCE_NAME與HOST_NAME可知。  
  169.   #最重要的一個FAILED_OVER值為YES,表明當前的session是一個failover來的session。  
  170.   #關於METHOD使用PRECONNECT與TYPE使用SELECT的方式在此不作演示  
  171.   fail_over@GOBO4> @verify  
  172.     
  173.   Failover status for current user  
  174.   ============================================  
  175.     
  176.      SID  SERIAL# FAILOVER_TYPE FAILOVER_METHOD FAILED_OVER  
  177.   ------ -------- ------------- --------------- -----------  
  178.     1073       29 SESSION       BASIC           YES  
  179.     
  180.     
  181.   Current instance name and host name  
  182.   ========================================  
  183.     
  184.   INSTANCE_NAME    HOST_NAME  
  185.   ---------------- --------------------  
  186.   GOBO4A           bo2dbp   
  187.   
  188.   #Author : Robinson  
  189.   #Blog   : http://blog.csdn.net/robinson_0612  
  190.     
  191. 5、小結:  
  192.   a、客戶端TAF方式實現了Oracle客戶端到伺服器透明故障轉移  
  193.   b、主要在客戶端tnsnames.ora配置FAILOVER_MODE來實現基於客戶端的TAF  
  194.   c、FAILOVER_MODE中基於連線方式(METHOD)可以分為BASIC與PRECONNECT兩種方式,後者開銷更大,延遲小,與前者相反  
  195.   d、FAILOVER_MODE中TYPE可以分為select與session兩種方式,兩者所有未提交的事務全部回滾,select方式會failover查詢,  
  196.      session方式不會。select方式多用在OLAP型別資料庫,而session多用在OLTP型別資料庫  
  197.   e、一旦所在的例項發生故障,會自動failover,無需手動重新連線,這就是與連線時故障轉移所不同的。     










About Me

...............................................................................................................................

● 本文整理自網路

● 本文在itpub(http://blog.itpub.net/26736162)、部落格園(http://www.cnblogs.com/lhrbest)和個人微信公眾號(xiaomaimiaolhr)上有同步更新

● 本文itpub地址:http://blog.itpub.net/26736162/abstract/1/

● 本文部落格園地址:http://www.cnblogs.com/lhrbest

● 本文pdf版及小麥苗雲盤地址:http://blog.itpub.net/26736162/viewspace-1624453/

● 資料庫筆試面試題庫及解答:http://blog.itpub.net/26736162/viewspace-2134706/

● QQ群:230161599     微信群:私聊

● 聯絡我請加QQ好友(646634621),註明新增緣由

● 於 2017-07-01 09:00 ~ 2017-07-31 22:00 在魔都完成

● 文章內容來源於小麥苗的學習筆記,部分整理自網路,若有侵權或不當之處還請諒解

● 版權所有,歡迎分享本文,轉載請保留出處

...............................................................................................................................

拿起手機使用微信客戶端掃描下邊的左邊圖片來關注小麥苗的微信公眾號:xiaomaimiaolhr,掃描右邊的二維碼加入小麥苗的QQ群,學習最實用的資料庫技術。

【RAC】RAC中的負載均衡和故障切換--TAF配置
DBA筆試面試講解
歡迎與我聯絡

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26736162/viewspace-2142102/,如需轉載,請註明出處,否則將追究法律責任。

相關文章