oracle数据迁移

经过这个文章演示一下Oracle的表空间迁移流程以及须要注意的诸多事项。

实验目标:将ora10g数据库实例上的表空间TBS_SEC_D迁移到secooler数据库实例上
操做系统:Redhat 5.3
数据库:Oracle 10.2.0.3

【实验BEGIN
【注意事项一】:导入以前,目标数据库中用户必须已经存在存在。
【注意事项二】:导入以前,目标数据库中不能存在同名的表空间,如迁移同名的表空间,须要对迁移以前的源数据库或待迁入数据库中的表空间更名。

1.检查源数据库的表空间是不是“自包含”的
1)以sys用户登陆数据库
sec@ora10g> conn / as sysdba
Connected.

2)使用dbms_tts.transport_set_check对待迁移表空间进行检查,这里待表空间的名字是TBS_SEC_D
sys@ora10g> exec dbms_tts.transport_set_check('TBS_SEC_D',true);

PL/SQL procedure successfully completed.

3)经过transport_set_violations视图查看是否有违反“自包含”的内容,这里显示结果是没有,因此能够对完成TBS_SEC_D表空间的迁移
sys@ora10g> select * from transport_set_violations;

no rows selected

简单列一下“非自包含”的四种可能状况以及应对方法:
--假设待迁移的表空间名字只是:TBS_SEC_D
1)【索引】表空间TBS_SEC_D上存在索引,可是这个索引的基表在另一个表空间上(后面的实验将会演示违反这种约束的状况);
2)【LOB】表存储在表空间TBS_SEC_D上,可是表上的LOB字段存储在其余表空间上;
3)【约束】表的约束有的在表空间TBS_SEC_D上,可是其余的约束在另外的表空间上;
4)【分区表】分区表的一些分区在表空间TBS_SEC_D上,可是其余的其余的分区在另外的表空间上。

若是违反上述的条件,单独想要导出表空间TBS_SEC_D是不行的,处理方法:
第一种处理方法:连带相关的表空间一块儿导出
第二种处理方法:预处理那些不在一块儿的表空间数据到TBS_SEC_D上,而后就能够导出表空间TBS_SEC_D

2.将待导出的表空间TBS_SEC_D修改成“只读”——————这一步很关键
sys@ora10g> alter tablespace TBS_SEC_D read only;

Tablespace altered.

3.SYSDBA权限导出表空间
ora10g@testdb183 /exp$ exp "'"/ as sysdba"'" file=exp_TBS.dmp log=exp_TBS.log transport_tablespace=y tablespaces=TBS_SEC_D triggers=y constraints=n grants=n

Export: Release 10.2.0.3.0 - Production on Tue Aug 25 19:54:22 2009

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP and Data Mining Scoring Engine options
Export done in AL32UTF8 character set and UTF8 NCHAR character set
Note: table data (rows) will not be exported
Note: grants on tables/views/sequences/roles will not be exported
Note: constraints on tables will not be exported
About to export transportable tablespace metadata...
For tablespace TBS_SEC_D ...
. exporting cluster definitions
. exporting table definitions
. . exporting table                           TEST
. exporting triggers
. end transportable tablespace metadata export
Export terminated successfully without warnings.

OK,导出成功。
表空间导出主要是transport_tablespace=y这个参数在起做用,看提示信息,这里导出的exp_TBS.dmp文件中是不包含对象数据的,仅包含表空间的“元数据”,真正的数据还在表空间对应的物理数据文件上,所以使用表空间传输技术完成导入时须要的不单单是这个exp_TBS.dmp导出文件,还须要表空间对应的数据文件。

4.不要着急将表空间TBS_SEC_D恢复为“读写”状态,须要先将导出的exp_TBS.dmp文件和组成表空间的物理数据文件发送到须要导入的secooler数据库服务器上
这里须要注意的是:要以二进制(bin)的模式传输数据。
我习惯于使用scp命令完成数据文件的传输。
最好将数据文件放置到目标数据库数据文件存放的目录,以便统一进行管理。

5.OK,传输完成后,如今能够将表空间TBS_SEC_D恢复为“读写”状态了
sys@ora10g> alter tablespace TBS_SEC_D read write;

Tablespace altered.

