标题: Online Move Datafile in Oracle Database 12c Release 1 (12.1) [打印本页] 作者: jiawang 时间: 4 天前 标题: Online Move Datafile in Oracle Database 12c Release 1 (12.1) Prior to Oracle 12c, moving datafiles has always been an offline task. There were certain techniques you could employ to minimize that downtime, but you couldn't remove it completely. Oracle 12c includes an enhancement to the ALTER DATABASE command to allow datafiles to be moved online.
Basic Syntax
Examples
Pluggable Database (PDB)
Tempfiles
Data Guard
Related articles.
Multitenant : Online Move of Datafiles in CDBs and PDBs
Renaming or Moving Oracle Files
Basic Syntax
The text description of the syntax is shown below, but the syntax diagrams and a full description of the ALTER DATABASE command is available in the documentation here.
ALTER DATABASE MOVE DATAFILE ( 'filename' | 'ASM_filename' | file_number )
[ TO ( 'filename' | 'ASM_filename' ) ]
[ REUSE ] [ KEEP ]
The source file can be specified using the file number or name, while the destination file must be specified by the file name. The REUSE keyword indicates the new file should be created even if it already exists. The KEEP keyword indicates the original copy of the datafile should be retained.
When the source file is an OMF file the KEEP option can not be used. If the destination file is an OMF file, the TO clause can be omitted and the file will be created with an OMF name in the DB_CREATE_FILE_DEST location.
The file number can be queried from the V$DATAFILE and DBA_DATA_FILES views.
SQL> CONN / AS SYSDBA
SQL> SET LINESIZE 100
SQL> COLUMN name FORMAT A70
SQL> SELECT file#, name FROM v$datafile WHERE con_id = 1 ORDER BY file#;
SQL>
Examples
The following example shows a basic file move, specifying both source and destination by name. Notice the original file is no longer present.
SQL> ALTER DATABASE MOVE DATAFILE '/u01/app/oracle/oradata/cdb1/system01.dbf' TO '/tmp/system01.dbf';
Database altered.
SQL>
SQL> SELECT file_id, file_name FROM dba_data_files WHERE file_id = 1;
SQL> host ls -al /u01/app/oracle/oradata/cdb1/CDB1/datafile/o1_mf_system_958zo3ll_.dbf
ls: cannot access /u01/app/oracle/oradata/cdb1/CDB1/datafile/o1_mf_system_958zo3ll_.dbf: No such file or directory
SQL>
Pluggable Database (PDB)
The container database (CDB) can not move files that belong to a pluggable database. The following query displays all the datafiles for the CDB and the PDBs.
SQL> SELECT file#, name FROM v$datafile ORDER BY file#;
SQL>
If we try to move a datafile belonging to a PDB an error is returned.
SQL> ALTER DATABASE MOVE DATAFILE '/u01/app/oracle/oradata/pdb2/system01.dbf' TO '/tmp/system01.dbf' REUSE;
ALTER DATABASE MOVE DATAFILE '/u01/app/oracle/oradata/pdb2/system01.dbf' TO '/tmp/system01.dbf' REUSE
*
ERROR at line 1:
ORA-01516: nonexistent log file, data file, or temporary file "29"
SQL>
If we switch to the PDB container, the datafile can be moved as normal.
SQL> ALTER SESSION SET container=pdb2;
Session altered.
SQL> ALTER DATABASE MOVE DATAFILE '/u01/app/oracle/oradata/pdb2/system01.dbf' TO '/tmp/system01.dbf' REUSE;
Database altered.
SQL>
SQL> SELECT file_id, file_name FROM dba_data_files WHERE file_id = 29;
SQL> ALTER DATABASE MOVE DATAFILE '/u01/app/oracle/oradata/cdb1/temp01.dbf' TO '/tmp/temp01.dbf' REUSE;
ALTER DATABASE MOVE DATAFILE '/u01/app/oracle/oradata/cdb1/temp01.dbf' TO '/tmp/temp01.dbf' REUSE
*
ERROR at line 1:
ORA-01516: nonexistent log file, data file, or temporary file
"/u01/app/oracle/oradata/cdb1/temp01.dbf"
SQL>
That is not major problem as temporary files can be created and removed quite simply.
Data Guard
We connect to the primary database (cdb1), check the datafile location, move it and check again.
sqlplus sys/${SYS_PASSWORD}@cdb1 as sysdba
SQL> SELECT name FROM v$datafile where file# = 1;
NAME
--------------------------------------------------------------------------------
/u01/oradata/CDB1/system01.dbf
SQL> ALTER DATABASE MOVE DATAFILE '/u01/oradata/CDB1/system01.dbf' TO '/tmp/system01.dbf';
Database altered.
SQL> SELECT name FROM v$datafile where file# = 1;
NAME
--------------------------------------------------------------------------------
/tmp/system01.dbf
SQL>
We connect to the standby database (cdb1_stby) and check the location of file 1. We can see it hasn't moved.
sqlplus sys/${SYS_PASSWORD}@cdb1_stby as sysdba
SQL> SELECT name FROM v$datafile where file# = 1;
NAME
--------------------------------------------------------------------------------
/u01/oradata/CDB1_STBY/datafile/o1_mf_system_h4p21n94_.dbf
SQL>
We can move it on the standby database, but we have to turn off the apply process.
dgmgrl sys/${SYS_PASSWORD}@cdb1 <<EOF
EDIT DATABASE 'cdb1_stby' SET STATE='APPLY-OFF';
EXIT;
EOF
We can now move the file on the standby.
sqlplus sys/${SYS_PASSWORD}@cdb1_stby as sysdba
SQL> SELECT name FROM v$datafile where file# = 1;
NAME
--------------------------------------------------------------------------------
/u01/oradata/CDB1_STBY/datafile/o1_mf_system_h4p21n94_.dbf
SQL> ALTER DATABASE MOVE DATAFILE 1 TO '/tmp/system01.dbf';
Database altered.
SQL> SELECT name FROM v$datafile where file# = 1;
NAME
--------------------------------------------------------------------------------
/tmp/system01.dbf
SQL>
Once the move is complete we need to turn on the apply process.
dgmgrl sys/${SYS_PASSWORD}@cdb1 <<EOF
EDIT DATABASE 'cdb1_stby' SET STATE='APPLY-ON';
EXIT;
EOF