Implement Oracle Datagurad Switchover

在生產環境中,經常會遇到各種情況,需要臨時對調下主,備庫的角色來滿足複雜的生產環境要求!本文簡單的對比下使用手工切換和使用dataguard broker工具切換的步驟!

一:手工切換
1:確認主庫(dg1)和備庫(dg2)的日誌應用狀態正常,主庫的switchover_status應當爲to_standby

  1. SQL> select db_unique_name,open_mode,database_role,switchover_status from v$database;  
  2.  
  3. DB_UNIQUE_NAME       OPEN_MODE            DATABASE_ROLE    SWITCHOVER_STATUS  
  4. -------------------- -------------------- ---------------- --------------------  
  5. dg1                  READ WRITE           PRIMARY          TO STANDBY  
  6.  
  7. SQL> archive log list;  
  8. Database log mode              Archive Mode  
  9. Automatic archival             Enabled  
  10. Archive destination            /u01/app/oracle/standbylog  
  11. Oldest online log sequence     16  
  12. Next log sequence to archive   18  
  13. Current log sequence           18  
  14.  
  15. SQL> select db_unique_name,open_mode,database_role,switchover_status from v$database;  
  16.  
  17. DB_UNIQUE_NAME       OPEN_MODE            DATABASE_ROLE    SWITCHOVER_STATUS  
  18. -------------------- -------------------- ---------------- --------------------  
  19. dg2                  READ ONLY WITH APPLY PHYSICAL STANDBY NOT ALLOWED  
  20.  
  21. SQL> archive log list;  
  22. Database log mode              Archive Mode  
  23. Automatic archival             Enabled  
  24. Archive destination            /u01/app/oracle/standbylog  
  25. Oldest online log sequence     17  
  26. Next log sequence to archive   0  
  27. Current log sequence           18 

2:在dg1上執行switchover操作後,重新啓動dg1到mount狀態

  1. SQL> alter database commit to switchover to physical standby;  
  2. Database altered.  
  3.  
  4. SQL> select db_unique_name,open_mode,database_role,switchover_status from v$database;  
  5.  
  6. DB_UNIQUE_NAME       OPEN_MODE            DATABASE_ROLE    SWITCHOVER_STATUS  
  7. -------------------- -------------------- ---------------- --------------------  
  8. dg1                  READ WRITE           PHYSICAL STANDBY RECOVERY NEEDED 

[oracle@dg1 ~]$ tail -f /u01/app/oracle/diag/rdbms/dg1/dg/trace/alert_dg.log
Mon Apr 30 11:59:21 2012
alter database commit to switchover to physical standby
ALTER DATABASE COMMIT TO SWITCHOVER TO PHYSICAL STANDBY [Process Id: 13575] (dg)
Mon Apr 30 11:59:21 2012
LGWR: Standby redo logfile selected to archive thread 1 sequence 19
LGWR: Standby redo logfile selected for thread 1 sequence 19 for destination LOG_ARCHIVE_DEST_2
Thread 1 advanced to log sequence 19 (LGWR switch)
  Current log# 1 seq# 19 mem# 0: /u01/app/oracle/oradata/dg/redo01.log
