Oracle數據庫導入(expdp)和導出(impdp)


文檔最后,列出了常用的一些導入導出的場景,以及一些導入導出的屬性說明。

一、數據庫導出(expdp)

  1. 使用sys或system賬號登錄oracle

             通過"Window + R" 打開dos命令行界面,使用sys或system登錄oracle。格式:sqlplus sys/密碼@數據庫實例名 as sysdba

          

         2、創建邏輯目錄 : create or replace directory data_dir as 'E:\orcl\data';

             data_dir為路徑名稱,可自命名,E:\orcl\data為數據庫導出文件存放路徑(路徑必須存在); 

             創建備份邏輯目錄,此目錄不是真實的目錄,此目錄需要手動在數據庫服務端創建。

              

            通過 select * from dba_directories  可以查看所有的目錄.

         3、為用戶授予訪問數據目錄的權限,輸入命令:Grant read,write on directory data_dir to dbuser;

        dbuser為數據庫用戶名(與第4步中相同)

    

          4、導入導出操作授權,輸入命令:grant exp_full_database,imp_full_database to dbuser; 

          5、退出,輸入命令:exit;

          6、數據導出,執行命令:

      expdp dbuser/123456@orcl schemas=dbuser dumpfile=expdp.dmp directory=data_dir logfile=expdp.log

           注意:命令結束不需要加“;

           

    expdp [為用戶名]/[密碼]@[服務名] 
      schemas=[為用戶名] 
      dumpfile=[導出數據庫文件(可自命名)] 
      directory=[目錄名] 
      logfile=[日志文件文件名(可自命名)]
 

數據庫還原前准備

1、創建表空間   

    create tablespace tbs_dbsunny datafile 
    'D:\app\Sunny\oradata\TableSpace\tbs_dbsunny.DBF' size 1G
    autoextend on next 100M maxsize unlimited logging
    extent management local autoallocate
    segment space management auto;

2、創建臨時表空間

create temporary tablespace tbs_dnsunny_temp tempfile 'D:\app\Sunny\oradata\TableSpace\tbs_dnsunny_temp.DBF' size 1000M autoextend on next 100M maxsize unlimited ;

3、創建用戶

create user sunny identified by sunny123 DEFAULT TABLESPACE  tbs_dbsunny  TEMPORARY TABLESPACE tbs_dnsunny_temp;

4、授權

alter user sunny temporary tablespace tbs_dnsunny_temp;
ALTER USER sunny QUOTA UNLIMITED ON TBS_DBSUNNY grant connect to sunny; grant resource to sunny; grant dba to sunny; grant create trigger to sunny; grant create session to sunny; grant create sequence to sunny; grant create synonym to sunny; grant create table to sunny; grant create view to sunny; grant create procedure to sunny; grant alter session to sunny; grant execute on ctxsys.ctx_ddl to sunny; grant create job to sunny; grant sysdba to sunny; alter user sunny default role all;

  -- 刪除這個用戶以及這個用戶下的所有對象
  DROP USER sunny CASCADE;

數據庫導入(impdp)

1、使用sys或system 登錄

      通過"Window + R" 打開dos命令行界面,使用sys或system登錄oracle。格式:sqlplus sys/密碼@數據庫實例名 as sysdba

sqlplus sys/12345@dborcl as sysdba

2、創建邏輯目錄,並手動創建真實目錄,並將備份文件DMP,放進此目錄下

sqlplus  create or replace directory data_dir as 'E:\orcl\data';

3、給目標用戶授權

sqlplus  grant read,write on directory data_dir to sunny;

4、導入:在dos命令行,執行

注意 : impdp 語句 后面 不要加 " ; "

impdp sunny/sunny123@DBSUNNY directory=data_dir dumpfile=EXPDPBUDGET.DMP logfile=impbudgett.log remap_schema =budgett:sunny remap_tablespace=PIMS:TBS_DBSUNNY

注:remap_schema=olduser:newuser               表示把左邊的olduser用戶的數據,導入到右邊的newuser 用戶里面

       remap_tablespace=old_tbs:new_tbs          表示把將要導入的備份庫的表空間old_tbs,導入到新庫替換為 new_tbs  

 

expdp導出數據

語法: expdp 用戶名/密碼@ip地址/實例 ....     ip地址不寫默認就是本地

屬性說明:
userid=test/test            --導出的用戶,本地用戶!!
directory=dmpfile          --導出的邏輯目錄,一定要在oracle中創建完成的,並且給用戶授權讀寫權限
dumpfile=xx.dmp      --導出的數據文件的名稱,如果想在指定的位置的話可以寫成dumpfile=/home/oracle/userxx.dmp
logfile=xx.log          --日志文件,如果不寫這個參數的話默認名稱就是export.log,可以在本地的文件夾中找到
schemas=userxx       --使用dblink導出的用戶不是本地的用戶,需要加上schema來確定導出的用戶,類似於exp中的owner,但還有一定的區別
EXCLUDE=TABLE:"IN('T1','T2','T3')"     --exclude 可以指定不導出的東西,table,index等,后面加上不想導出的表名
network_link=db_local          --這個參數是使用的dblink來遠程導出,需要指定dblink的名稱

列出一些場景:

1)導出用戶及其對象
expdp scott/tiger@orcl schemas=scott dumpfile=expdp.dmp directory=dump_dir logfile=expdp.log; 2)導出指定表 expdp scott/tiger@orcl tables=emp,dept dumpfile=expdp.dmp directory=dump_dir logfile=expdp.log; 3)按查詢條件導 expdp scott/tiger@orcl directory=dump_dir dumpfile=expdp.dmp tables=empquery='where deptno=20' logfile=expdp.log; 4)按表空間導 expdp system/manager@orcl directory=dump_dir dumpfile=tablespace.dmp tablespaces=temp,example logfile=expdp.log; 5)導整個數據庫 expdp scott/123@127.0.0.1/orcl directory=dump_dir dumpfile=ly.dmp full=y  logfile=expdp.log;
一般用的都是導出整個數據庫,本人使用的代碼:
//包含所有用戶的表、視圖、索引等
expdp JCPT/123@127.0.0.1/orcl directory=mydata dumpfile=ly.dmp full=y  logfile=expdp.log; //指定用戶的表、視圖、索引等 expdp JCPT/123@127.0.0.1/orcl directory=mydata schemas=jcpt dumpfile=ly.dmp logfile=expdp.log;

impdp 導入

列出一些場景:

1)導入用戶(從用戶scott導入到用戶scott)
impdp scott/tiger@orcl directory=dump_dir dumpfile=expdp.dmp schemas=scott logfile=impdp.log; 2)導入表(從scott用戶中把表dept和emp導入到system用戶中) impdp system/manager@orcl directory=dump_dir dumpfile=expdp.dmp tables=scott.dept,scott.emp remap_schema=scott:system logfile=impdp.log table_exists_action=replace (表空間已存在則替換); 3)導入表空間 impdp system/manager@orcl directory=dump_dir dumpfile=tablespace.dmp tablespaces=example logfile=impdp.log; 4)導入整個數據庫 impdb system/manager@orcl directory=dump_dir dumpfile=full.dmp full=y logfile=impdp.log; 5)追加數據 impdp system/manager@orcl directory=dump_dir dumpfile=expdp.dmp schemas=systemtable_exists_action logfile=impdp.log;

日常使用的:

//把用戶jcpt中所有的表導入到lyxt用戶下
impdp lyxt/lyxt123@127.0.0.1/orcl directory=mydata dumpfile=LY.DMP   remap_schema=jcpt:lyxt logfile=ims20171122.log table_exists_action=replace

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM