一、最常用导出导入方案—exp、imp命令工具
1、ssh工具连接服务器主机,exp命令导出
1.1将数据库全部数据导出
exp system/password@TestDB file=bak.dmp log=exp.log full=y
1.2 将数据库中system用户与sys用户的表导出
exp system/password@TestDB file=bak.dmp log=exp.log owner=(system,sys)
1.3 将数据库中的表 Table1,Table2 导出
exp system/password@TestDB tables="(table1,table2)" file=bak.dmp log=exp.log
1.4 将数据库中的表tableA中的字段filed1 值为 “王五” 的数据导出
exp system/password@TestDB tables=tableA query=' where filed1='王五' file=bak.dmp log=exp.log
注意:
- 假设想对dmp文件进行压缩,在上面命令后面加上 compress=y 来实现。
- 检查数据版本和导入后的数据库版本是否一致,字符集是否一致,涉及导出的表是否有分区子表等;如果存在问题,建议使用数据泵导出导入。
- 如果是在数据库服务器主机操作,可以不写实例名。实例名一般配置在tns监听文件中。
2、ssh工具连接服务器主机,imp命令导入
2.1 全量导入dmp文件的数据:
imp system/password@TestDB file=bak.dmp log=imp.log ignore=y full=y
2.2 将一个用户所属的数据导入到另一个用户下:
imp system/manager@TestDB file=bak.dmp log=imp.log fromuser=seapark touser=seapark_copy
二、数据泵导出方法-expdp命令
1、ssh工具连接服务器主机,创建数据库目录对象-data_pump
create directory data_pump as '/u01/data_pump';
注意:
- 其中/u01/data_pump是目录对象对应的linux文件目录,只能在数据库服务器上创建。创建目录前,记得检查一下文件目录的可用空间大小,避免导出操作失败。
- 创建目录对象前,先使用select * from dba_directories,查询已创建的所有数据泵目录,其中DATA_PUMP_DIR是默认的,只有拥有sys和system权限的用户才可以使用,其他用户必须创建新的目录对象。
- 创建目录对象对应,只有dba或者给予了directory权限的用户可以操作。dba用户可以使用 grant create any directory to user 命令给其他用户授权。
2、将目录DATA_PUMP的读写权限赋予xxx用户
grant read,write on directory DATA_PUMP to xxx;
3、expdp命令导出
(1)将table1,table2导出
expdp scott/tiger directory=DATA_PUMP dumpfile=emp1.dmp logfile=expdp1.log tables=table1,table2;
(2)按查询条件导出
将emp1表deptno为10的数据导出
expdp scott/tiger directory=DATA_PUMP dumpfile=condition.dmp logfile=condition.log tables=emp1 query=\' WHERE deptno\=\10\'
(3)按表空间导出
expdp \' / as sysdba \' directory=DATA_PUMP dumpfile=soe_tbs.dmp tablespaces=soe
(4)按用户导出
expdp \' / as sysdba \' directory=DATA_PUMP dumpfile=scott.dmp schemas=scott
(5)全库导出
expdp system/manager@orcl directory=dump_dir logfile=expdp1.log dumpfile=full.dmp full=y
注意:
- 导出前检查表占用空间大小。
- 导出文件和日志保存在/u01/data_pump目录下。
- 如果想让导出的文件更小,可以加入compression=data_only 参数
二、数据泵导入方法-impdp命令
(1)将源端数据文件拷贝到目标端的目录下
scp 192.168.3.88:/u01/data_pump/* /u01/oracle/dir
(2)导入用户有变化
impdp \'/ as sysdba \' directory=DIR dumpfile=emp1.dmp remap_schema=scott:wr
(3)导入的表空间有变化
impdp \' / as sysdba \' directory=DIR dumpfile=tbs.dmp logfile=tbs.log remap_tablespace=abc:soe
四、目标库远程连接源库导入
①创建目录对象
create directory data_pump as '/u01/data_pump';
②授读写权限
grant read,write on directory DATA_PUMP to zyz;
③配置tnsnames
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = xxbora)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
④创建dblink
create public database link orcl18 connect to zyz identified by zyz using 'ORCL18';
select count(*) from zyz.userinfo@orcl18;
⑤导入数据
impdp zyz/zyz directory=data_pump network_link=orcl18 schemas=zyz logfile=zyz.log remap_tablespace=JCTABLESPACE:ZYZTABLESPACE exclude=statistics
⑥收集统计数据
exec dbms_stats.gather_schema_stats(ownname=>'ZYZ',estimate_percent=>10,degree=>8,cascade=>true,granularity=>'ALL');