溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

挽救DG中主庫(kù)的nologging操作的塊

發(fā)布時(shí)間:2020-08-05 00:37:32 來(lái)源:ITPUB博客 閱讀:158 作者:沃趣科技 欄目:關(guān)系型數(shù)據(jù)庫(kù)


眾所周知我們的Data Guard數(shù)據(jù)同步是基于日志流的。所以在主庫(kù)執(zhí)行nologging操作是不被允許的。這也就是為什么我們需要在配置Data Guard階段需要使用Force Logging。但是這也會(huì)帶來(lái)很多問(wèn)題(SQL執(zhí)行效率),例如:當(dāng)我們使用數(shù)據(jù)泵進(jìn)行遷移時(shí)我們希望最少停機(jī)時(shí)間完成,這時(shí)候我們就可能會(huì)考慮到以最小日志導(dǎo)入的方式以加快導(dǎo)入速度,然后重新同步備庫(kù)。

在一些場(chǎng)景中,我們會(huì)去使用nologging操作去節(jié)省大量數(shù)據(jù)插入的時(shí)間,而這種操作所帶來(lái)的問(wèn)題就是,如果該庫(kù)在有備庫(kù)的情況下,因?yàn)橹鲙?kù)的nologging插入操作不會(huì)生成redo,所以不會(huì)在備庫(kù)上傳輸和應(yīng)用,這會(huì)導(dǎo)致備庫(kù)的數(shù)據(jù)出現(xiàn)問(wèn)題。

在Oracle 11g,如果遇到這樣的問(wèn)題,可以通過(guò)在備庫(kù)恢復(fù)有問(wèn)題的數(shù)據(jù)文件來(lái)解決問(wèn)題,示例如下:

在一個(gè)具有主備關(guān)系的主庫(kù)上將force_logging設(shè)置為nologging模式,隨后創(chuàng)建一張表,設(shè)置為nologging模式

點(diǎn)擊(此處)折疊或打開(kāi)

  1. SQL> alter database no force logging;
  2. SQL> create table DEMO tablespace users pctfree 99 as select rownum n from xmltable('1 to 1000');
  3. SQL> alter table DEMO nologging;

之后使用/* +append*/插入數(shù)據(jù)并提交

點(diǎn)擊(此處)折疊或打開(kāi)

  1. SQL> insert /*+ append */ into DEMO select rownum n from xmltable('1 to 100000');
  2. SQL> commit

這時(shí)候在備庫(kù)對(duì)該表進(jìn)行查詢會(huì)看到如下報(bào)錯(cuò)信息

點(diǎn)擊(此處)折疊或打開(kāi)

  1. SQL>select count(1) from demo;
  2. select count(1) from demo
  3.                  *
  4. ERROR at line 1:
  5. ORA-01578: ORACLE data block corrupted (file # 4, block # 819)
  6. ORA-01110: data file 4: '/data/data1/ORCL2/datafile/o1_mf_users_3ft1e9qb_.dbf'
  7. ORA-26040: Data block was loaded using the NOLOGGING option


而要修復(fù)這個(gè)問(wèn)題,需要將包含缺少的數(shù)據(jù)的數(shù)據(jù)文件從主庫(kù)復(fù)制到物理備庫(kù)。

步驟一

1、查詢主庫(kù)

點(diǎn)擊(此處)折疊或打開(kāi)

  1. SQL> SELECT NAME, UNRECOVERABLE_CHANGE# FROM V$DATAFILE;
  2. NAME UNRECOVERABLE_CHANGE#
  3. --------------------------------------------------------------------------- ---------------------
  4. +DATADG/orcl/datafile/system.270.972381717 0
  5. +DATADG/orcl/datafile/sysaux.265.972381717 0
  6. +DATADG/orcl/datafile/undotbs1.261.972381717 0
  7. +DATADG/orcl/datafile/users.259.972381717 6252054
  8. +DATADG/orcl/datafile/example.264.972381807 0
  9. +DATADG/orcl/datafile/undotbs2.258.972381927 0
  10. +DATADG/orcl/datafile/example.266.972400297 0
  11. +DATADG/orcl/datafile/ax.268.973612569 0


2、查詢備庫(kù)

點(diǎn)擊(此處)折疊或打開(kāi)

  1. sys@ORCL>SELECT NAME, UNRECOVERABLE_CHANGE# FROM V$DATAFILE;
  2. NAME UNRECOVERABLE_CHANGE#
  3. --------------------------------------------------------------------------- ---------------------
  4. /data/data1/ORCL2/datafile/o1_mf_system_3dt1e9op_.dbf 0
  5. /data/data1/ORCL2/datafile/o1_mf_sysaux_3ct1e9nb_.dbf 0
  6. /data/data1/ORCL2/datafile/o1_mf_undotbs1_3gt1e9qq_.dbf 0
  7. /data/data1/ORCL2/datafile/o1_mf_users_3ft1e9qb_.dbf 5383754
  8. /data/data1/ORCL2/datafile/o1_mf_example_3et1e9ps_.dbf 0
  9. /data/data1/ORCL2/datafile/o1_mf_undotbs2_3ht1e9r1_.dbf 0
  10. /data/data1/ORCL2/datafile/o1_mf_example_3at1e9nb_.dbf 0
  11. /data/data1/ORCL2/datafile/o1_mf_ax_3bt1e9nb_.dbf 0


3、比較主數(shù)據(jù)庫(kù)和備用數(shù)據(jù)庫(kù)的查詢結(jié)果

在兩個(gè)查詢結(jié)果中比較UNRECOVERABLE_CHANGE#列的值。如果主庫(kù)中UNRECOVERABLE_CHANGE#列的值大于備庫(kù)中的同一列,則需要將這些數(shù)據(jù)文件在備庫(kù)恢復(fù)。

步驟二 

將主庫(kù)對(duì)應(yīng)的數(shù)據(jù)文件拷貝至備庫(kù)


點(diǎn)擊(此處)折疊或打開(kāi)

  1. SQL> alter tablespace users begin backup
  2. SQL> exit
  3. ASMCMD>cp +DATADG/orcl/datafile/users.259.972381717 /tmp
  4. $ scp /tmp/users.259.972381717 10.10.60.123:/data/data1/ORCL2/datafile/
  5. SQL> alter tablespace users end backup


步驟三 

備庫(kù)將舊的數(shù)據(jù)文件rename至新的數(shù)據(jù)文件

點(diǎn)擊(此處)折疊或打開(kāi)

  1. SQL> alter database recover managed standby database cancel;
  2. SQL> alter system set standby_file_management=manual; #在備庫(kù)執(zhí)行rename操作時(shí),需要此參數(shù)為manual
  3. SQL> alter database rename file '/data/data1/ORCL2/datafile/o1_mf_users_3ft1e9qb_.dbf' to '/data/data1/ORCL2/datafile/users.259.972381717';
  4. SQL> alter system set standby_file_management=auto;
  5. SQL> alter database recover managed standby database using current logfile disconnect from session;


之后就可以在備庫(kù)查詢到實(shí)例表DEMO

點(diǎn)擊(此處)折疊或打開(kāi)

  1. SQL> select count(1) from demo;
  2.   COUNT(1)
  3. ----------
  4.     101000


對(duì)于這種情況,在12.1版本中,RMAN提供了一種便捷的方式讓我們不需要在主庫(kù)上進(jìn)行數(shù)據(jù)文件的備份傳輸而可以在備庫(kù)使用 restore database (or datafile ) from service去從主庫(kù)進(jìn)行恢復(fù)。

當(dāng)然,Oracle的RMAN是足夠聰明的:如果數(shù)據(jù)文件是正常的狀態(tài),RMAN可以根據(jù)它們的數(shù)據(jù)文件頭進(jìn)行跳躍恢復(fù)。如果,由于nologging操作導(dǎo)致某些塊被標(biāo)記為損壞的,那么這部分?jǐn)?shù)據(jù)文件就是需要恢復(fù)的,然后怎么辦?在恢復(fù)命令中有FORCE選項(xiàng)。但我們可能并不需要它。因?yàn)橛行r(shí)候數(shù)據(jù)文件是同步的,實(shí)時(shí)日志應(yīng)用進(jìn)程還是在運(yùn)行的。這個(gè)時(shí)候,為了恢復(fù),我們需要停止應(yīng)用。 

