EXPDP数据导出
一、新建逻辑目录
最好以system等管理员创建逻辑目录,Oracle不会自动创建实际的物理目录“D:\oracleData”(务必手动创建此目录),仅仅是进行定义逻辑路径backup_path;(backup_path 为逻辑路径名称)
create directory backup_path as 'D:\beifen';
更换逻辑目录:create or replace directory backup_path as 'D:\beifen1';
二、查看逻辑目录是否创建成功
select * from dba_directories
三、给库赋权限
grant exp_full_database,imp_full_database to freedom;
grant read,write on directory backup_path to freedom;
四、用expdp导出数据
用法及解释:
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=backup_path logfile=expdp.log;
2)导出指定表
expdp scott/tiger@orcl tables=emp,dept dumpfile=expdp.dmp directory=backup_path logfile=expdp.log;
3)按查询条件导
expdp scott/tiger@orcl directory=backup_path dumpfile=expdp.dmp tables=empquery='where deptno=20' logfile=expdp.log;
4)按表空间导
expdp system/manager@orcl directory=backup_path dumpfile=tablespace.dmp tablespaces=temp,example logfile=expdp.log;
5)导整个数据库
expdp scott/123@127.0.0.1/orcl directory=backup_path dumpfile=ly.dmp full=y logfile=expdp.log;
6)指定不导出的某些表(注意转义,下面文章中有方法,建议使用parfile)
expdp scott/123@orcl directory=backup_path dumpfile=freedom.dmp logfile=dmuser_schema_29.log EXCLUDE=TABLE:"IN('MD_IOT_TAGHISTORY0330','MD_IOT_TAGHISTORY','MD_IOT_TAGHISMONTH','MD_IOT_TAGHISDAY','MD_IOT_TAGALERT1','MD_IOT_TAGALERT')"
注意:expdp/impdp由于源数据版本高于目标数据库版本,遭遇ORA-39001、ORA-39000,ORA-39142,解决只需要expdp导出时加上目标数据的version,具体问题及解决如下:
如两个数据库版本不一致,导出时需要在后面指定要导入数据库的版本号 :version=11.1.0.6.0
IMPDP数据导入
在正式导入数据前,要先确保要导入的用户已存在,如果没有存在,请先用下述命令进行新建用户
注释:
1)导入用户(从用户scott导入到用户scott)
impdp scott/tiger@orcl directory=backup_path dumpfile=expdp.dmp schemas=scott logfile=impdp.log;
2)导入表(从scott用户中把表dept和emp导入到system用户中)
impdp system/manager@orcl directory=backup_path 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=backup_path dumpfile=tablespace.dmp tablespaces=example logfile=impdp.log;
4)导入整个数据库
impdb system/manager@orcl directory=backup_path dumpfile=full.dmp full=y logfile=impdp.log;
5)追加数据
impdp system/manager@orcl directory=backup_path dumpfile=expdp.dmp schemas=systemtable_exists_action logfile=impdp.log;
其他用到的方法,顺手记录一下
①parfile用法(解决特殊字符转义问题及其他莫名其妙的问题)
windows打开一记事本,Linux就touch一文本。
内容想添加啥就添加啥:
directory=backup_path dumpfile=freedom.dmp logfile=dmuser_schema_29.log version=11.1.0.6.0 EXCLUDE=TABLE:"IN('MD_IOT_TAGHISTORY0330','MD_IOT_TAGHISTORY','MD_IOT_TAGHISMONTH','MD_IOT_TAGHISDAY','MD_IOT_TAGALERT1','MD_IOT_TAGALERT')"
保存在D:\dump\par.txt ,文件名随便起.
C:\Users\admin>expdp admin/admin parfile=d:\dump\par.txt
②查看导入导出job
select job_name,state from dba_datapump_jobs
③查看当前数据库版本
select * from v$version;