用dbms_streams_tablespace_adm來表空間的遷移(1)

jolly10發表於2009-10-26

http://yangtingkun.itpub.net/post/468/484859

9i的時候,表空間的遷移使用EXP/IMP來實現。在10g中,除了使用EXP/IMP之外,還可以使用資料泵EXPDP/IMPDP,以及RMAN的命令TRANSPORT TABLESPACE命令,除此之外,還可以透過PL/SQLDBMS_STREAMS_TABLESPACE_ADM來實現。

這一篇介紹PULL_SIMPLE_TABLESPACE過程。

OracleDBMS_STREAMS_TABLESPACE_ADM包提供了8個過程:ATTACH_TABLESPACESATTACH_SIMPLE_TABLESPACECLONE_TABLESPACESCLONE_SIMPLE_TABLESPACEDETACH_TABLESPACESDETACH_SIMPLE_TABLESPACEPULL_TABLESPACESPULL_SIMPLE_TABLESPACE

其中包含了4SIMPLE過程,SIMPLE過程可以用來處理一個表空間,且這個表空間中僅包含一個資料檔案的情況。

ATTACH過程用於將表空間載入到目標資料庫中,這個過程的功能類似IMP匯入。

CLONE過程用於將表空間置於只讀,並匯入源資料,並將表空間對應的資料檔案複製到指定目錄,這個操作包含了EXP的功能。

DETACH過程和CLONE過程功能型別,區別在於DETACH過程在複製完成後,會從源資料庫中刪除表空間。

PULL過程實際上包含了CLONE以及ATTACH兩個過程,其中還包括了DBMS_FILE_TRANSFER包的呼叫,使得在目標資料庫執行一個命令就完成表空間遷移的所有的操作。

使用Oracle提供PL/SQL包,可以與TRANSPORT TABLESPACE命令,以及EXP/IMPEXPDP/IMPDP工具配合。比如透過TRANSPORT TABLESPACE命令,或EXP/EXPDP生成的匯出源資料以及表空間的資料檔案,可以利用ATTACH過程進行匯入。利用CLONE過程或DETACH過程生成的源資料以及資料檔案,也可以透過IMP/IMPDP方式匯入的目標資料庫中。

這裡介紹一個最簡單的例子,由於當前要遷移一個表空間,且表空間僅包含了一個資料檔案,因此這裡選擇了最簡單的PULL_SIMPLE_TABLESPACE過程。

[@more@]源資料庫的情況:

SQL> select * From v$version;

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production


SQL> select property_name,property_value
2 from database_properties
3 where property_name like '%CHARACTERSET%';

PROPERTY_NAME
------------------------------
PROPERTY_VALUE
--------------------------------------------------------------------------------
NLS_CHARACTERSET
WE8ISO8859P1

NLS_NCHAR_CHARACTERSET
AL16UTF16


SQL> SELECT FILE_NAME FROM DBA_DATA_FILES WHERE TABLESPACE_NAME ='NADSPACE';

FILE_NAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/orcl/NADSPACE001.dbf

SQL> SHOW PARAMETER DB_BLOCK_SIZE

NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_block_size integer 8192


SQL> EXEC DBMS_TTS.TRANSPORT_SET_CHECK('NADSPACE',TRUE);

PL/SQL procedure successfully completed.

SQL> select * From transport_set_violations;

no rows selected


檢查目標資料庫:

SQL> SELECT * FROM V$VERSION;

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Prod
PL/SQL Release 10.2.0.4.0 - Production
CORE 10.2.0.4.0 Production
TNS for Linux: Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - Production


SQL> SELECT PROPERTY_NAME, PROPERTY_VALUE
2 FROM DATABASE_PROPERTIES
3 WHERE PROPERTY_NAME LIKE '%CHARACTERSET%';

PROPERTY_NAME
------------------------------
PROPERTY_VALUE
--------------------------------------------------------------------------------
NLS_NCHAR_CHARACTERSET
AL16UTF16

NLS_CHARACTERSET
WE8ISO8859P1

SQL> SHOW PARAMETER DB_BLOCK_SIZE

NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_block_size integer 8192
SQL> SELECT FILE_NAME FROM DBA_DATA_FILES WHERE TABLESPACE_NAME ='NADSPACE';

no rows selected

這裡目標資料庫的版本高於源資料庫,其它的字符集以及DB_BLOCK_SIZE都與源資料庫一致


在源資料庫建立目錄,使得源資料庫可以讀取資料檔案:

SQL> create directory d_datafile as '/u01/app/oracle/oradata/orcl';

Directory created.

SQL> grant read,write on directory d_datafile to system;

Grant succeeded.


在目標資料庫建立目標使用者:


SQL> create user nad identified by nad ;

User created.

SQL> grant connect,resource to nad;

Grant succeeded.


建立到源資料庫的dblink:

SQL> create public database link nad
2 connect to system
3 identified by sys
4 using 'orcl130';

Database link created.

測試一下dblink

SQL> select count(*) from nad.AAA@nad;

COUNT(*)
----------
49835

建立目標資料庫的directory

SQL> create directory d_trans as '/u01/app/oracle/oradata/orcl';

Directory created.

SQL> grant read,write on directory d_trans to system;

Grant succeeded.


SQL> exec dbms_streams_tablespace_adm.pull_simple_tablespace('NADSPACE','NAD','D_TRANS');

PL/SQL procedure successfully completed.

[oracle@rhel131 orcl]$ pwd
/u01/app/oracle/oradata/orcl
[oracle@rhel131 orcl]$ cat NADSPACE001.plg
Starting "SYSTEM"."SYS_IMPORT_TRANSPORTABLE_01":
Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK
Processing object type TRANSPORTABLE_EXPORT/TABLE
Processing object type TRANSPORTABLE_EXPORT/INDEX
Processing object type TRANSPORTABLE_EXPORT/INDEX_STATISTICS
Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK
Job "SYSTEM"."SYS_IMPORT_TRANSPORTABLE_01" successfully completed at 03:02:26


需要特別注意的地方是建立的源資料庫以及目標資料庫的directory必須是要傳輸的datafile的目錄,否則會如下的錯誤:
SQL> exec dbms_streams_tablespace_adm.pull_simple_tablespace('NADSPACE','NAD','D_TRANS');
BEGIN dbms_streams_tablespace_adm.pull_simple_tablespace('NADSPACE','NAD','D_TRANS'); END;

*
ERROR at line 1:
ORA-23609: unable to find directory object for directory
/u01/app/oracle/oradata/orcl/
ORA-06512: at "SYS.DBMS_STREAMS_TBS_INT_INVOK", line 535
ORA-06512: at "SYS.DBMS_STREAMS_TBS_INT_INVOK", line 370
ORA-06512: at "SYS.DBMS_STREAMS_RPC", line 142
ORA-06512: at "SYS.DBMS_STREAMS_TABLESPACE_ADM", line 1949
ORA-06512: at line 1


文件上是這個描述的:
Usage Notes

To run this procedure, a user must meet the following requirements on the remote database:

* Have the EXP_FULL_DATABASE role
* Have execute privilege on the DBMS_STREAMS_TABLESPACE_ADM package
* Have access to at least one data dictionary view that contains information about the tablespaces. These views

include DBA_TABLESPACES and USER_TABLESPACES.
* Have MANAGE TABLESPACE or ALTER TABLESPACE privilege on a tablespace if the tablespace must be made read-only
* Have READ privilege on the directory object for the directory that contains the datafile for the tablespace. The

name of this tablespace is specified by the tablespace_name parameter. If a directory object does not exist for this

directory, then create the directory object and grant the necessary privileges before you run this procedure.

To run this procedure, a user must meet the following requirements on the local database:

* Have the roles IMP_FULL_DATABASE and EXECUTE_CATALOG_ROLE
* Have WRITE privilege on the directory object that will contain the Data Pump export the log file, specified by

the log_file parameter if non-NULL
* Have WRITE privilege on the directory object that will hold the datafile for the tablespace, specified by the

directory_object parameter

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/271283/viewspace-1028140/,如需轉載,請註明出處,否則將追究法律責任。

相關文章