Oracle Golden Gate 系列十二 -- GG 資料初始化裝載二 基於SCN 的初始化 說明 與 示例

perfychi發表於2014-06-05
From: http://blog.csdn.net/tianlesoftware/article/details/6982908


一.初始化說明

       GG實施過程中,初始化是一個重要的工作,尤其是要初始化的資料較多,並且系統又是7*24的時。

 

       對於靜態初始化,把業務停掉,DB 上的資料就不會有變化,這時候,我們可以用expdp/impdp 或者dblink 等方式把基資料同步過去, 在啟動相關的GG 同步程式就可以了。

      但如果要求零停機,DB事務就會不間斷進行,可以透過如下兩種方法來保證初始化過程中事務的完整性和資料的準確性呢

     1. 利用 Keys + Handlecollisions

     2. 利用 commit SCN/CSN

 

1.1 Keys+Handlecollisions 方法

在我們系列九中的說明和測試使用的就是第一種方法:

Oracle Golden Gate 系列九 --GG 資料初始化裝載 說明 與 示例

http://blog.csdn.net/tianlesoftware/article/details/6976551

 

       Handlecollisions引數依賴於表上的Key(Primarykey/Unique key)來對資料進行重複行和缺失行的處理,常在資料初始化過程中保證資料的一致性。但是這種辦法在實際的工程實施中是有相當大的限制。

        首先,該初始化辦法效能比較糟糕,對於大型資料庫來講,並不合適。更嚴重的是,它有很大的缺陷性。

 

MOS 上有關使用Handlecollisions 方法的一個說明:

1. When there isprimary key update (PKUpdate), the HANDLECOLLISIONS method may lose data. Thesolution in the case of a primary key update is for Extract to fetch whole rowby making a flashback query. However, this is not efficient and may not bepossible in certain environments.

2. When a tabledoes not have any type of key, HANDLECOLLISIONS may introduce duplicate rows inthe table. One workaround is to add a primary or unique key to the targettable.

3. The exacttimestamp when the initial load was started may not be known (and differenttables may have different start times), so it is almost inevitable thatHANDLECOLLISIONS will be used for certain records un-necessarily. This couldmask a real data integrity problem.

 

1.2 Commit+SCN/CSN 方法

可以使用一下幾種方法來實現:

(1)一致性的exp和imp

(2)一致性的expdp和impdp

(3) 基於備份的表空間搬移

(4) Dataguard

 

1.2.1 Transportable Tablespace

       TransporttableTablespace 可以使用Expdp/impdp實現,也可以使用RMAN 來實現,這裡要注意的就是傳輸表空間需要將表空間設定為read only,但一般生產庫不允許,所以這裡可以使用基於備份的RMAN 傳輸表空間。

  基於備份的表空間搬移的一個最大的優勢就是零停機,而且支援異構平臺和跨版本(對於不同位元組順序的source-target平臺初始化,需要進行convert),但是需要10g以上版本才支援,同樣受到表空間搬移的那些限制條件。

 

1.2.2 Data Guard

        Dataguard適合同平臺同版本的系統環境初始化。

      

 有關Oracle DG 系列的文章,參考我的Blog:

http://blog.csdn.net/tianlesoftware/article/category/700326

 

1.2.3 exp/expdp

        透過exp和expdp的一致性引數(flashback_scn),匯出特定SCN點上的一致性版本. FLASHBACK_SCN 引數用於指定匯出特定SCN時刻的表資料,如:

FLASHBACK_SCN=scn_value。

       Scn_value用於標識SCN值.FLASHBACK_SCN和FLASHBACK_TIME不能同時使用。

 

使用這種方法初始化存在一個問題,就是構造一致性資料過程中會對undo造成比較大的壓力,尤其對大型資料庫來講,可以透過分割的datapump來實現資料的分組同步,分散undo的壓力,然後合適時間將分組的datapump合併即可。

       該方法的優點就是可以跨平臺和跨版本初始化。

 

Oracle 10g Data Pump Expdp/Impdp 詳解

http://blog.csdn.net/tianlesoftware/article/details/4674224

 

Oracle expdp/impdp 使用示例

http://blog.csdn.net/tianlesoftware/article/details/6260138

 

ORACLE EXP/IMP 說明

http://blog.csdn.net/tianlesoftware/article/details/4718366

 

在基於SCN 的資料初始化完成之後,我們在Replicat時,從指定SCN 開始就ok了。如:

start rep1,aftercsn 12345678

 

二.示例

這裡只演示基於Expdp/Impdp, 這種方法最靈活。

 

我們使用GG的架構是: Extract+Data Pump + Replicat.

 

2.1 清除之前的GG環境

--Target System

SQL> conn dave/dave;

Connected.

SQL> drop table pdba;

Table dropped.

 

GGSCI (gg2) 46> stop rep1