6.在目标数据库(secooler数据库实例)中导入表空间
secooler@dbserver /imp$ imp "'"/ as sysdba"'" file='/imp/exp_TBS.dmp' transport_tablespace=y datafiles='/imp/tbs_sec_d01.dbf' tablespaces=TBS_SEC_D tts_owners=sec fromuser=sec touser=sec

Import: Release 10.2.0.3.0 - Production on Tue Aug 25 21:27:37 2009

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP and Data Mining Scoring Engine options

Export file created by EXPORT:V10.02.01 via conventional path
About to import transportable tablespace(s) metadata...
import done in AL32UTF8 character set and UTF8 NCHAR character set
. importing SYS's objects into SYS
. importing SYS's objects into SYS
. importing SEC's objects into SEC
. . importing table                         "TEST"
. importing SYS's objects into SYS
Import terminated successfully without warnings.
secooler@dbserver /imp$


7.经过登录到sec用户中查询数据库对象,验证数据已经成功导入。


8.将表空间置为可读写状态,完成整个表空间的迁移任务。
sec@secooler> select TABLESPACE_NAME,STATUS from dba_tablespaces where TABLESPACE_NAME='TBS_SEC_D';

TABLESPACE_NAME                STATUS
------------------------------ ---------
TBS_SEC_D                      READ ONLY

sec@secooler> alter tablespace SEC_D read write;

Tablespace altered.

sec@secooler> select TABLESPACE_NAME,STATUS from dba_tablespaces where TABLESPACE_NAME='TBS_SEC_D';

TABLESPACE_NAME                STATUS
------------------------------ ---------
TBS_SEC_D                      ONLINE


【实验补充ing
【模拟违反“自包含”第一条原则过程】

sec@ora10g> create table t (x number) tablespace USERS;

Table created.

sec@ora10g> create index t_idx on t(x) tablespace TBS_SEC_D;

Index created.

sec@ora10g> conn / as sysdba
Connected.
sys@ora10g> exec dbms_tts.transport_set_check('USERS',true);

PL/SQL procedure successfully completed.

sys@ora10g> select * from transport_set_violations;

no rows selected

sys@ora10g> exec dbms_tts.transport_set_check('USERS',true);

PL/SQL procedure successfully completed.

sys@ora10g> select * from transport_set_violations;

no rows selected

sys@ora10g> exec dbms_tts.transport_set_check('TBS_SEC_D',true);

PL/SQL procedure successfully completed.

sys@ora10g> select * from transport_set_violations;

VIOLATIONS
------------------------------------------------
Index SEC.T_IDX in tablespace TBS_SEC_D points to table SEC.T in tablespace USERS

TBS_SEC_D,USERS两个表空间同时导出不会有问题:
ora10g@testdb183 /exp$ exp "'"/ as sysdba"'" file=exp_TBS.dmp log=exp_TBS.log transport_tablespace=y tablespaces=TBS_SEC_D,USERS triggers=y constraints=n grants=n

Export: Release 10.2.0.3.0 - Production on Tue Aug 25 19:40:09 2009

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP and Data Mining Scoring Engine options
Export done in AL32UTF8 character set and UTF8 NCHAR character set
Note: table data (rows) will not be exported
Note: grants on tables/views/sequences/roles will not be exported
Note: constraints on tables will not be exported
About to export transportable tablespace metadata...
For tablespace TBS_SEC_D ...
. exporting cluster definitions
. exporting table definitions
. . exporting table                           TEST
For tablespace USERS ...
. exporting cluster definitions
. exporting table definitions
. . exporting table                              T
. exporting triggers
. end transportable tablespace metadata export
Export terminated successfully without warnings.

单独将USERS表空间同时导出也不会有问题:
ora10g@testdb183 /exp$ exp "'"/ as sysdba"'" file=exp_TBS.dmp log=exp_TBS.log transport_tablespace=y tablespaces=USERS triggers=y constraints=n grants=n

Export: Release 10.2.0.3.0 - Production on Tue Aug 25 19:40:19 2009

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP and Data Mining Scoring Engine options
Export done in AL32UTF8 character set and UTF8 NCHAR character set
Note: table data (rows) will not be exported
Note: grants on tables/views/sequences/roles will not be exported
Note: constraints on tables will not be exported
About to export transportable tablespace metadata...
For tablespace USERS ...
. exporting cluster definitions
. exporting table definitions
. . exporting table                              T
. exporting triggers
. end transportable tablespace metadata export
Export terminated successfully without warnings.