一旦我們停止了應(yīng)用,那么我們就不需要執(zhí)行RESOTORE DATABASE FORCE操作,因?yàn)楝F(xiàn)在數(shù)據(jù)文件的狀態(tài)是過(guò)舊的,就算你不加FORCE選項(xiàng)RMAN也是不會(huì)跳過(guò)這些數(shù)據(jù)文件的。

步驟一 

備庫(kù)關(guān)掉實(shí)時(shí)日志應(yīng)用,并開(kāi)啟至mount狀態(tài)。

點(diǎn)擊(此處)折疊或打開(kāi)

  1. SQL> alter database recover managed standby database cancel;
  2. SQL> shutdown immediate
  3. Database closed.
  4. Database dismounted.
  5. ORACLE instance shut down.
  6. SQL> startup mount
  7. ORACLE instance started


步驟二 

備庫(kù)登陸RMAN,使用restore database (or datafile ) from service進(jìn)行恢復(fù)

點(diǎn)擊(此處)折疊或打開(kāi)

  1. RMAN> restore database from service 'primary_db'; #這里的primary_db,為備庫(kù)至主庫(kù)的tns連接串的別名
  2. Starting restore at 2018-05-03 17:00:35
  3. using target database control file instead of recovery catalog
  4. allocated channel: ORA_DISK_1
  5. channel ORA_DISK_1: SID=29 device type=DISK
  6. channel ORA_DISK_1: starting datafile backup set restore
  7. channel ORA_DISK_1: using network backup set from service primary_db
  8. channel ORA_DISK_1: specifying datafile(s) to restore from backup set
  9. channel ORA_DISK_1: restoring datafile 00001 to /data/data1/ORCL2/datafile/o1_mf_system_02t1t9ck_.dbf
  10. channel ORA_DISK_1: restore complete, elapsed time: 00:00:25
  11. channel ORA_DISK_1: starting datafile backup set restore
  12. channel ORA_DISK_1: using network backup set from service primary_db
  13. channel ORA_DISK_1: specifying datafile(s) to restore from backup set
  14. channel ORA_DISK_1: restoring datafile 00003 to /data/data1/ORCL2/datafile/o1_mf_sysaux_03t1t9d3_.dbf
  15. channel ORA_DISK_1: restore complete, elapsed time: 00:00:15
  16. channel ORA_DISK_1: starting datafile backup set restore
  17. channel ORA_DISK_1: using network backup set from service primary_db
  18. channel ORA_DISK_1: specifying datafile(s) to restore from backup set
  19. channel ORA_DISK_1: restoring datafile 00004 to /data/data1/ORCL2/datafile/o1_mf_undotbs1_04t1t9di_.dbf
  20. channel ORA_DISK_1: restore complete, elapsed time: 00:00:02
  21. channel ORA_DISK_1: starting datafile backup set restore
  22. channel ORA_DISK_1: using network backup set from service primary_db
  23. channel ORA_DISK_1: specifying datafile(s) to restore from backup set
  24. channel ORA_DISK_1: restoring datafile 00006 to /data/data1/ORCL2/datafile/o1_mf_users_05t1t9dm_.dbf
  25. channel ORA_DISK_1: restore complete, elapsed time: 00:00:15
  26. Finished restore at 2018-05-03 17:01:34


