1. 程式人生 > >創建邏輯dg

創建邏輯dg

bype manager release 2 cond 目的 做的 building ani 說明

邏輯備用DG
今天是2014-04-29,近期一直忙的事情,也沒來的急寫點東西。今天繼續整理dg的相關內容,要說的是邏輯dg的創建過程和註意事項。
什麽是邏輯dg呢?物理dg類似於主庫的完整副本。是塊對塊的重做應用。

可是對於邏輯dg。是類似採用日誌挖掘技術將主庫傳來的redo數據,進行構造sql進而在備庫進行應用。

且備庫能夠正常打開,並能維護其它不是主庫傳送的須要維護的對象。


那麽就存在一個問題,假設在備庫採用rowid獲得的數據。有可能和主庫的全然不一致。

那麽當在主庫改動數據的時候,我們建議在所改動的對象上。創建非空索引或是主鍵條件,那麽在傳送到備庫的時候,繼而可以應用這些約束,避免數據更新的錯誤性。但有時候並非非要說一定要在主庫建立對象表的主鍵或是非空索引。假設沒有創建了。那麽將會產生大量的無用redo,這就是在全部字段上都進行了日誌補充導致。

The physical organization in a logical standby database is different from that of the primary database, even though the logical standby database is created from a backup copy of the primary database. Thus, ROWIDs contained in the redo records generated by the primary database cannot be used to identify the corresponding row in the logical standby database.

Oracle uses primary-key or unique-constraint/index supplemental logging to logically identify a modified row in the logical standby database. When database-wide primary-key and unique-constraint/index supplemental logging is enabled, eachUPDATE statement also writes the column values necessary in the redo log to uniquely identify the modified row in the logical standby database.

  • If a table has a primary key defined, then the primary key is logged along with the modified columns as part of theUPDATE statement to identify the modified row.

  • If there is no primary key, then the shortest nonnull unique-constraint/index is logged along with the modified columns as part of theUPDATE statement to identify the modified row.

  • If there is no primary key and no nonnull unique constraint/index, then all columns of bounded size are logged as part of the UPDATE statement to identify the modified row. All columns are logged except the following: LONG, LOB, LONG RAW, object type, and collections.

  • A function-based index, even though it is declared as unique, cannot be used to uniquely identify a modified row. However, logical standby databases support replication of tables that have function-based indexes defined, as long as modified rows can be uniquely identified.

Oracle recommends that you add a primary key or a nonnull unique index to tables in the primary database, whenever possible, to ensure that SQL Apply can efficiently apply redo data updates to the logical standby database.


那麽哪些是sql apply支持的類型和不支持哪些類型呢?查看手冊例如以下:

C.1.1 Supported Datatypes in a Logical Standby Database