可是,单独将TBS_SEC_D表空间同时就会报错,由于违反了一下原则:
【索引】表空间TBS_SEC_D上存在索引,可是这个索引的基表在另一个表空间上(后面的实验将会演示违反这种约束的状况)
ora10g@testdb183 /exp$ exp "'"/ as sysdba"'" file=exp_TBS.dmp log=exp_TBS.log transport_tablespace=y tablespaces=TBS_SEC_D triggers=y constraints=n grants=n

Export: Release 10.2.0.3.0 - Production on Tue Aug 25 19:40:25 2009

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP and Data Mining Scoring Engine options
Export done in AL32UTF8 character set and UTF8 NCHAR character set
Note: table data (rows) will not be exported
Note: grants on tables/views/sequences/roles will not be exported
Note: constraints on tables will not be exported
About to export transportable tablespace metadata...
EXP-00008: ORACLE error 29341 encountered
ORA-29341: The transportable set is not self-contained
ORA-06512: at "SYS.DBMS_PLUGTS", line 1387
ORA-06512: at line 1
EXP-00000: Export terminated unsuccessfully

======================================================================
【注意】不相同的数据库字符集和国家字符集是不能完成表空间迁移的!报错以下,要多加注意。
bomsdb1@testdb183 /imp$ imp "'"/ as sysdba"'" file='/imp/exp_TBS.dmp' transport_tablespace=y datafiles='/imp/tbs_sec_d01.dbf' tablespaces=TBS_SEC_D tts_owners=sec fromuser=sec touser=sec

Import: Release 10.2.0.3.0 - Production on Tue Aug 25 20:18:10 2009

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP and Data Mining Scoring Engine options

Export file created by EXPORT:V10.02.01 via conventional path
About to import transportable tablespace(s) metadata...
import done in AL32UTF8 character set and AL16UTF16 NCHAR character set
import server uses WE8ISO8859P1 character set (possible charset conversion)
export server uses UTF8 NCHAR character set (possible ncharset conversion)
IMP-00017: following statement failed with ORACLE error 29345:
 "BEGIN   sys.dbms_plugts.beginImport ('10.2.0.3.0',873,'871',13,'Linux 64-bi"
 "t for AMD',12006,39801,1,0,0,0); END;"
IMP-00003: ORACLE error 29345 encountered
ORA-29345: cannot plug a tablespace into a database using an incompatible character set
ORA-06512: at "SYS.DBMS_PLUGTS", line 2386
ORA-06512: at "SYS.DBMS_PLUGTS", line 1946
ORA-06512: at line 1
IMP-00000: Import terminated unsuccessfully


【最后小结】
表空间迁移技术能够很是高效的完成数据的迁移任务,所用时间基本等于物理拷贝数据文件的时间。不过有一些具体环境的限制,在真正使用以前,须要进行严格的测试。

将完成表空间迁移过程当中须要注意的事项列一下,若是不全,请你们补充。
【注意事项一】:导入以前,目标数据库中用户必须已经存在存在。
【注意事项二】:导入以前,目标数据库中不能存在同名的表空间,如迁移同名的表空间,须要对迁移以前的源数据库或待迁入数据库中的表空间更名。
【注意事项三】:导出前须要将表空间置为“只读状态”
【注意事项四】:须要以SYSDBA权限完成表空间迁移
【注意事项五】:表空间须要“自包含”,不符合“自包含”的状况以下
1)【索引】表空间TBS_SEC_D上存在索引,可是这个索引的基表在另一个表空间上(后面的实验将会演示违反这种约束的状况);
2)【LOB】表存储在表空间TBS_SEC_D上,可是表上的LOB字段存储在其余表空间上;
3)【约束】表的约束有的在表空间TBS_SEC_D上,可是其余的约束在另外的表空间上;
4)【分区表】分区表的一些分区在表空间TBS_SEC_D上,可是其余的其余的分区在另外的表空间上;

Goodluck.

-- The End --数据库

来自《oracle数据库表空间的导出服务器