Mon Apr 30 11:59:21 2012
Stopping background process CJQ0
Stopping background process QMNC
Mon Apr 30 11:59:22 2012
Archived Log entry 26 added for thread 1 sequence 18 ID 0x6501e627 dest 1:
All dispatchers and shared servers shutdown
CLOSE: killing server sessions.
CLOSE: all sessions shutdown successfully.
Waiting for all non-current ORLs to be archived...
All non-current ORLs have been archived.
Waiting for all FAL entries to be archived...
All FAL entries have been archived.
Waiting for potential Physical Standby switchover target to become synchronized...
Active, synchronized Physical Standby switchover target has been identified
Switchover End-Of-Redo Log thread 1 sequence 19 has been fixed
Switchover: Primary highest seen SCN set to 0x0.0x10758d
ARCH: Noswitch archival of thread 1, sequence 19
ARCH: End-Of-Redo Branch archival of thread 1 sequence 19
ARCH: LGWR is actively archiving destination LOG_ARCHIVE_DEST_2
ARCH: Standby redo logfile selected for thread 1 sequence 19 for destination LOG_ARCHIVE_DEST_2
Archived Log entry 27 added for thread 1 sequence 19 ID 0x6501e627 dest 1:
ARCH: Archiving is disabled due to current logfile archival
Primary will check for some target standby to have received alls redo
Final check for a synchronized target standby. Check will be made once.
LOG_ARCHIVE_DEST_2 is a potential Physical Standby switchover target
Active, synchronized target has been identified
Target has also received all redo
Backup controlfile written to trace file /u01/app/oracle/diag/rdbms/dg1/dg/trace/dg_ora_13575.trc
Clearing standby activation ID 1694623271 (0x6501e627)
The primary database controlfile was created using the
'MAXLOGFILES 16' clause.
There is space for up to 13 standby redo logfiles
Use the following SQL commands on the standby database to create
standby redo logfiles that match the primary database:
ALTER DATABASE ADD STANDBY LOGFILE 'srl1.f' SIZE 52428800;
ALTER DATABASE ADD STANDBY LOGFILE 'srl2.f' SIZE 52428800;
ALTER DATABASE ADD STANDBY LOGFILE 'srl3.f' SIZE 52428800;
ALTER DATABASE ADD STANDBY LOGFILE 'srl4.f' SIZE 52428800;
Archivelog for thread 1 sequence 19 required for standby recovery
Switchover: Primary controlfile converted to standby controlfile succesfully.
Switchover: Complete - Database shutdown required
Completed: alter database commit to switchover to physical standby
Mon Apr 30 11:59:53 2012
Process (ospid 13546) is suspended due to switchover to physical standby operation.
Mon Apr 30 11:59:57 2012
Process (ospid 13581) is suspended due to switchover to physical standby operation
.

 

  1. SQL> shutdown immediate  
  2. SQL> startup mount  
  3.  
  4. SQL> select db_unique_name,open_mode,database_role,switchover_status from v$database;  
  5.  
  6. DB_UNIQUE_NAME       OPEN_MODE            DATABASE_ROLE    SWITCHOVER_STATUS  
  7. -------------------- -------------------- ---------------- --------------------  
  8. dg1                  MOUNTED              PHYSICAL STANDBY RECOVERY NEEDED 

3:在dg2上執行switchover到主庫的操作,執行完後將dg2啓動到讀寫狀態

  1. SQL> select db_unique_name,open_mode,database_role,switchover_status from v$database;  
  2.  
  3. DB_UNIQUE_NAME       OPEN_MODE            DATABASE_ROLE    SWITCHOVER_STATUS  
  4. -------------------- -------------------- ---------------- --------------------  
  5. dg2                  READ ONLY WITH APPLY PHYSICAL STANDBY TO PRIMARY  
  6.  
  7. SQL> alter database commit to switchover to primary;  
  8. Database altered.  
  9.  
  10. SQL> select db_unique_name,open_mode,database_role,switchover_status from v$database;  
  11.  
  12. DB_UNIQUE_NAME       OPEN_MODE            DATABASE_ROLE    SWITCHOVER_STATUS  
  13. -------------------- -------------------- ---------------- --------------------  
  14. dg2                  MOUNTED              PRIMARY          NOT ALLOWED  
  15.  
  16. SQL> alter database open;  
  17. Database altered.  
  18.  
  19. SQL> select db_unique_name,open_mode,database_role,switchover_status from v$database;  
  20.  
  21. DB_UNIQUE_NAME       OPEN_MODE            DATABASE_ROLE    SWITCHOVER_STATUS  
  22. -------------------- -------------------- ---------------- --------------------  
  23. dg2                  READ WRITE           PRIMARY          TO STANDBY 

[oracle@dg2 ~]$ tail -f /u01/app/oracle/diag/rdbms/dg2/dg/trace/alert_dg.log
Mon Apr 30 12:03:40 2012
alter database commit to switchover to primary
ALTER DATABASE SWITCHOVER TO PRIMARY (dg)
Maximum wait for role transition is 15 minutes.
Switchover: Media recovery is still active
Role Change: Canceling MRP - no more redo to apply
Mon Apr 30 12:03:42 2012
MRP0: Background Media Recovery cancelled with status 16037
Errors in file /u01/app/oracle/diag/rdbms/dg2/dg/trace/dg_pr00_13555.trc:
ORA-16037: user requested cancel of managed recovery operation
Managed Standby Recovery not using Real Time Apply
Recovery interrupted!
Mon Apr 30 12:03:42 2012
MRP0: Background Media Recovery process shutdown (dg)
Role Change: Canceled MRP
All dispatchers and shared servers shutdown
CLOSE: killing server sessions.
CLOSE: all sessions shutdown successfully.
Mon Apr 30 12:03:42 2012
SMON: disabling cache recovery
Backup controlfile written to trace file /u01/app/oracle/diag/rdbms/dg2/dg/trace/dg_ora_13547.trc
SwitchOver after complete recovery through change 1078669
Online logfile pre-clearing operation disabled by switchover
Online log /u01/app/oracle/fast_recovery_area/DG2/onlinelog/o1_mf_1_7s76q5w1_.log: Thread 1 Group 1 was

