小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Oracle使用expdb、impdb解決導(dǎo)出、導(dǎo)入時(shí)的用戶修改、表空間修改變更問(wèn)題

 愛(ài)吃魚(yú)的俊懶貓 2019-09-27

        對(duì)于Oracle的數(shù)據(jù)導(dǎo)出和導(dǎo)入,我們之前一直使用exp、imp來(lái)處理,  但用imp在導(dǎo)入時(shí)經(jīng)常會(huì)要求表空間一致,而博主今天恰恰就遇到了需要變更表空間和用戶的情況,這在我們平常的業(yè)務(wù)場(chǎng)景中也會(huì)遇到,那么該如何處理呢?

        我們采用Oralce的 “數(shù)據(jù)泵” Data Dump來(lái)處理。

        測(cè)試環(huán)境:Windows Server 2008 R2、Oracle11g

      目錄

      1、數(shù)據(jù)泵(Data Dump)主要解決問(wèn)題

      2、數(shù)據(jù)泵使用限制

      3、創(chuàng)建測(cè)試數(shù)據(jù)庫(kù)

      4、導(dǎo)出源數(shù)據(jù)庫(kù)

      5、導(dǎo)入目標(biāo)數(shù)據(jù)庫(kù)

      6、驗(yàn)證導(dǎo)入后表空間


      1、數(shù)據(jù)泵(Data Dump)主要解決問(wèn)題

        ①. 比imp/exp更加靈活,支持多種元數(shù)據(jù)過(guò)濾策略,多種導(dǎo)入、導(dǎo)出模式,如將A表空間B用戶數(shù)據(jù)導(dǎo)入C表空間D用戶下,則只需REMAP_SCHEMA即可

        ②. 表空間占用和表空間變更問(wèn)題,可使用REMAP_TABLESPACE來(lái)解決

        ③. 空表問(wèn)題,Oracle11g新的表并且表中無(wú)數(shù)據(jù)也為使用過(guò)則表的segment空間是不會(huì)分配的,這樣在使用exp導(dǎo)出時(shí)空表便不會(huì)被導(dǎo)出,解決這個(gè)問(wèn)題要么給空分配segment或直接使用expdp。

        ④. 效率問(wèn)題。普通情況下expdp/impdp比exp/imp效率要高,高多少博主沒(méi)有測(cè)試,有興趣的不妨驗(yàn)證下,@博主。

 

      2、數(shù)據(jù)泵使用限制

        使用expdp、impdp有條件限制

        一是遠(yuǎn)程使用時(shí)必須配置好Database Link,因?yàn)閑xpdp、impdp只能在服務(wù)端使用,不能再客戶端使用;

        二是試用前要解決路徑配置問(wèn)題。

 

      3、創(chuàng)建測(cè)試數(shù)據(jù)庫(kù)

        創(chuàng)建源用戶和表空間,為了便于操作我們給新創(chuàng)建用戶授權(quán)的了dba權(quán)限,大家在本地使用時(shí)注意

  1. /*第1步:創(chuàng)建臨時(shí)表空間 */
  2. create temporary tablespace test_source_temp
  3. tempfile 'D:\Oracle11g\oradata\test_source_temp.dbf'
  4. size 10m
  5. autoextend on
  6. next 1m maxsize unlimited
  7. extent management local;
  8. /*第2步:創(chuàng)建數(shù)據(jù)表空間 */
  9. create tablespace test_source
  10. logging
  11. datafile 'D:\Oracle11g\oradata\test_source.dbf'
  12. size 10m
  13. autoextend on
  14. next 1m maxsize unlimited
  15. extent management local;
  16. /*第3步:創(chuàng)建用戶并指定表空間 */
  17. create user test_source identified by a123456
  18. default tablespace test_source
  19. temporary tablespace test_source_temp;
  20. /*第4步:給用戶授予權(quán)限 */
  21. grant connect,resource,dba to test_source;

        創(chuàng)建兩張測(cè)試表,一張基礎(chǔ)數(shù)據(jù)類(lèi)型,一張含clob、blob、date等復(fù)雜數(shù)據(jù)類(lèi)型

  1. --表1
  2. create table tab_1
  3. (
  4. tab1_field1 number,
  5. tab1_field2 varchar2(20),
  6. tab1_field3 varchar2(10)
  7. )
  8. --插入測(cè)試數(shù)據(jù)
  9. insert into TAB_1 (tab1_field1, tab1_field2, tab1_field3) values (1, 'a12', 'a13');
  10. insert into TAB_1 (tab1_field1, tab1_field2, tab1_field3) values (2, '張22', '李23');
  11. --表2
  12. create table TAB_2
  13. (
  14. tab2_field1 NUMBER,
  15. tab2_field2 VARCHAR2(20),
  16. tab2_field3 DATE,
  17. tab2_field4 CLOB,
  18. tab2_field5 BLOB
  19. )
  20. --插入測(cè)試數(shù)據(jù)
  21. insert into TAB_2 (tab2_field1,tab2_field2,tab2_field3,tab2_field4,tab2_field5) values ('1','A12',SYSDATE,'測(cè)試1','C1');
  22. insert into TAB_2 (tab2_field1,tab2_field2,tab2_field3,tab2_field4,tab2_field5) values ('1','測(cè)試22',SYSDATE,'測(cè)試2','C2');
  23. insert into TAB_2 (tab2_field1,tab2_field2,tab2_field3,tab2_field4,tab2_field5) values ('1','嘿嘿32',SYSDATE,'測(cè)試3','C3');

        創(chuàng)建目標(biāo)用戶和表空間

  1. /*第1步:創(chuàng)建臨時(shí)表空間 */
  2. create temporary tablespace test_target_temp
  3. tempfile 'D:\Oracle11g\oradata\test_target_temp.dbf'
  4. size 10m
  5. autoextend on
  6. next 1m maxsize unlimited
  7. extent management local;
  8. /*第2步:創(chuàng)建數(shù)據(jù)表空間 */
  9. create tablespace test_target
  10. logging
  11. datafile 'D:\Oracle11g\oradata\test_target.dbf'
  12. size 10m
  13. autoextend on
  14. next 1m maxsize unlimited
  15. extent management local;
  16. /*第3步:創(chuàng)建用戶并指定表空間 */
  17. create user test_target identified by a123456
  18. default tablespace test_target
  19. temporary tablespace test_target_temp;
  20. /*第4步:給用戶授予權(quán)限 */
  21. grant connect,resource,dba to test_target;

      4、導(dǎo)出源數(shù)據(jù)庫(kù)

        這里需要注意:

        EXP和IMP是客戶端工具程序,它們既可以在客戶端使用,也可以在服務(wù)端使用。

        而EXPDP和IMPDP是服務(wù)端的工具程序,他們只能在ORACLE服務(wù)端使用,不能在客戶端使用。

        IMP只適用于EXP導(dǎo)出的文件,不適用于EXPDP導(dǎo)出文件;IMPDP只適用于EXPDP導(dǎo)出的文件,而不適用于EXP導(dǎo)出文件。

        我們這里需要使用expdp導(dǎo)出,需在服務(wù)端執(zhí)行。

        ①.創(chuàng)建邏輯目錄

  1. --創(chuàng)建一個(gè)dump路徑的對(duì)象
  2. create directory dump_dir1 AS 'D:\Oracle11g\admin\orcl\dpdump';

        當(dāng)然我們這里也可以使用dump默認(rèn)目錄,查詢(xún)默認(rèn)目錄,然后執(zhí)行創(chuàng)建

  1. --查詢(xún)使用默認(rèn)dump路徑
  2. select * from dba_directories where directory_name='DATA_PUMP_DIR';
  3. --創(chuàng)建一個(gè)dump路徑的對(duì)象
  4. create directory dump_dir1 AS 'D:\Oracle11g\admin\orcl\dpdump';
  5. --查詢(xún)已創(chuàng)建的dump路徑對(duì)象
  6. select * from dba_directories where directory_name='DUMP_DIR1';

        但正常情況下這個(gè)目錄只有dba 和system高級(jí)用戶可以使用,所以我們需要給用戶授權(quán),這里我們同時(shí)給源數(shù)據(jù)庫(kù)用戶和目標(biāo)數(shù)據(jù)庫(kù)用戶均加上目錄授權(quán)。

  1. --用戶地址讀寫(xiě)授權(quán)
  2. grant read,write on directory dump_dir1 to test_source;
  3. grant read,write on directory dump_dir1 to test_target;

        ②.導(dǎo)出源數(shù)據(jù)庫(kù)

        在數(shù)據(jù)庫(kù)服務(wù)器上,點(diǎn)擊Win+R彈出命令框,輸入“cmd”,進(jìn)入命令操作框,輸入以下語(yǔ)句,導(dǎo)出數(shù)據(jù)文件dmp。

  1. 1)按用戶導(dǎo)
  2. expdp test_source/a123456 directory=dump_dir1 dumpfile=test_source.dmp schemas=test_source;
  3. 2)并行進(jìn)程parallel
  4. expdp test_source/a123456 directory=dump_dir1 dumpfile=test_source.dmp parallel=40 job_name=test_source
  5. 3)按表名導(dǎo)
  6. expdp test_source/a123456 directory=dump_dir1 dumpfile=test_source.dmp tables=tab_1,tab_2;
  7. 4)按查詢(xún)條件導(dǎo)
  8. expdp test_source/a123456 directory=dump_dir1 dumpfile=test_source.dmp tables=tab_1 query='where deptno=20';
  9. 5)按表空間導(dǎo)
  10. expdp test_source/a123456 directory=dump_dir1 dumpfile=test_source.dmp tablespaces=test_source;
  11. 6)導(dǎo)整個(gè)數(shù)據(jù)庫(kù)
  12. expdp test_source/a123456 directory=dump_dir1 dumpfile=test_source.dmp full=y;

         我采用按用戶導(dǎo)出測(cè)試,發(fā)現(xiàn)報(bào)以下錯(cuò)誤

        

        然后打開(kāi)目錄“”發(fā)現(xiàn)該目錄為只讀目錄,去掉只讀選項(xiàng),確認(rèn),同時(shí)增加日志文件指向

        再次嘗試,成功!

        

      5、導(dǎo)入目標(biāo)數(shù)據(jù)庫(kù)

        上面我們已經(jīng)給目標(biāo)數(shù)據(jù)庫(kù)用戶加上了目錄授權(quán),可以通過(guò)下面語(yǔ)句進(jìn)行導(dǎo)入:

  1. 1)導(dǎo)到指定用戶下
  2. impdp test_target/a123456 directory=dump_dir1 dumpfile=test_source.dmp schemas=test_target;
  3. 2)改變表的owner
  4. impdp test_target/a123456 directory=dump_dir1 dumpfile=test_source.dmp tables=test_target.tab_1,test_target.tab_2 remap_schema=test_target;
  5. 3)導(dǎo)入表空間
  6. impdp test_target/a123456 directory=dump_dir1 dumpfile=test_source.dmp tablespaces=test_target;
  7. 4)導(dǎo)入數(shù)據(jù)庫(kù)
  8. impdb test_target/a123456 directory=dump_dir1 dumpfile=test_source.dmp full=y;
  9. 5)追加數(shù)據(jù)
  10. impdp test_target/a123456 directory=dump_dir1 dumpfile=test_source.dmp schemas=test_target table_exists_action

        我們采用導(dǎo)入指定用戶下,同時(shí)改變他的owner和表空間

       impdp test_target/a123456 directory=dump_dir1 dumpfile=test_source.dmp remap_schema=test_source:test_target

remap_tablespace=test_source:test_target

         

        導(dǎo)入完成!

      6、驗(yàn)證導(dǎo)入后表空間

        我們驗(yàn)證下導(dǎo)入后的表所屬用戶和表空間是否有變化

         

        所屬用戶和表空間已發(fā)生改變。

        為了確保blob、clob等字段所屬表空間已改變,我們刪除源數(shù)據(jù)庫(kù)用戶和表空間,測(cè)試目標(biāo)數(shù)據(jù)庫(kù)是否正常

  1. --刪除源數(shù)據(jù)庫(kù)用戶
  2. drop user test_source;
  3. --因?yàn)樗鶆h表空間非空,所以我們加上including contents進(jìn)行表空間刪除
  4. drop tablespace test_source including contents and datafiles;

        然后我們查詢(xún)下帶有clob和blob字段的表數(shù)據(jù)是否正常

        成功,完美!

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類(lèi)似文章 更多