在Oracle12.1之前的版本中要重命名数据文件或移动数据文件需要关闭数据库或把表空间/数据文件置为offline状态才可以,参考之前总结的Oracle修改数据文件名/移动数据文件。但到了12.1版本,可以直接在数据文件online状态下把数据文件重命名或移动数据文件。
要实现这一功能需要使用ALTER
DATABASE
MOVE
DATAFILE
语句,语法如下
ALTER DATABASE MOVE DATAFILE ( ‘filename‘ | ‘ASM_filename‘ | file_number ) [ TO ( ‘filename‘ | ‘ASM_filename‘ ) ] [ REUSE ] [ KEEP ]
下面在12.2版本测试这一功能
[email protected]>select * from v$version; BANNER CON_ID -------------------------------------------------------------------------------- ---------- Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production 0 PL/SQL Release 12.2.0.1.0 - Production 0 CORE 12.2.0.1.0 Production 0 TNS for Linux: Version 12.2.0.1.0 - Production 0 NLSRTL Version 12.2.0.1.0 - Production 0
1、创建测试表空间及数据文件:
[email protected]>create tablespace t_move datafile ‘/home/oracle/t_move.dbf‘ size 50m ; Tablespace created. [email protected]>col name for a50 [email protected]>select d.name,d.status from v$datafile d,v$tablespace t where t.ts#=d.ts# and t.name=‘T_MOVE‘; NAME STATUS -------------------------------------------------- ------- /home/oracle/t_move.dbf ONLINE
现在数据文件‘/home/oracle/t_move.dbf‘为ONLINE状态
2、执行重命名操作
[email protected]>alter database move datafile ‘/home/oracle/t_move.dbf‘ to ‘/home/oracle/t_move01.dbf‘; Database altered. [email protected]>select d.name,d.status from v$datafile d,v$tablespace t where t.ts#=d.ts# and t.name=‘T_MOVE‘; NAME STATUS -------------------------------------------------- ------- /home/oracle/t_move01.dbf ONLINE [email protected]>!ls -l /home/oracle/t_move01.dbf -rw-r----- 1 oracle oinstall 52436992 Jul 11 16:07 /home/oracle/t_move01.dbf [email protected]>!ls -l /home/oracle/t_move.dbf ls: cannot access /home/oracle/t_move.dbf: No such file or directory
可以看到文件名从t_move.dbf改为了t_move01.dbf,原文件已经不存在了。
3、执行移动目录操作
[email protected]>alter database move datafile ‘/home/oracle/t_move01.dbf‘ to ‘/u01/app/oracle/oradata/ora12c/t_move01.dbf‘; Database altered. [email protected]>!ls -l /u01/app/oracle/oradata/ora12c/t_move01.dbf -rw-r----- 1 oracle oinstall 52436992 Jul 11 16:10 /u01/app/oracle/oradata/ora12c/t_move01.dbf [email protected]>!ls -l /home/oracle/t_move01.dbf ls: cannot access /home/oracle/t_move01.dbf: No such file or directory [email protected]>select d.name,d.status from v$datafile d,v$tablespace t where t.ts#=d.ts# and t.name=‘T_MOVE‘; NAME STATUS -------------------------------------------------- ------- /u01/app/oracle/oradata/ora12c/t_move01.dbf ONLINE
从上面的结果可以看到数据文件从‘/home/oracle‘目录移动到了‘/u01/app/oracle/oradata/ora12c‘目录下了。
4、copy数据文件到目标目录,保留原文件
[email protected]>alter database move datafile ‘/u01/app/oracle/oradata/ora12c/t_move01.dbf‘ to ‘/home/oracle/t_move01.dbf‘ keep; Database altered. [email protected]>select d.name,d.status from v$datafile d,v$tablespace t where t.ts#=d.ts# and t.name=‘T_MOVE‘; NAME STATUS -------------------------------------------------- ------- /home/oracle/t_move01.dbf ONLINE [email protected]>!ls -l /home/oracle/t_move01.dbf -rw-r----- 1 oracle oinstall 52436992 Jul 11 16:15 /home/oracle/t_move01.dbf [email protected]>!ls -l /u01/app/oracle/oradata/ora12c/t_move01.dbf -rw-r----- 1 oracle oinstall 52436992 Jul 11 16:15 /u01/app/oracle/oradata/ora12c/t_move01.dbf
从上面的结果可以看到数据文件改为了/home/oracle/t_move01.dbf,但原来的数据文件仍然被保留。
5、移动数据文件到ASM存储
--文件系统到ASM ALTER DATABASE MOVE DATAFILE ‘/u01/oracle/rbdb1/user1.dbf‘ TO ‘+dgroup_01/data/orcl/datafile/user1.dbf‘; --ASM到ASM ALTER DATABASE MOVE DATAFILE ‘+dgroup_01/data/orcl/datafile/user1.dbf‘ TO ‘+dgroup_02/data/orcl/datafile/user1.dbf‘;
参考:http://docs.oracle.com/database/121/ADMIN/dfiles.htm#ADMIN13837
时间: 2024-10-24 23:18:53