Sending STOP request to REPLICAT REP1 ...

Request processed.

 

GGSCI (gg2) 47> info all

Program    Status      Group       Lag           Time Since Chkpt

MANAGER    RUNNING                                          

REPLICAT   STOPPED     REP1        00:00:00      00:00:04   

 

GGSCI (gg2) 48> delete rep1

ERROR: Could not delete DB checkpoint forREPLICAT REP1 (Database login required to delete database checkpoint).

 

GGSCI (gg2) 49> dblogin useridggate,password ggate

Successfully logged into database.

 

GGSCI (gg2) 51> delete rep1

Deleted REPLICAT REP1.

 

GGSCI (gg2) 52> info all

Program    Status      Group       Lag      Time Since Chkpt

MANAGER    RUNNING                                          

 

--Source System

GGSCI (gg1) 55> info all

Program    Status      Group      Lag           Time Since Chkpt

 

MANAGER    RUNNING                                          

EXTRACT    RUNNING     DPUMP       00:00:00      00:00:09   

EXTRACT    RUNNING     EXT1        00:00:00      00:00:09   

 

GGSCI (gg1) 56> dblogin userid ggate,password ggate

Successfully logged into database.

 

GGSCI (gg1) 57> stop ext1

Sending STOP request to EXTRACT EXT1 ...

Request processed.

 

GGSCI (gg1) 58> stop dpump

Sending STOP request to EXTRACT DPUMP ...

Request processed.

 

GGSCI (gg1) 59> delete ext1

2011-11-17 16:51:48  INFO   OGG-01750  Successfullyunregistered EXTRACT EXT1 from database.

Deleted EXTRACT EXT1.

 

GGSCI (gg1) 60> delete dpump

Deleted EXTRACT DPUMP.

 

GGSCI (gg1) 61> info all

Program    Status      Group       Lag           Time Since Chkpt

MANAGER    RUNNING                                          

 

 

2.2 重新配置GG 環境

2.2.1 Source System建立Extract和 Data Pump

--建立Extract: ext1

GGSCI (gg1) 62> add extractext1,tranlog, begin now

2011-11-17 16:56:21  INFO   OGG-01749  Successfully registeredEXTRACT EXT1 to start managing log retention at SCN 1374149.

EXTRACT added.

 

GGSCI (gg1) 63> add exttrail/u01/ggate/dirdat/lt, extract ext1

EXTTRAIL added.

 

GGSCI (gg1) 64> view params ext1

 

extract ext1

userid ggate@gg1, password ggate

--rmthost gg2,mgrport 7809

--rmttrail /u01/ggate/dirdat/lt

exttrail /u01/ggate/dirdat/lt

ddl include all objname dave.pdba;

table dave.pdba;

 

--建立DataPump:dpump

GGSCI (gg1) 65> add extractdpump,exttrailsource /u01/ggate/dirdat/lt

EXTRACT added.

 

GGSCI (gg1) 66> add rmttrail/u01/ggate/dirdat/lt, extract dpump

RMTTRAIL added.

 

GGSCI (gg1) 67> view params dpump

extract dpump

userid ggate@gg1, password ggate

rmthost gg2, mgrport 7809

rmttrail /u01/ggate/dirdat/lt

passthru

table dave.pdba;

 

2.2.2 Target System 建立Replicat

 

--建立checkpoint 表

GGSCI (gg2) 57> view param ./GLOBALS

GGSCHEMA ggate

CHECKPOINTTABLE ggate.checkpoint

 

GGSCI (gg2) 58> dblogin userid ggate@gg2,password ggate

Successfully logged into database.

 

GGSCI (gg2) 59> add checkpointtable ggate.checkpoint

Successfully created checkpoint tableGGATE.CHECKPOINT.

 

--建立Replicat: rep1

GGSCI (gg2) 60> add replicatrep1,exttrail /u01/ggate/dirdat/lt, checkpointtable ggate.checkpoint

REPLICAT added.

 

GGSCI (gg2) 61> view param rep1

replicat rep1

ASSUMETARGETDEFS

userid ggate@gg2,password ggate

discardfile/u01/ggate/dirdat/rep1_discard.txt, append, megabytes 10

--HANDLECOLLISIONS

ddl include all

ddlerror default ignore retryop

map dave.pdba, target dave.pdba;

 

這裡注意一點,因為我在刪除程式時,並沒有刪除對應的引數檔案,所以在建立同名的程式之後,就會預設使用之前的引數檔案,在edit 程式時會顯示引數檔案的位置。

 

預設放在$GGATE/dirprm目錄下:

gg2:/u01/ggate/dirprm> ls

mgr.prm rep1.prm  rept2.prm

 

2.3 啟動Extract 和 Data Pump程式:ext1,dpump

       注意這裡不要啟動Replicat 程式,要等到我們用expdp/impdp 完成初始化之後,在用SCN 來啟動replicat 程式。

 