previously cleared
Online log /u01/app/oracle/fast_recovery_area/DG2/onlinelog/o1_mf_2_7s76q94s_.log: Thread 1 Group 2 was

previously cleared
Online log /u01/app/oracle/fast_recovery_area/DG2/onlinelog/o1_mf_3_7s76qbhq_.log: Thread 1 Group 3 was

previously cleared
Standby became primary SCN: 1078667
AUDIT_TRAIL initialization parameter is changed back to its original value as specified in the parameter file.
Switchover: Complete - Database mounted as primary
Completed: alter database commit to switchover to primary

4:將dg1啓動到read only with apply狀態下,完成切換操作

  1. SQL> select db_unique_name,open_mode,database_role,switchover_status from v$database;  
  2.  
  3. DB_UNIQUE_NAME       OPEN_MODE            DATABASE_ROLE    SWITCHOVER_STATUS  
  4. -------------------- -------------------- ---------------- --------------------  
  5. dg1                  MOUNTED              PHYSICAL STANDBY RECOVERY NEEDED  
  6.  
  7. SQL> alter database open;  
  8. Database altered.  
  9.  
  10. SQL> alter database recover managed standby database using current logfile disconnect from session;  
  11. Database altered.  
  12.  
  13. SQL> select db_unique_name,open_mode,database_role,switchover_status from v$database;  
  14.  
  15. DB_UNIQUE_NAME       OPEN_MODE            DATABASE_ROLE    SWITCHOVER_STATUS  
  16. -------------------- -------------------- ---------------- --------------------  
  17. dg1                  READ ONLY            PHYSICAL STANDBY RECOVERY NEEDED  
  18.  
  19. SQL> select db_unique_name,open_mode,database_role,switchover_status from v$database;  
  20.  
  21. DB_UNIQUE_NAME       OPEN_MODE            DATABASE_ROLE    SWITCHOVER_STATUS  
  22. -------------------- -------------------- ---------------- --------------------  
  23. dg1                  READ ONLY WITH APPLY PHYSICAL STANDBY NOT ALLOWED 

二:使用dataguard broker進行切換
1:配置dg1和dg2數據庫服務器的listener.ora和tnsnames.ora文件

[oracle@dg2 ~]$ cat $TNS_ADMIN/listener.ora
SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (GLOBAL_DBNAME = dg2.yang.com)
      (ORACLE_HOME = /u01/app/oracle/product/11.2.0/db1)
      (SID_NAME = dg)
    )
  )

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS_LIST =
        (ADDRESS = (PROTOCOL = TCP)(HOST = dg2.yang.com)(PORT = 1521))
      )
    )
  )

[oracle@dg2 ~]$ cat $TNS_ADMIN/tnsnames.ora
dg1 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.61)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = dg1.yang.com)
    )
  )

dg2 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.62)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = dg2.yang.com)
    )
  )

for_db =
 (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.62)(PORT = 1521))
  )

2:啓動dg1和dg2數據庫的dmon進程

  1. SQL> select db_unique_name,open_mode,database_role from v$database;  
  2.  
  3. DB_UNIQUE_NAME                 OPEN_MODE            DATABASE_ROLE  
  4. ------------------------------ -------------------- ----------------  
  5. dg2                            READ WRITE           PRIMARY  
  6.  
  7. SQL> alter system set dg_broker_start=true;  
  8. System altered.  
  9.  
  10. SQL> !ps -ef |grep dmon  
  11. oracle   14037     1  0 12:30 ?        00:00:00 ora_dmon_dg  
  12. oracle   14103 13958  0 12:35 pts/4    00:00:00 /bin/bash -c ps -ef |grep dmon  
  13. oracle   14105 14103  0 12:35 pts/4    00:00:00 grep dmon  
  14.  
  15. SQL> select db_unique_name,open_mode,database_role from v$database;  
  16.  
  17. DB_UNIQUE_NAME       OPEN_MODE            DATABASE_ROLE  
  18. -------------------- -------------------- ----------------  
  19. dg1                  READ ONLY WITH APPLY PHYSICAL STANDBY  
  20.  
  21. SQL> alter system set dg_broker_start=true;  
  22. System altered. 

3:創建配置文件,可以在dg1和dg2上觀察日誌的輸出

  1. [oracle@dg2 ~]$ dgmgrl sys/123456@dg2  
  2. DGMGRL for Linux: Version 11.2.0.3.0 - 64bit Production  
  3.  
  4. Copyright (c) 2000, 2009, Oracle. All rights reserved.  
  5.  
  6. Welcome to DGMGRL, type "help" for information.  
  7. Connected.  
  8. DGMGRL> show configuration;  
  9. ORA-16541: database is not enabled  
  10.  
  11. Configuration details cannot be determined by DGMGRL  
  12. DGMGRL> create configuration 'DG_BROKER_CONFIG' as primary database is dg2 connect identifier is dg2;  
  13. Configuration "DG_BROKER_CONFIG" created with primary database "dg2"  
  14.  
  15. DGMGRL> add database dg1 as connect identifier is dg1 maintained as physical;  
  16. Database "dg1" added  
  17.  
  18. DGMGRL> enable configuration  
  19. Enabled 

[oracle@dg2 ~]$ tail -f /u01/app/oracle/diag/rdbms/dg2/dg/trace/drcdg.log
04/30/2012 12:27:37
ENABLE CONFIGURATION
04/30/2012 12:27:43
Command ENABLE CONFIGURATION completed
EDIT DATABASE dg1 SET PROPERTY ActualApplyInstance = dg
Apply Instance for database dg1 is dg
Command EDIT DATABASE dg1 SET PROPERTY ActualApplyInstance = dg completed

DGMGRL> show configuration;

Configuration - DG_BROKER_CONFIG

  Protection Mode: MaxAvailability
  Databases:
    dg2 - Primary database
    dg1 - Physical standby database

Fast-Start Failover: DISABLED

Configuration Status:
SUCCESS

DGMGRL> show database dg2;

Database - dg2

  Role:            PRIMARY
  Intended State:  TRANSPORT-ON
  Instance(s):
    dg

Database Status:
SUCCESS

DGMGRL> show database dg1;

Database - dg1

  Role:            PHYSICAL STANDBY
  Intended State:  APPLY-ON
  Transport Lag:   0 seconds
  Apply Lag:       0 seconds
  Real Time Query: ON
  Instance(s):
    dg

Database Status:
SUCCESS

4:開始切換

  1. DGMGRL> switchover to dg1;  
  2. Performing switchover NOW, please wait...  
  3. New primary database "dg1" is opening...  
  4. Operation requires shutdown of instance "dg" on database "dg2"  
  5. Shutting down instance "dg"...  
  6. ORACLE instance shut down.  
  7. Operation requires startup of instance "dg" on database "dg2"  
  8. Starting instance "dg"...  
  9. Unable to connect to database  
  10. ORA-12514: TNS:listener does not currently know of service requested in connect descriptor  
  11.  
  12. Failed.  
  13. Warning: You are no longer connected to ORACLE.  
  14.  
  15. Please complete the following steps to finish switchover:  
  16.         start up instance "dg" of database "dg2" 

5:驗證

  1. [oracle@dg2 ~]$ sqlplus /nolog  
  2. SQL*Plus: Release 11.2.0.3.0 Production on Mon Apr 30 12:30:25 2012  
  3. Copyright (c) 1982, 2011, Oracle.  All rights reserved.  
  4.  
  5. SQL> conn /as sysdba  
  6. Connected to an idle instance.  
  7. SQL> startup   
  8. ORACLE instance started.  
  9.  
  10. Total System Global Area  417546240 bytes  
  11. Fixed Size                  2228944 bytes  
  12. Variable Size             339742000 bytes  
  13. Database Buffers           67108864 bytes  
  14. Redo Buffers                8466432 bytes  
  15. Database mounted.  
  16. Database opened.  
  17. SQL> alter database recover managed standby database using current logfile disconnect from session;  
  18.  
  19. Database altered.  
  20.  
  21. SQL> select db_unique_name,database_role,open_mode from v$database;  
  22.  
  23. DB_UNIQUE_NAME                 DATABASE_ROLE    OPEN_MODE  
  24. ------------------------------ ---------------- --------------------  
  25. dg2                            PHYSICAL STANDBY READ ONLY WITH APPLY  
  26.  
  27. SQL> select protection_mode,protection_level,switchover_status from v$database;  
  28.  
  29. PROTECTION_MODE      PROTECTION_LEVEL     SWITCHOVER_STATUS  
  30. -------------------- -------------------- --------------------  
  31. MAXIMUM AVAILABILITY MAXIMUM AVAILABILITY NOT ALLOWED  
  32.  
  33.  
  34. [oracle@dg1 ~]$ sqlplus /nolog  
  35. SQL*Plus: Release 11.2.0.3.0 Production on Mon Apr 30 12:32:58 2012  
  36. Copyright (c) 1982, 2011, Oracle.  All rights reserved.  
  37.  
  38. SQL> conn /as sysdba  
  39. Connected.  
  40. SQL> select db_unique_name,database_role,open_mode from v$database;  
  41.  
  42. DB_UNIQUE_NAME                 DATABASE_ROLE    OPEN_MODE  
  43. ------------------------------ ---------------- --------------------  
  44. dg1                            PRIMARY          READ WRITE  
  45.  
  46. SQL> select protection_mode,protection_level,switchover_status from v$database;  
  47.  
  48. PROTECTION_MODE      PROTECTION_LEVEL     SWITCHOVER_STATUS  
  49. -------------------- -------------------- --------------------  
  50. MAXIMUM AVAILABILITY MAXIMUM AVAILABILITY TO STANDBY  
  51.  
  52.  
  53. [oracle@dg1 ~]$ dgmgrl sys/123456@dg1  
  54. DGMGRL for Linux: Version 11.2.0.3.0 - 64bit Production  
  55.  
  56. Copyright (c) 2000, 2009, Oracle. All rights reserved.  
  57.  
  58. Welcome to DGMGRL, type "help" for information.  
  59. Connected.  
  60. DGMGRL> show configuration;  
  61.  
  62. Configuration - DG_BROKER_CONFIG  
  63.  
  64.   Protection Mode: MaxAvailability  
  65.   Databases:  
  66.     dg1 - Primary database  
  67.     dg2 - Physical standby database  
  68.  
  69. Fast-Start Failover: DISABLED  
  70.  
  71. Configuration Status:  
  72. SUCCESS  
  73.  
  74. DGMGRL> show database verbose dg1;  
  75.  
  76. Database - dg1  
  77.  
  78.   Role:            PRIMARY  
  79.   Intended State:  TRANSPORT-ON  
  80.   Instance(s):  
  81.     dg  
  82.  
  83.   Properties:  
  84.     DGConnectIdentifier             = 'dg1' 
  85.     ObserverConnectIdentifier       = '' 
  86.     LogXptMode                      = 'SYNC' 
  87.     DelayMins                       = '0' 
  88.     Binding                         = 'OPTIONAL' 
  89.     MaxFailure                      = '0' 
  90.     MaxConnections                  = '1' 
  91.     ReopenSecs                      = '300' 
  92.     NetTimeout                      = '30' 
  93.     RedoCompression                 = 'DISABLE' 
  94.     LogShipping                     = 'ON' 
  95.     PreferredApplyInstance          = '' 
  96.     ApplyInstanceTimeout            = '0' 
  97.     ApplyParallel                   = 'AUTO' 
  98.     StandbyFileManagement           = 'AUTO' 
  99.     ArchiveLagTarget                = '0' 
  100.     LogArchiveMaxProcesses          = '4' 
  101.     LogArchiveMinSucceedDest        = '1' 
  102.     DbFileNameConvert               = '' 
  103.     LogFileNameConvert              = '' 
  104.     FastStartFailoverTarget         = '' 
  105.     InconsistentProperties          = '(monitor)' 
  106.     InconsistentLogXptProps         = '(monitor)' 
  107.     SendQEntries                    = '(monitor)' 
  108.     LogXptStatus                    = '(monitor)' 
  109.     RecvQEntries                    = '(monitor)' 
  110.     SidName                         = 'dg' 
  111.     StaticConnectIdentifier         = '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.61)(PORT=1521))  
  112.  
  113. (CONNECT_DATA=(SERVICE_NAME=dg1_DGMGRL.yang.com)(INSTANCE_NAME=dg)(SERVER=DEDICATED)))'  
  114.     StandbyArchiveLocation          = '/u01/app/oracle/standbylog' 
  115.     AlternateLocation               = '' 
  116.     LogArchiveTrace                 = '0' 
  117.     LogArchiveFormat                = '%t_%s_%r.dbf' 
  118.     TopWaitEvents                   = '(monitor)' 
  119.  
  120. Database Status:  
  121. SUCCESS 

總結:個人推薦在生產環境中使用手工切換的方式,dataguard broker是比較方便,除了前期的配置比較麻煩外,在本次的測試中出現了ORA-12514錯誤,oracle推薦listener.ora文件中將GLOBAL_DBNAME設置成db_unique_name_DGMGRL.db_domain格式,在10g平臺下broker測試沒有問題,詳情可參考:http://ylw6006.blog.51cto.com/470441/686900,另外在平常的使用broker的測試中,曾出現過ORA-16014錯誤,詳情可參考:http://www.itpub.net/thread-1604189-1-1.html

擴展閱讀:
http://ylw6006.blog.51cto.com/470441/841815
http://ylw6006.blog.51cto.com/470441/661228
http://ylw6006.blog.51cto.com/470441/678157

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章