Oracle数据库备份与还原有两套语句:exm/imp 和expdp/impdp
1. 使用expdp,impdp命令
准备:
1.创建逻辑目录:
查看所有目录:select * from dba_directories;
创建数据泵目录:create or replace direcory dp_dir as 'D:\oradata\orcl';
创建一个dbp权限的数据泵用户: grant dba to dp identified by dp;
用户授权数据泵读写权限:grant read,write on directory data to dp
数据库备份:建逻辑目录,给用户授予读写权限
备份: expdp dp/123456@orcl directory=dp_dir dumpfile=student.dmp logfile=student.log schemas =STU
数据库还原: 将还原文件放到数据泵目录里: 创建和备份数据库相同的用户,创建表空间,为用户指定表空间:
还原: impdp dp/123456@orcl directory=dp_dir dumpfile=student.dmp logfile=student.log schemas = STU
常见几种导出:
--按用户导出表
expdp dp/123456@orcl directory=dp_dir dumpfile=student.dmp schemas=STU
--按表名导出表
expdp scott/tiger@orcl directory=dp_dir dumpfile=expdp.dmp tables=emp,dept ;
--按查询条件导出表
expdp scott/tiger@orcl directory=dp_dir dumpfile=expdp.dmp tables=emp query=‘WHERE deptno=20‘;
--按表空间导出表
expdp system/manager directory=dp_dir dumpfile=tablespace.dmp tablespaces=temp,example;
--导整个数据库
expdp system/manager directory=dp_dir dumpfile=full.dmp full=y;
常见几种导入:
--按用户导入
impdp dp/123456@orcl directory=dp_dir dumpfile=student.dmp schemas=STU
--按表空间导入
impdp system/manager direcotry=dp_dir dumpfile=tablespace.dmp tablespaces=example;
--导入数据库
impdp system/manager directory=dp_dir dumpfile=full.dmp full=y;
--追加数据
impdp system/manager directory=dp_dir dumpfile=expdp.dmp schemas=system TABLE_EXISTS_ACTION
--改变表的owner
impdp system/manager directory=dp_dir dumpfile=expdp.dmp tables=scott.dept REMAP_SCHEMA=scott:system;
注:
12c推出了可插拔数据库,在一个容器cdb中以多租户的形式同时存在多个数据库pdb。在为pdb做数据泵导入导出时和传统的数据库有少许不同。
2,导入导出时需要在userid参数内指定其tansnames的值 比如 userid=user/pwd@tnsname
设置tnsnames.ora,增加pdborocl。SERVICE_NAME为pdb的实例名,这里为pdborcl
位置:oracle\product\12.2.0\dbhome_1\network\admin
orclpdb =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = xqzt)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME =orclpdb)
)
)