當(dāng)然要記得去起庫(kù)并開(kāi)啟實(shí)時(shí)日志應(yīng)用進(jìn)程!

而在12.2中,Oracle提供了一種更方便的方式去進(jìn)行恢復(fù)主庫(kù)會(huì)將未記錄的塊的列表發(fā)送至備庫(kù),并記錄在備庫(kù)控制文件中,我們可以從備庫(kù)的v$nonlogged_block這個(gè)視圖查看到相關(guān)信息。不需要發(fā)送主庫(kù)的整個(gè)數(shù)據(jù)文件,而是在RMAN執(zhí)行一個(gè)簡(jiǎn)單的命令來(lái)恢復(fù)它們:

RECOVER DATABASE NONLOGGED BLOCK

步驟一 

停止備庫(kù)實(shí)時(shí)日志應(yīng)用

點(diǎn)擊(此處)折疊或打開(kāi)

  1. SQL> alter database recover managed standby database cancel;


步驟二 

備庫(kù)登陸RMAN執(zhí)行

RECOVER DATABASE NONLOGGED BLOCK 

注意執(zhí)行此步驟前請(qǐng)確認(rèn)主備庫(kù)的log_archive_config參數(shù)已經(jīng)設(shè)置

點(diǎn)擊(此處)折疊或打開(kāi)

  1. RMAN> recover database nonlogged block;
  2. Starting recover at 2018-05-03 14:54:22
  3. using target database control file instead of recovery catalog
  4. allocated channel: ORA_DISK_1
  5. channel ORA_DISK_1: SID=56 device type=DISK
  6. starting recovery of nonlogged blocks
  7. List of Datafiles
  8. =================
  9. File Status Nonlogged Blocks Blocks Examined Blocks Skipped
  10. ---- ------ ---------------- --------------- --------------
  11. 1 OK 0 0 107519
  12. 3 OK 0 0 262399
  13. 4 OK 0 0 149759
  14. 5 OK 0 0 31999
  15. 6 OK 0 0 42239
  16. 7 OK 0 16707 21532
  17. 8 OK 0 0 12799
  18. 9 OK 0 0 76799
  19. 18 OK 0 0 33279
  20. 19 OK 0 0 57599
  21. 20 OK 0 0 24959
  22. 21 OK 0 0 33279
  23. 22 OK 0 0 51199
  24. 23 OK 0 0 12799
  25. 29 OK 0 0 1310719
  26. 30 OK 0 0 12799
  27. 31 OK 0 0 33279
  28. 32 OK 0 0 52479
  29. 33 OK 0 0 923519
  30. 34 OK 0 16822 8777
  31. 35 OK 0 0 12799
  32. 37 OK 0 0 24959
  33. Details of nonlogged blocks can be queried from v$nonlogged_block view
  34. recovery of nonlogged blocks complete, elapsed time: 00:00:08
  35. Finished recover at 2018-05-03 14:54:32

最后別忘了開(kāi)啟實(shí)時(shí)日志應(yīng)用進(jìn)程。

綜上來(lái)看,12.2中這個(gè)特性在數(shù)據(jù)倉(cāng)庫(kù)等一些場(chǎng)景是可以嘗試的。以往我們開(kāi)啟force logging造成大量的redo日志并且影響一部分dml語(yǔ)句的執(zhí)行效率。在12.2我們可以嘗試使用nonlogging操作去節(jié)省大量數(shù)據(jù)插入的時(shí)間,然后在系統(tǒng)空閑時(shí)間進(jìn)行備庫(kù)恢復(fù)操作。但是注意這種操作也存在弊端,這樣你的備庫(kù)的可用性就大大降低了。凡事總有取舍!


|  作者簡(jiǎn)介

陳康,沃趣科技數(shù)據(jù)庫(kù)技術(shù)專(zhuān)家

主要參與公司產(chǎn)品實(shí)施、測(cè)試、維護(hù)以及優(yōu)化。


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI