问题描述:
如果某个表空间有多个用户,那么我们在迁移的时候如果按用户导出导入的话,那将是一个麻烦费事的活,这个时候我们可以通过transport_tablespace参数进行表空间迁移,达到一次性迁移整个表空间的目的。下面通过一个实验来讲解整个实施过程及相关注意事项。
实验步骤:
1、创建表空间:
SQL> create tablespace test datafile '+DATA' size 10m autoextend on next 1m maxsize 20m;
2、创建用户:
SQL> create user test identified by test default tablespace test temporary tablespace temp quota 10m on test;
3、授予权限:
SQL> grant connect,resource to test;
4、在test用户下建表:
SQL> conn test/test
SQL> create table test(i number) tablespace test;
SQL> begin
2 for i in 1..10000 loop
3 insert into test values (i);
4 end loop;
5 commit;
6 end ;
7 /
PL/SQL procedure successfully completed
5、把test表空间设置为只读:
SQL> conn / as sysdba
SQL> alter tablespace test read only;
SQL> col name for a40
SQL> set linesize 200
SQL> select name,file#,checkpoint_change#,status,enabled from v$datafile where enabled like '%ONLY%';
NAME FILE# CHECKPOINT_CHANGE# STATUS ENABLED
---------------------------------------- ---------- ------------------ ------- ----------
+DATA/rac/datafile/test.478.945596467 6 2950036 ONLINE READ ONLY
6、使用sys用户导出表空间(必须是sys用户)
SQL> host exp transport_tablespace=y tablespaces=test file=/u01/oracle/test.dmp
Export: Release 11.2.0.4.0 - Production on Fri Jun 2 07:40:41 2017
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Username: sys
Password:
EXP-00056: ORACLE error 28009 encountered
ORA-28009: connection as SYS should be as SYSDBA or SYSOPER
Username: sys as sysdba
Password:
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Tes
Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses ZHS16GBK character set (possible charset conversion)
Note: table data (rows) will not be exported
About to export transportable tablespace metadata...
For tablespace test ...
. exporting cluster definitions
. exporting table definitions
. . exporting table TEST
. exporting referential integrity constraints
. exporting triggers
. end transportable tablespace metadata export
Export terminated successfully without warnings.
7、导出完成后,将表空间重新设置为读写:
SQL> alter tablespace test read write;
8、拷贝数据文件:从ASM里边拷贝数据文件到文件系统有以下两种方式:
8.1、使用ASMCMD:
ASMCMD> cd data/rac/datafile
ASMCMD> cp TEST.478.945596467 /u01/grid/test.dbf
copying +data/rac/datafile/TEST.478.945596467 -> /u01/grid/test.dbf
8.2、使用RMAN:
RMAN> copy datafile '+DATA/rac/datafile/TEST.478.945596467' to '/home/oracle/test.dbf';
9、我们需要把导出的元文件和原数据文件一起拷贝至目标库的数据文件目录:
[root@rac1 ~]# scp /u01/grid/test.dbf 192.168.56.20:/u01/oracle/app/oradata/rac/test.dbf
[oracle@rac1 ~]$ scp /u01/oracle/test.dmp 192.168.56.20:/u01/oracle/test.dmp
10、目标端导入恢复
10.1、创建test用户
SQL> create user test identified by test;
10.2、执行表空间导入
[root@racdg ~]# chmod 777 /u01/oracle/app/oradata/rac/test.dbf
[root@racdg ~]# su - oracle
[oracle@racdg ~]$ imp userid=\'sys/oracle as sysdba\' transport_tablespace=y file=/u01/oracle/test.dmp tablespaces=test datafiles='/u01/oracle/app/oradata/rac/test.dbf';
Import: Release 11.2.0.4.0 - Production on Fri Jun 2 10:34:34 2017
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export file created by EXPORT:V11.02.00 via conventional path
About to import transportable tablespace(s) metadata...
import done in US7ASCII character set and AL16UTF16 NCHAR character set
import server uses ZHS16GBK character set (possible charset conversion)
. importing test's objects into SYSTEM
. . importing table "TEST"
Import terminated successfully without warnings.
注:如果这个表空间只有单个用户,那么导入的时候就相对灵活,我们可以把它导入到另外一个用户下:
[oracle@racdg ~]$ imp userid=\'sys/oracle as sysdba\' transport_tablespace=y file=/u01/oracle/test.dmp fromuser=test touser=scott tablespaces=test datafiles='/u01/oracle/app/oradata/rac/test.dbf'
11、查询导入结果
SQL> select count(*) from test.test;
COUNT(*)
----------
10000
SQL> select tablespace_name,file_name from dba_data_files;
TABLESPACE_NAME FILE_NAME
------------------------------ ---------------------------------------------
USERS /u01/oracle/app/oradata/rac/users01.dbf
UNDOTBS1 /u01/oracle/app/oradata/rac/undotbs01.dbf
SYSAUX /u01/oracle/app/oradata/rac/sysaux01.dbf
SYSTEM /u01/oracle/app/oradata/rac/system01.dbf
TEST /u01/oracle/app/oradata/rac/test.dbf
12、修改test用户的默认表空间为test
SQL> alter user test default tablespace test;
User altered.
SQL> select username,default_tablespace from dba_users where username ='TEST';
USERNAME DEFAULT_TABLESPACE
------------------------------ ------------------------------
TEST TEST
13、将表空间设置为读写
SQL> alter tablespace test read write;
至此,表空间的迁移已经完成。
注意事项:
(1)、exp需要使用sys用户;
(2)、注意源库与目标库字符集完全一致;
(3)、目标库用户需要事先创建,包含原库在该表空间下的所有用户;
(4)、如果该表空间只有一个用户,并且需要导入到其他用户下,imp导入时需要使用fromuser,touser进行映射;
(5)、导出的dmp元文件和ora数据文件均需要拷贝至目标库;
(6)、tablespace要进行read only和read write的状态修改;
(7)、导出导入的数据好像是没有包含过程、触发器、视图等对象,只有表数据;
(8)、导入完成之后,需要修改用户的默认表空间。