Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1753364
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4341
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: Oracle

2016-04-19 22:01:59

在开始某一个项目的时候,我们经常需要搭建测试环境,初始数据的导致其中不可避免涉及异构平台数据迁移。oracle提供的工具聆郎满目,有时候也会挑花眼,在这种情况下DBA就需要根据不同的场景,指定不同的迁移方案,这里我们对oracle涉及的迁移方案进行一下简单的总结,然后重点介绍一下DATA PUMP的使用。
oracle提供的可以用来数据迁移的工具主要数据迁移工具(DATA MIGRATION TOOLS),数据复制工具(DATA REPLICATION TOOLS),数据备份恢复工具(DATA BUCKUP/RESTORE TOOLS)三大类。
其中DMT的可用工具有:oracle data  pump,transportable tablespace,copy over db link,sql * loader utility,sql devoloper,sql*plus copy command
custom pl/sql procedure
DRT可用的工具有:ORACLE GODEN GATE,ORALCE STREAM
DBR的工具需要配套使用:oracle recovery manager,oracle active data guard 
每种工具使用的场景都不同,如copy over db link,必须保证source DB与target DB网络连通,而 SQL * LOADER则是导入外部数据源txt/csv/asii的最好选择。
在考虑数据迁移的时候,考虑的主要因素有三个,数据库平台,数据库版本,平台版本,还有就是是否有数据时效性要求。这里我们考虑对简单的场景:异构平台不同oracle数据迁移,网络不同,这里我们最好的选择即EXPDP/EXIMP
EXPDP/EXIMP 的主要特点有:
Some advantages of Data Pump:
x perform fine-grained object selection 更细的对象颗粒度控制
x ability to estimate jobs times 可以预估作业执行时间
x ability to restart failed jobs  断点重提
x monitor running jobs  监控作业运行进度
x directly load a database from a remote instance via the network 通过网络可以直接实现类似COPY OVER DB LINK 功能。
x remapping capabilities 模式重命名
x improved performance using parallel executions 并行执行
总而言之,言而总之,该工具是你数据迁移的首选。

EXPDP一般的使用步骤如下:
1 Create a directory on both the source and target databases with the following command.

点击(此处)折叠或打开

  1. create or replace directory expdp_dir as 'c:\backup\database\expdp';
Extract DDL for user to be moved (ProdUser) to the Target System
这一步其实没有必要


点击(此处)折叠或打开

  1. select dbms_metadata.get_ddl('USER', username) || '/' usercreate from dba_us
  2. username = ‘PRODUSER’;
  3. SELECT DBMS_METADATA.GET_GRANTED_DDL('ROLE_GRANT','PRODUSER') FR
  4. DUAL;
  5. SELECT DBMS_METADATA.GET_GRANTED_DDL('SYSTEM_GRANT','PRODUSER')
  6. DUAL;
  7. SELECT DBMS_METADATA.GET_GRANTED_DDL('OBJECT_GRANT','PRODUSER')
  8. DUAL;
Move the Data

点击(此处)折叠或打开

  1. C:\>expdp system directory=expdp_dir dumpfile=export_produser.dmp
  2. schemas=PROSUSER logfile=export_produser.log
Create user in Target Database (use output from step #2)
5 Move the output from step #3 to the target machine
6. Import Data into the target database.

点击(此处)折叠或打开

  1. C:\>impdp system directory=expdp_dir dumpfile=export_produser.dmp
  2. schemas=PRODUSER logfile=produser_import.log

点击(此处)折叠或打开

  1. If you want to change the name of the user that the data is loaded into then you will use
  2. the remap_schema parameter (see below). You may also want to remap_tablespace(s).

  3. remap_schema=PRODUSER:TESTUSER
  4. remap_tablespace=PROD_DATA:TEST_DATA
  5. remap_tablespace=PROD_INDX:TEST_INDX

6 通过DBlink直传,同时支持数据加密


这里在简单的介绍一下DATA PUMP的对象颗粒度控制
以schema=(hr,scott)为例进行介绍
schema=hr

点击(此处)折叠或打开

  1. select table_name from user_tables;

  2. REGIONS
  3. LOCATIONS
  4. DEPARTMENTS
  5. JOBS
  6. EMPLOYEES
  7. JOB_HISTORY
  8. COUNTRIES
schema=soctt

点击(此处)折叠或打开

  1. select table_name from user_tables;
  2. DEPT
    EMP
    BONUS
    SALGRADE



1 支持模式匹配:

点击(此处)折叠或打开

  1. C:\Users\kinfinger>expdp xx/xx directory=hrdir dumpfile=%u.dump tables=
  2. %e% logfile=mt.log

2 支持多个TABLES

点击(此处)折叠或打开

  1. C:\Users\kinfinger>expdp hr/kinfinger directory=hrdir dumpfile=%u.dump tables=
  2. (jobs,regions) logfile=mt.log
3 不同SCHEMA下table的导出,输出结果支持多个dump文件,参数%u 同filesize配合使用,范围01-99

点击(此处)折叠或打开

  1. C:\Users\kinfinger>expdp system/kinfinger directory=hrdir dumpfile=%u.dump ta
  2. bles=(hr.jobs,scott.emp) logfile=mt.log



场景实际测试:

点击(此处)折叠或打开

  1. SQL> select view_name from user_views;

  2. VIEW_NAME
  3. ------------------------------
  4. JVIEW
  5. MVIEW

  6. SQL> select table_name ,tablespace_name from user_tables;

  7. TABLE_NAME TABLESPACE_NAME
  8. ------------------------------ ------------------------------
  9. REGIONS HRSPACE
  10. LOCATIONS HRSPACE
  11. DEPARTMENTS HRSPACE
  12. JOBS HRSPACE
  13. EMPLOYEES HRSPACE
  14. JOB_HISTORY HRSPACE
  15. COUNTRIES

  16. 已选择7行。

  17. C:\Users\kinfinger>expdp hr/kinfinger directory=hrdir dumpfile=hr.dump logfil
  18. e=mt.logle
导入HR3 ,REMAP to hr3 

点击(此处)折叠或打开

  1. drop user hr2 cascade;
  2. drop tablespace mapsp ;
  3.  create tablespace mapsp datafile 'g:/mapsp/m.data' size 20m nologging;

  4. create user hr3 identified by kinfinger default tablespace mapsp quota unli
  5. mited on users;
  6. grant create session,connect to hr3;
  7.  grant read,write on directory hrdir to hr3;
  8. 成功。

  9. grant create table,create view to hr3
连接到: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
ORA-39006: 内部错误
ORA-39068: 在 PROCESS_ORDER=-4 的行中主表数据无效
ORA-01950: 对表空间 'MAPSP' 无权限
ORA-39097: 数据泵作业出现意外的错误 -1950


点击(此处)折叠或打开

  1. grant resource to hr3;
  2. SQL> select * from hr.mview where rownum < 2 union all  select * from hr3.mview
    where rownum < 2 ;


    MAX_SALARY
    ----------
         40000

知道数据导入成功,且REMAP SCHEMA成功。

关于resource,其中涉及很多权限,涉及的查询如下:

点击(此处)折叠或打开

  1. SELECT TYPE FROM USER_SOURCE
查询当前用户:

点击(此处)折叠或打开

  1. show user;




















阅读(3666) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~