Oracle-数据泵expdp/impdp实操
- 概述
- 數據泵的作用:
- 數據泵的特點與傳統導出導入的區別
- 需求描述
- 方案分析
- 操作步驟
- expdp
- 1.創建邏輯目錄
- 2.查看目錄
- 3.給cc用戶賦予在指定目錄的操作權限
- 4. 導出數據
- scp dmp 到目標主機
- impdp
- 刪除用戶
- 創建用戶
- 賦予權限
- 使用Oracle用戶導入
- 相同的schema
- 不同的schema
- REMAP_SCHEMA、REMAP_TABLESPACE、REMAP_DATAFILE
- 不同路徑下的dump文件同時導入
- 查看腳本
- schema解釋
- expdp
- 注意事項
- 導出導入全庫
- 全庫導出
- 單個實例
- 多個實例
- 全庫導入
- 全庫導出
- 單表 以及單表按條件導出導入
概述
數據泵的作用:
- 1.實現邏輯備份和邏輯恢復
- 2.在數據庫用戶之間移動對象
- 3.在數據庫之間移動對象
- 4.實現表空間轉移
數據泵的特點與傳統導出導入的區別
- 1.EXP和IMP是客戶段工具程序, EXPDP和IMPDP是服務端的工具程序
- 2.EXP和IMP效率比較低. EXPDP和IMPDP效率高
- 3.數據泵功能強大并行、過濾、轉換、壓縮、加密、交互等等
- 4.數據泵不支持9i以前版本, EXP/IMP短期內還是比較適用
- 5.同exp/imp數據泵導出包括導出表,導出方案,導出表空間,導出數據庫4種方式.
Oracle官方指導文檔
需求描述
198測試環境的cc用戶下的全量數據,需要同步到到準生產環境197一份
方案分析
因數據庫版本為11.2(大于Oracle10g)初步確定使用數據泵的方式從198導出后導入197數據庫
操作步驟
expdp
SQL 窗口下執行以下操作
1.創建邏輯目錄
創建邏輯目錄
SQL> create directory dir_dp as '/oracle/oracle11';2.查看目錄
同時查看操作系統是否存在,如果不存在,mkdir -p 新建目錄 否則出錯
SELECT privilege, directory_name, DIRECTORY_PATHFROM user_tab_privs t, all_directories dWHERE t.table_name(+) = d.directory_nameORDER BY 2, 1;或者 可以查看
select * from dba_directories ;DATA_PUMP_DIR就是直接可以用的DUMP目錄,導出文件和導入文件就放在上面的路徑下,如果沒有,則需要手動創建并賦給讀/寫權限.
如果沒有查詢到的話,刷新一下
SQL> host ls /oracle/oracle113.給cc用戶賦予在指定目錄的操作權限
以dba用戶等管理員賦予
SQL> Grant read,write on directory dir_dp to cc;4. 導出數據
使用主機的Oracle用戶
按用戶導/導出指定schema:
oracle@entel2:[/oracle]$expdp cc/password@//10.45.7.198:1521/cc schemas=cc directory=dir_dp dumpfile =expdp_test1.dmp logfile=expdp_test1.log;...........Dump file set for CC.SYS_EXPORT_SCHEMA_01 is:/oracle/oracle11/expdp_test1.dmp Job "CC"."SYS_EXPORT_SCHEMA_01" successfully completed at Sat Aug 20 12:02:13 2016 elapsed 0 00:01:33參數解析:
- schemas:數據庫用戶別名 而非實例名
- cc/password@//10.45.7.198:1521/cc port后面指定的是實例名 ,最前面的是用戶名和密碼
scp dmp 到目標主機
[root@entel2 ~]# scp expdp_198.dmp oracle@10.45.7.197:/oracle/oracle11impdp
刪除用戶
SQL> DROP USER cc CASCADE;創建用戶
SQL>CREATE USER cc IDENTIFIED BY password DEFAULT TABLESPACE TAB_CC;賦予權限
SQL>GRANT CONNECT ,RESOURCE TO cc ; SQL>GRANT ALL PRIVILEGES TO cc ; SQL>GRANT DBA TO cc ;使用Oracle用戶導入
相同的schema
oracle@entel1:[/oracle]$impdp cc/password@//10.45.7.197:1521/cc schemas=cc directory=dir_dp_198 dumpfile =expdp_198.dmp logfile=expdp_198.log;不同的schema
oracle@entel1:[/oracle]$impdp entelcc_st4/password@//10.45.7.197:1521/cc schemas=cc remap_schema=cc:entelcc_st4 directory=dir_dp_198 dumpfile =expdp_198.dmp logfile=expdp_198.log;說明:
- 目標主機上 directory也需要建立 ,并賦予讀寫權限,如果不指定,則使用缺省目錄 缺省Directory為DATA_PUMP_DIR
DATA_PUMP_DIR這個目錄,是系統缺省目錄,如不單獨指定目錄,dmp文件會在這里,但默認這個目錄其他用戶是沒有權限的。
- dmp文件需要存放到對應的directory目錄下,否者導入報錯
如果目標庫的schema和源庫的schema一致,按以上腳本即可,不一致的情況需要指定,語法如下:schemas=old_schema remap_schema=old_schema:new_schema
否者報錯:
REMAP_SCHEMA、REMAP_TABLESPACE、REMAP_DATAFILE
除了REMAP_SCHEMA,還有REMAP_TABLESPACE、REMAP_DATAFILE 等
1、REMAP_SCHEMA
該選項用于將源方案的所有對象裝載到目標方案中:REMAP_SCHEMA=source_schema:target_schema
2、REMAP_TABLESPACE
將源表空間的所有對象導入到目標表空間中:REMAP_TABLESPACE=source_tablespace:target:tablespace
3、REMAP_DATAFILE 該選項用于將源數據文件名轉變為目標數據文件名,在不同平臺之間搬移表空間時可能需要該選項.
REMAP_DATAFIEL=source_datafie:target_datafile
不同路徑下的dump文件同時導入
需要創建多個directory
指定 dumpfile=dir1:1.dmp,dir2:2.dm
查看腳本
查看各個SCHEMA占用空間的大小
select owner,round(sum(bytes/1024/1024),2)||' MB' as schema_size from dba_segments group by owner order by SCHEMA_SIZE desc;查詢己經導入的對象個數
select owner,count(*) from dba_objects where owner like 'ENTELCC_ST4%' group by owner order by owner;這里不同的schema,之前沒有注意到,導致一直報錯,看到了yfleng2002的博文Oracle schema 級別的數據遷移,贊一個。
schema解釋
A schema is a collection of database objects (used by a user.).
Schema objects are the logical structures that directly refer to the database’s data.
A user is a name defined in the database that can connect to and access objects.
Schemas and users help database administrators manage database security.
一個用戶一般對應一個schema,該用戶的schema名等于用戶名,并作為該用戶缺省schema。
Oracle數據庫中不能新創建一個schema,要想創建一個schema,只能通過創建一個用戶的方法解決(Oracle中雖然有create schema語句,但是它并不是用來創建一個schema的),在創建一個用戶的同時為這個用戶創建一個與用戶名同名的schem并作為該用戶的缺省shcema。
即schema的個數同user的個數相同,而且schema名字同user名字一一 對應并且相同,所有我們可以稱schema為user的別名,雖然這樣說并不準確,但是更容易理解一些。
一個用戶有一個缺省的schema,其schema名就等于用戶名,當然一個用戶還可以使用其他的schema。如果我們訪問一個表時,沒有指明該表屬于哪一個schema中的,系統就會自動給我們在表上加上缺省的sheman名。比如我們在訪問數據庫時,訪問scott用戶下的emp表,通過select * from emp; 其實,這sql語句的完整寫法為select * from scott.emp。在數據庫中一個對象的完整名稱為schema.object,而不屬user.object。類似如果我們在創建對象時不指定該對象的schema,在該對象的schema為用戶的缺省schema。這就像一個用戶有一個缺省的表空間,但是該用戶還可以使用其他的表空間,如果我們在創建對象時不指定表空間,則對象存儲在缺省表空間中,要想讓對象存儲在其他表空間中,我們需要在創建對象時指定該對象的表空間。
以上schema解釋參考oracle中schema指的是什么?,感謝分享
注意事項
EXPDP和IMPDP是服務端的工具程序,只能在ORACLE服務端使用,不能在客戶端使用
低版本是不能導入高版本dmp,需要在高版本的EXPDP導出時指定版本號導出。低版本IMPDP無需指定版本。
例如:11.2.0.4導入到10.2.0.5
導出導入全庫
全庫導出
單個實例
oracle@entel2:[/oracle]$expdp \'/ as sysdba\' directory=dir_dp full=y dumpfile=fulldb.dmp parallel=2;【開始執行........上述命令也可以增加logfile等 自行決定】 Export: Release 11.2.0.4.0 - Production on Mon Oct 24 18:47:01 2016Copyright (c) 1982, 2011, Oracle and/or its affiliates. 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 Starting "SYS"."SYS_EXPORT_FULL_01": "/******** AS SYSDBA" directory=dir_dp full=y dumpfile=fulldb.dmp parallel=2 Estimate in progress using BLOCKS method... Processing object type DATABASE_EXPORT/SCHEMA/TABLE/TABLE_DATA Total estimation using BLOCKS method: 2.294 GB Processing object type DATABASE_EXPORT/TABLESPACE . . exported "ZMC"."NM_ALARM_EVENT" 317.5 MB 467108 rows Processing object type DATABASE_EXPORT/PROFILE Processing object type DATABASE_EXPORT/SYS_USER/USER Processing object type DATABASE_EXPORT/SCHEMA/USER Processing object type DATABASE_EXPORT/ROLE Processing object type DATABASE_EXPORT/GRANT/SYSTEM_GRANT/PROC_SYSTEM_GRANT Processing object type DATABASE_EXPORT/SCHEMA/GRANT/SYSTEM_GRANT Processing object type DATABASE_EXPORT/SCHEMA/ROLE_GRANT Processing object type DATABASE_EXPORT/SCHEMA/DEFAULT_ROLE Processing object type DATABASE_EXPORT/SCHEMA/TABLESPACE_QUOTA ..............PARALLEL 更改當前作業的活動 worker 的數目。PARALLEL=<worker 的數目>。指定執行導出操作的并行進程個數,默認值為1
FULL 導出整個數據庫 默認為N。
多個實例
如果存在多個實例,需要先export ORACLE_SID
tbprocsdb1:[/oracle$]export ORACLE_SID=testbed tbprocsdb1:[/oracle$]expdp \'/ as sysdba\' directory=TMP_DIR full=y dumpfile=fulldb20180512.dmp logfile=full20180512.log parallel=4;Export: Release 11.2.0.4.0 - Production on Sat May 12 14:06:38 2018Copyright (c) 1982, 2011, Oracle and/or its affiliates. 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 Starting "SYS"."SYS_EXPORT_FULL_01": "/******** AS SYSDBA" directory=TMP_DIR full=y dumpfile=fulldb20180512.dmp logfile=full20180512.log parallel=4 Estimate in progress using BLOCKS method... Processing object type DATABASE_EXPORT/SCHEMA/TABLE/TABLE_DATA全庫導入
$ impdp \'/ as sysdba\' directory=dir_dp full=y dumpfile=fulldb.dmp parallel=2 table_exists_action=replace;更多詳情
單表 以及單表按條件導出導入
artisandb:[/oracle$] expdp xxx/xxxx@PR_CC directory=CC_DUMP include=table:\" =\'TMP_BATCH203\' \" dumpfile =TMP_BATCH203.dmp logfile=TMP_BATCH203.log;Export: Release 11.2.0.4.0 - Production on Thu May 31 14:31:41 2018Copyright (c) 1982, 2011, Oracle and/or its affiliates. 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 Starting "CRM"."SYS_EXPORT_SCHEMA_02": crm/********@PR_CC directory=CC_DUMP include=table:" ='TMP_BATCH203' " dumpfile=TMP_BATCH203.dmp logfile=TMP_BATCH203.log Estimate in progress using BLOCKS method... Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA Total estimation using BLOCKS method: 64 KB Processing object type SCHEMA_EXPORT/TABLE/TABLE Processing object type SCHEMA_EXPORT/TABLE/COMMENT . . exported "CRM"."TMP_BATCH203" 9.75 KB 50 rows Master table "CRM"."SYS_EXPORT_SCHEMA_02" successfully loaded/unloaded ****************************************************************************** Dump file set for CRM.SYS_EXPORT_SCHEMA_02 is:/ccexp/cc/TMP_BATCH203.dmp Job "CRM"."SYS_EXPORT_SCHEMA_02" successfully completed at Thu May 31 14:31:49 2018 elapsed 0 00:00:08artisandb:[/oracle$]或者
artisandb:[/oracle$] expdp xxx/xxxxx@PR_CC directory=CC_DUMP Tables=TMP_BATCH203 dumpfile =TMP_BATCH204.dmp logfile=TMP_BATCH204.log;Export: Release 11.2.0.4.0 - Production on Thu May 31 14:36:25 2018Copyright (c) 1982, 2011, Oracle and/or its affiliates. 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 Starting "CRM"."SYS_EXPORT_TABLE_01": crm/********@PR_CC directory=CC_DUMP Tables=TMP_BATCH203 dumpfile=TMP_BATCH204.dmp logfile=TMP_BATCH204.log Estimate in progress using BLOCKS method... Processing object type TABLE_EXPORT/TABLE/TABLE_DATA Total estimation using BLOCKS method: 64 KB Processing object type TABLE_EXPORT/TABLE/TABLE Processing object type TABLE_EXPORT/TABLE/COMMENT . . exported "CRM"."TMP_BATCH203" 9.75 KB 50 rows Master table "CRM"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded ****************************************************************************** Dump file set for CRM.SYS_EXPORT_TABLE_01 is:/ccexp/cc/TMP_BATCH204.dmp Job "CRM"."SYS_EXPORT_TABLE_01" successfully completed at Thu May 31 14:36:28 2018 elapsed 0 00:00:02artisandb:[/oracle$]按照條件
artisandb:[/ccexp/cc$] expdp xxx/xxxxx@PR_CC directory=CC_DUMP Tables=TMP_BATCH203 dumpfile =TMP_BATCH206.dmp logfile=TMP_BATCH206.log query=TMP_BATCH203:'"where acc_nbr_id=170686"';Export: Release 11.2.0.4.0 - Production on Thu May 31 14:40:16 2018Copyright (c) 1982, 2011, Oracle and/or its affiliates. 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 Starting "CRM"."SYS_EXPORT_TABLE_01": crm/********@PR_CC directory=CC_DUMP Tables=TMP_BATCH203 dumpfile=TMP_BATCH206.dmp logfile=TMP_BATCH206.log query=TMP_BATCH203:"where acc_nbr_id=170686" Estimate in progress using BLOCKS method... Processing object type TABLE_EXPORT/TABLE/TABLE_DATA Total estimation using BLOCKS method: 64 KB Processing object type TABLE_EXPORT/TABLE/TABLE Processing object type TABLE_EXPORT/TABLE/COMMENT . . exported "CRM"."TMP_BATCH203" 7.601 KB 1 rows Master table "CRM"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded ****************************************************************************** Dump file set for CRM.SYS_EXPORT_TABLE_01 is:/ccexp/cc/TMP_BATCH206.dmp Job "CRM"."SYS_EXPORT_TABLE_01" successfully completed at Thu May 31 14:40:27 2018 elapsed 0 00:00:10artisandb:[/ccexp/cc$]然后drop掉TMP_BATCH203
artisandb:[/ccexp/cc$] impdp xxx/xxxxx@PR_CC directory=CC_DUMP dumpfile =TMP_BATCH206.dmp logfile=TMP_BATCH_1.log ;Import: Release 11.2.0.4.0 - Production on Thu May 31 14:44:26 2018Copyright (c) 1982, 2011, Oracle and/or its affiliates. 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 Master table "CRM"."SYS_IMPORT_FULL_01" successfully loaded/unloaded Starting "CRM"."SYS_IMPORT_FULL_01": crm/********@PR_CC directory=CC_DUMP dumpfile=TMP_BATCH206.dmp logfile=TMP_BATCH_1.log Processing object type TABLE_EXPORT/TABLE/TABLE Processing object type TABLE_EXPORT/TABLE/TABLE_DATA . . imported "CRM"."TMP_BATCH203" 7.601 KB 1 rows Processing object type TABLE_EXPORT/TABLE/COMMENT Job "CRM"."SYS_IMPORT_FULL_01" successfully completed at Thu May 31 14:44:27 2018 elapsed 0 00:00:01artisandb:[/ccexp/cc$]查看數據:
exclude和include更多用法
總結
以上是生活随笔為你收集整理的Oracle-数据泵expdp/impdp实操的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 修改centos等linux的hostn
- 下一篇: Oracle-OLAP和OLTP解读