Logical standby databases support the following datatypes:

  • BINARY_DOUBLE

  • BINARY_FLOAT

  • BLOB

  • CHAR

  • CLOB and NCLOB

  • DATE

  • INTERVAL YEAR TO MONTH

  • INTERVAL DAY TO SECOND

  • LONG

  • LONG RAW

  • NCHAR

  • NUMBER

  • NVARCHAR2

  • RAW

  • TIMESTAMP

  • TIMESTAMP WITH TIMEZONE

  • TIMESTAMP WITH LOCAL TIMEZONE

  • VARCHAR and VARCHAR2

  • LOBs stored as SecureFiles (requires that the primary database be run at a compatibility of 11.2 or higher.

  • XMLType data for all storage models, assuming the following primary database compatibility requirements:

    • XMLType stored in CLOB format requires primary database to run at a compatibility of 11.1 or higher

    • XMLType stored in object-relational format or as binary XML requires that the primary database be running Oracle Database 11g Release 2 (11.2.0.3) or higher with a redo compatibility setting of 11.2.0.3 or higher

C.1.1.1 Compatibility Requirements

SQL Apply support for the following has compatibility requirements on the primary database:

  • Multibyte CLOB support requires primary database to run at a compatibility of 10.1 or higher.

  • IOT support without LOBs and Overflows requires primary database to run at a compatibility of 10.1 or higher.

  • IOT support with LOB and Overflow requires primary database to run at a compatibility of 10.2 or higher.

  • TDE support requires primary database to run at a compatibility of 11.1 or higher.

  • Segment compression requires primary database to run at a compatibility of 11.1 or higher.

  • Hybrid Columnar Compression support is dependent on the underlying storage system.

通過視圖查看主庫中的哪些表不能被sql apply :

[[email protected] ~]$ sqlplus  / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Tue Apr 29 21:30:01 2014

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, OLAP, Data Mining and Real Application Testing options
SQL> 
SQL> desc dba_logstdby_unsupported
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 OWNER                                              VARCHAR2(30)
 TABLE_NAME                                         VARCHAR2(30)
 COLUMN_NAME                                        VARCHAR2(30)
 ATTRIBUTES                                         VARCHAR2(39)
 DATA_TYPE                                          VARCHAR2(32)

SQL> select owner,table_name,column_name,attributes,data_type from dba_logstdby_unsupported;

no rows selected
查看存在唯一性問題的表。例如以下:
SQL> select * from dba_logstdby_not_unique;

OWNER                          TABLE_NAME                     B
------------------------------ ------------------------------ -
SCOTT                          BONUS                          N
SCOTT                          SALGRADE                       N
STDBYPERF                      STATS$MANAGED_STANDBY          N
STDBYPERF                      STATS$RECOVERY_PROGRESS        N

SQL> desc dba_logstdby_not_unique
 Name                                      Null?

Type ----------------------------------------- -------- ---------------------------- OWNER VARCHAR2(30) TABLE_NAME VARCHAR2(30) BAD_COLUMN VARCHAR2(1)

好了。如今開始創建邏輯dg,由於我已經創建了物理 dg,那麽我們從該基礎上轉換成邏輯dg(註:不能從邏輯dg轉為物理dg)。假設沒有創建物理dg。那麽能夠
直接去創建邏輯dg,可是註意參數的改動。
第一步:停止備庫redo應用:
查看數據庫模式:

SQL> set linesize 200                                                                       
SQL> col dest_name for a50
SQL> select database_mode,recovery_mode,protection_mode,dest_name from v$archive_dest_status where database_mode!=‘UNKNOWN‘;

DATABASE_MODE   RECOVERY_MODE           PROTECTION_MODE      DEST_NAME
--------------- ----------------------- -------------------- --------------------------------------------------
OPEN_READ-ONLY  MANAGED REAL TIME APPLY MAXIMUM PERFORMANCE  LOG_ARCHIVE_DEST_1

SQL> 

停止redo應用:

SQL> alter database recover managed standby database cancel;

Database altered.

SQL> 
 

第二步:在主庫build邏輯dg所需元數據。


SQL> exec dbms_logstdby.build;

PL/SQL procedure successfully completed.

SQL> 

註:這個過程主要是做了什麽操作呢?

A LogMiner dictionary must be built into the redo data so that the LogMiner component of SQL Apply can properly interpret changes it sees in the redo. As part of building the LogMiner dictionary, supplemental logging is automatically set up to log primary key and unique-constraint/index columns. The supplemental logging information ensures each update contains enough information to logically identify each row that is modified by the statement.

To build the LogMiner dictionary, issue the following statement:

SQL> EXECUTE DBMS_LOGSTDBY.BUILD;

The DBMS_LOGSTDBY.BUILD procedure waits for all existing transactions to complete. Long-running transactions executed on the primary database will affect the timeliness of this command.

Note:

In databases created using Oracle Database 11g release 2 (11.2) or later, supplemental logging information is automatically propagated to any existing physical standby databases. However, for databases in earlier releases, or if the database was created using an earlier release and then upgraded to 11.2, you must check whether supplemental logging is enabled at the physical standby(s) if it is also enabled at the primary database. If it is not enabled at the physical standby(s), then before performing a switchover or failover, you must enable supplemental logging on all existing physical standby databases. To do so, issue the following SQL command on each physical standby:
SQL>  ALTER DATABASE ADD SUPPLEMENTAL LOG DATA (PRIMARY KEY, UNIQUE INDEX) COLUMNS;

If you do not do this, then any logical standby that is also in the same Data Guard configuration will be unusable if a switchover or failover is performed to one of the physical standby databases. If a switchover or failover has already occurred and supplemental logging was not enabled, then you must recreate all logical standby databases

第三步:在備庫告訴mrp繼續應用redo。達到字典構建時的scn。

SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.

Total System Global Area  313159680 bytes
Fixed Size                  2252824 bytes
Variable Size             171970536 bytes
Database Buffers          134217728 bytes
Redo Buffers                4718592 bytes
Database mounted.

SQL> alter database recover to logical standby Amy;

Database altered.

這時我們改動幾個地方,首先是db_name;

SQL> alter system set db_name=Amy scope=spfile;

System altered.

SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options


另外還有log_archive_dest_n 參數配置:

SQL> show parameter log_archive_dest_1

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
log_archive_dest_1                   string      location=USE_DB_RECOVERY_FILE_
                                                 DEST valid_for=(all_logfiles,a
                                                 ll_roles)
log_archive_dest_10                  string
log_archive_dest_11                  string
log_archive_dest_12                  string
log_archive_dest_13                  string
log_archive_dest_14                  string
log_archive_dest_15                  string
log_archive_dest_16                  string
log_archive_dest_17                  string

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
log_archive_dest_18                  string
log_archive_dest_19                  string
SQL> show parameter log_archive_dest_2

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
log_archive_dest_2                   string      service=dg1 lgwr sync valid_fo
                                                 r=(online_logfiles,primary_rol
                                                 e) db_unique_name=dg1
log_archive_dest_20                  string


這裏須要說明第一個log_archive_dest_1 為什麽是all_logfiles呢?由於我們是創建的邏輯備庫,邏輯備庫除了維護主庫的對象外。還能夠自己進行其它對象的改動操作那麽這些對象的redo,是須要歸檔的。邏輯備庫是一個真實數據庫啊,另外假設不指定高速閃回區,那麽應該在加一個log_archive_dest_3,將日誌分開。另外假設是高速閃回區。能夠省略valid_for,由於默認就是all_logfiles,all_roles。


另外要註意:假設邏輯備庫和主庫在一個同樣的數據存儲管理上。那麽須要使用dbms_logstdby.skip去跳過alter tablespace dml的操作,不然可能產生誤改動主庫的表空間問題。
第四步:完畢邏輯dg的創建:

SQL> shutdown immediat;
SP2-0717: illegal SHUTDOWN option
SQL> shutdown immediate;
ORA-01507: database not mounted


ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.

Total System Global Area  313159680 bytes
Fixed Size                  2252824 bytes
Variable Size             171970536 bytes
Database Buffers          134217728 bytes
Redo Buffers                4718592 bytes
Database mounted.
SQL> alter database open resetlogs;

Database altered.

SQL> alter database start logical standby apply immediate;

Database altered.

SQL> 
SQL> set linesize 200

SQL> select * from v$log

    GROUP#    THREAD#  SEQUENCE#      BYTES  BLOCKSIZE    MEMBERS ARC STATUS           FIRST_CHANGE# FIRST_TIM NEXT_CHANGE# NEXT_TIME
---------- ---------- ---------- ---------- ---------- ---------- --- ---------------- ------------- --------- ------------ ---------
         1          1          1   52428800        512          1 NO  CURRENT                1335271 29-APR-14   2.8147E+14
         2          1          0   52428800        512          1 YES UNUSED                       0                      0
         3          1          0   52428800        512          1 YES UNUSED                       0                      0

註:resetlogs做什麽的?
resetlogs打開講會對沒有歸檔的redo日誌文件裏的內容進行覆蓋,sequence又一次從1開始計算。相當於數據庫進入了一個新的化身或是新的生命周期。進入incarnation的目的就是為了不在去應用resetlog之後的日誌的lcr。

常常有朋友問我resetlog打開數據庫了是否就是一個不全然恢復呢?這是一個很錯誤的觀點,全然恢復和不全然恢復不是根據resetlog定的。而是我們在前滾的時候是否所有應用了日誌。假設所有應用了,那麽就是一次全然恢復過程。
我們能夠查看數據庫的原型列表例如以下。

[[email protected] dbs]$ rman target /

Recovery Manager: Release 11.2.0.4.0 - Production on Tue Apr 29 22:20:46 2014

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

connected to target database: AMY (DBID=1205246033)

RMAN> list incarnation
2> ;

using target database control file instead of recovery catalog

List of Database Incarnations
DB Key  Inc Key DB Name  DB ID            STATUS  Reset SCN  Reset Time
------- ------- -------- ---------------- --- ---------- ----------
1       1       AMY      1205246033       PARENT  1335270    29-APR-14
2       2       AMY      1205246033       CURRENT 1335271    29-APR-14

第五步:驗證邏輯dg:

SQL> select * from v$logstdby_progress;

APPLIED_SCN APPLIED_T RESTART_SCN RESTART_T LATEST_SCN LATEST_TI MINING_SCN MINING_TI RESETLOGS_ID
----------- --------- ----------- --------- ---------- --------- ---------- --------- ------------
    1335268               1334503              1334503    
    
SQL>  select process,status,sequence# from v$managed_standby;

PROCESS   STATUS        SEQUENCE#
--------- ------------ ----------
ARCH      CONNECTED             0
ARCH      CONNECTED             0
ARCH      CONNECTED             0
ARCH      CONNECTED             0
ARCH      CONNECTED             0

SQL> 


咦?發現問題了,查看主數據庫日誌:

Error 1017 received logging on to the standby
------------------------------------------------------------
Check that the primary and standby are using a password file
and remote_login_passwordfile is set to SHARED or EXCLUSIVE, 
and that the SYS password is same in the password files.
      returning error ORA-16191
------------------------------------------------------------
PING[ARC1]: Heartbeat failed to connect to standby ‘dg2‘. Error is 16191.

password文件的問題咯
重建password文件?那應該是在10G可是到11g不能重建。我們要做的是將主庫的password 文件copy到備庫,然後mv。


之後查看主庫日誌 狀態例如以下(能夠發現主庫每隔1分鐘去主動ping一下備庫):

------------------------------------------------------------
PING[ARC1]: Heartbeat failed to connect to standby ‘dg2‘. Error is 16191.
Tue Apr 29 22:39:42 2014
Error 1017 received logging on to the standby
------------------------------------------------------------
Check that the primary and standby are using a password file
and remote_login_passwordfile is set to SHARED or EXCLUSIVE, 
and that the SYS password is same in the password files.
      returning error ORA-16191
------------------------------------------------------------
PING[ARC1]: Heartbeat failed to connect to standby ‘dg2‘. Error is 16191.
Tue Apr 29 22:40:46 2014
******************************************************************
LGWR: Setting ‘active‘ archival for destination LOG_ARCHIVE_DEST_2
******************************************************************
LGWR: Standby redo logfile selected for thread 1 sequence 84 for destination LOG_ARCHIVE_DEST_2
Thread 1 advanced to log sequence 84 (LGWR switch)
  Current log# 3 seq# 84 mem# 0: /u01/app/oracle/oradata/dg/redo03.log
Tue Apr 29 22:40:49 2014
Archived Log entry 138 added for thread 1 sequence 83 ID 0x68b85302 dest 1:
Tue Apr 29 22:40:49 2014
ARC3: Standby redo logfile selected for thread 1 sequence 83 for destination LOG_ARCHIVE_DEST_2

查看備庫日誌:

LOGMINER: Read buffers: 16
LOGMINER: Memory LWM: limit 10M, LWM 24M, 80%
LOGMINER: Memory Release Limit: 1M
RFS LogMiner: Registered logfile [/u01/app/oracle/fast_recovery_area/DG2/foreign_archivelog/DG1/2014_04_29/o1_mf_1_82_9ozghfdo_.arc] to LogMiner session id [1]
RFS LogMiner: Registered logfile [/u01/app/oracle/fast_recovery_area/DG2/foreign_archivelog/DG1/2014_04_29/o1_mf_1_81_9ozghffw_.arc] to LogMiner session id [1]
Tue Apr 29 22:40:47 2014
Primary database is in MAXIMUM PERFORMANCE mode
RFS[4]: Assigned to RFS process 3779
RFS[4]: Selected log 4 for thread 1 sequence 84 dbid 1756848898 branch 843517891
Tue Apr 29 22:40:48 2014
RFS[5]: Assigned to RFS process 3777
RFS[5]: Opened log for thread 1 sequence 79 dbid 1756848898 branch 843517891
RFS LogMiner: Registered logfile [/u01/app/oracle/fast_recovery_area/DG2/foreign_archivelog/DG1/2014_04_29/o1_mf_1_79_9ozghhy9_.arc] to LogMiner session id [1]
Tue Apr 29 22:40:50 2014
LOGMINER: session#=1 (Logical_Standby$), reader MS00 pid=38 OS id=3781 sid=44 started
Tue Apr 29 22:40:50 2014
LOGMINER: session#=1 (Logical_Standby$), builder MS01 pid=39 OS id=3783 sid=45 started
Tue Apr 29 22:40:50 2014
LOGMINER: session#=1 (Logical_Standby$), preparer MS02 pid=40 OS id=3785 sid=48 started
Tue Apr 29 22:40:50 2014
RFS LogMiner: RFS id [3787] assigned as thread [1] PING handler
Tue Apr 29 22:40:51 2014
RFS[6]: Assigned to RFS process 3789
RFS[6]: Selected log 5 for thread 1 sequence 83 dbid 1756848898 branch 843517891
Tue Apr 29 22:40:55 2014
RFS LogMiner: Registered logfile [/u01/app/oracle/fast_recovery_area/DG2/foreign_archivelog/DG1/2014_04_29/o1_mf_1_83_9ozghp29_.arc] to LogMiner session id [1]
LOGMINER: Begin mining logfile during dictionary load for session 1 thread 1 sequence 79, /u01/app/oracle/fast_recovery_area/DG2/foreign_archivelog/DG1/2014_04_29/o1_mf_1_79_9ozghhy9_.arc
Tue Apr 29 22:41:09 2014
Thread 1 advanced to log sequence 2 (LGWR switch)
  Current log# 2 seq# 2 mem# 0: /u01/app/oracle/oradata/dg/DG2/onlinelog/o1_mf_2_9ozd8zc1_.log
Tue Apr 29 22:41:11 2014
Archived Log entry 1 added for thread 1 sequence 1 ID 0x47d6f0d0 dest 1:
Tue Apr 29 22:41:20 2014
Thread 1 advanced to log sequence 3 (LGWR switch)
  Current log# 3 seq# 3 mem# 0: /u01/app/oracle/oradata/dg/DG2/onlinelog/o1_mf_3_9ozd904o_.log
Tue Apr 29 22:41:22 2014
Archived Log entry 2 added for thread 1 sequence 2 ID 0x47d6f0d0 dest 1:
Thread 1 cannot allocate new log, sequence 4
Checkpoint not complete
  Current log# 3 seq# 3 mem# 0: /u01/app/oracle/oradata/dg/DG2/onlinelog/o1_mf_3_9ozd904o_.log
Thread 1 advanced to log sequence 4 (LGWR switch)
  Current log# 1 seq# 4 mem# 0: /u01/app/oracle/oradata/dg/DG2/onlinelog/o1_mf_1_9ozd8yhq_.log
Tue Apr 29 22:41:31 2014
Archived Log entry 3 added for thread 1 sequence 3 ID 0x47d6f0d0 dest 1:
Tue Apr 29 22:41:47 2014
RFS LogMiner: RFS id [3787] assigned as thread [1] PING handler

確認進程:

SQL> select process,status,sequence# from v$managed_standby;

PROCESS   STATUS        SEQUENCE#
--------- ------------ ----------
ARCH      CLOSING               2
ARCH      CLOSING               3
ARCH      CONNECTED             0
ARCH      CLOSING              83
ARCH      CLOSING               1
RFS       IDLE                  0
RFS       IDLE                  0
RFS       IDLE                  0
RFS       IDLE                  0
RFS       IDLE                 84

10 rows selected.

SQL> 

至此完畢邏輯dg的搭建過程:

創建邏輯dg