GGSCI (gg1) 70> start ext1

Sending START request to MANAGER ...

EXTRACT EXT1 starting

 

GGSCI (gg1) 71> start dpump

Sending START request to MANAGER ...

EXTRACT DPUMP starting

 

GGSCI (gg1) 73> info all

 

Program    Status      Group       Lag           Time Since Chkpt

MANAGER    RUNNING                                          

EXTRACT    RUNNING     DPUMP       00:00:00      00:18:27   

EXTRACT    RUNNING     EXT1        00:20:10      00:00:02   

 

2.4 用帶FLASHBACK_SCN 的expdp/impdp完成初始化

2.4.1 在SourceDB 查詢當前的SCN

SQL> select current_scn from v$database;

CURRENT_SCN

-----------

   1376141

 

2.4.2 匯出pdba 表的資料

gg1:/u01/backup> expdp  dave/dave directory=backup dumpfile=pdba.dmplogfile=table.log tables=pdba flashback_scn=1376141;

 

Export: Release 11.2.0.3.0 - Production onThu Nov 17 17:24:49 2011

 

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

 

Connected to: Oracle Database 11gEnterprise Edition Release 11.2.0.3.0 - 64bit Production

With the Partitioning, OLAP, Data Miningand Real Application Testing options

Starting"DAVE"."SYS_EXPORT_TABLE_02":  dave/******** directory=backupdumpfile=pdba.dmp logfile=table.log tables=pdba flashback_scn=1376141

Estimate in progress using BLOCKS method...

Processing object typeTABLE_EXPORT/TABLE/TABLE_DATA

Total estimation using BLOCKS method: 60 MB

Processing object typeTABLE_EXPORT/TABLE/TABLE

Processing object typeTABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS

. . exported"DAVE"."PDBA"                               48.51 MB 2678634rows

Master table"DAVE"."SYS_EXPORT_TABLE_02" successfully loaded/unloaded

******************************************************************************

Dump file set for DAVE.SYS_EXPORT_TABLE_02is:

 /u01/backup/pdba.dmp

Job"DAVE"."SYS_EXPORT_TABLE_02" successfully completed at17:26:22

 

2.4.3 刪除pdba 表的部分資料

SQL> delete from pdba whererownum<1000;

999 rows deleted.

 

SQL> commit;

Commit complete.

 

SQL> select current_scn from v$database;

CURRENT_SCN

-----------

   1377291

 

2.4.4 將dumpscp到Target

gg1:/u01/backup> scp pdba.dmp192.168.3.200:/u01/backup

oracle@192.168.3.200's password:

pdba.dmp                                         100%   49MB   6.1MB/s  00:08 

 

2.4.5 impdp dump檔案

gg2:/u01/backup> impdp dave/davedirectory=backup dumpfile=pdba.dmp logfile=table.log tables=pdbatable_exists_action=replace;

 

Import: Release 11.2.0.3.0 - Production onThu Nov 17 17:30:04 2011

 

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

 

Connected to: Oracle Database 11gEnterprise Edition Release 11.2.0.3.0 - 64bit Production

With the Partitioning, OLAP, Data Miningand Real Application Testing options

Master table"DAVE"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded

Starting "DAVE"."SYS_IMPORT_TABLE_01":  dave/******** directory=backupdumpfile=pdba.dmp logfile=table.log tables=pdba table_exists_action=replace

Processing object typeTABLE_EXPORT/TABLE/TABLE

Processing object typeTABLE_EXPORT/TABLE/TABLE_DATA

. . imported "DAVE"."PDBA"                               48.51 MB 2678634rows

Processing object typeTABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS

Job"DAVE"."SYS_IMPORT_TABLE_01" successfully completed at17:31:00

 

2.5 用SCN 啟動Replicat

 

GGSCI (gg2) 63> start rep1, aftercsn 1376141

 

Sending START request to MANAGER ...

REPLICAT REP1 starting

 

 

GGSCI (gg2) 64> info rep1

 

REPLICAT  REP1      Last Started 2011-11-1717:32   Status RUNNING

Checkpoint Lag       00:00:00 (updated 00:00:04 ago)

Log Read Checkpoint  File /u01/ggate/dirdat/lt000000

                    First Record  RBA 978

 

 

2.6 驗證

只要Source 和Target 表上的pdba 記錄數一致,就說明,已經ok了。

--Source DB:

SQL> conn dave/dave;

Connected.

SQL> select count(*) from pdba;

 

 COUNT(*)

----------

  2678634

 

--Target DB:

SQL> conn dave/dave;

Connected.

SQL> select count(*) from pdba;

 

 COUNT(*)

----------

  2678634

 

同步正常。基於SCN 的初始化示例到此結束。

 

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

相關文章