设为首页 收藏本站
查看: 472|回复: 0

[经验分享] 更改Oracle数据文件路径(原创)

[复制链接]

尚未签到

发表于 2016-7-21 12:50:08 | 显示全部楼层 |阅读模式
  近日,群里的朋友经常遇到数据文件需要删除或者改名的问题,这里,笔者进行了下总结,也算是抛砖引玉,希望大家拍砖。
删除数据文件
在10g中,如果是未被Oracle写入数据的数据文件则可以被直接删除,如果是已经被写入数据的数据文件,则无法被直接删除。请见下例
SQL> create tablespace test datafile '/home/oracle/test1.dbf' size 1m;
Tablespace created.
SQL>  create table test tablespace test
  2  as
  3* select * from dba_objects where rownum <=8000
Table created.
SQL> select SEGMENT_NAME,BYTES/1024/1024 Mb from user_segments where SEGMENT_NAME='TEST';
SEGMENT_NAME                 MB                                                                                                                                                    
------------------          ------------                                                                 
TEST                           .875 
SQL> alter tablespace test add datafile '/home/oracle/test2.dbf' size 50m;
Tablespace altered.
SQL> insert into test select * from dba_objects ;
50323 rows created.
SQL> commit;
Commit complete.
SQL> select SEGMENT_NAME,BYTES/1024/1024 from dba_segments where SEGMENT_NAME='TEST'
SEGMENT_NAME         BYTES/1024/1024
-------------------- ---------------
TEST                               7
QL> alter tablespace test add datafile '/u01/app/test3.dbf' size 1m;
Tablespace altered.
SQL> alter tablespace test drop datafile '/u01/app/test3.dbf';
Tablespace altered.
SQL> alter tablespace test drop datafile '/u01/app/test2.dbf';
alter tablespace test drop datafile '/u01/app/test2.dbf'
*
ERROR at line 1:
ORA-03262: the file is non-empty


  可以看到,只有非空的数据文件才能进行删除,已经写入数据的数据文件不能进行删除。
更改数据文件位置
更改数据文件位置,笔者认为有3种方法
1、利用rman的set newname来更改数据文件位置,对应用影响较小。
  2、利用手工备份,然后拷贝数据到指定位置,原理和rman类似。
  3、利用exp导出数据,重建表空间后再倒入数据,笔者认为该方法对应用影响较大,需结合实际情况考虑,这里不做详细介绍。
利用rman的set newname来更改数据文件位置
SQL> select file_name,status,file_id from dba_data_files;
FILE_NAME                                                         STATUS                    FILE_ID
-------------------------------------                ---------------------------        
---------------------------
  /u01/app/oradata/orcl/users01.dbf                 AVAILABLE                  5
/u01/app/oradata/orcl/sysaux01.dbf               AVAILABLE                  2
/u01/app/oradata/orcl/undotbs01.dbf             AVAILABLE                  3
/u01/app/oradata/orcl/system01.dbf               AVAILABLE                  1
/u01/app/oradata/orcl/example01.dbf             AVAILABLE                  4
/home/oracle/test1.dbf                                          AVAILABLE                   6
/home/oracle/test2.dbf                                          AVAILABLE                   7
通过使用rman的set newname命令更改数据文件位置,整个过程如下:
1、由于set newname命令是更改rman repository的信息,并根据set newname的信息restore备份数据到目标路径,故需先对数据文件进行那个备份
RMAN> backup datafile 6,7;
Starting backup at 21-APR-12
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
input datafile fno=00007 name=/u01/app/test2.dbf
input datafile fno=00006 name=/u01/app/test1.dbf
channel ORA_DISK_1: starting piece 1 at 21-APR-12
channel ORA_DISK_1: finished piece 1 at 21-APR-12
piece handle=/u01/app/flash_recovery_area/ORCL/backupset/2012_04_21/o1_mf_nnndf_TAG20120421T150219_7s4pvvmt_.bkp tag=TAG20120421T150219 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 21-APR-12
2、这一步rman并不认为是将数据restore到新路径下,而是认为将数据文件以copy的方式备份到新路径下,官方文档解释如下:
Sets the default name for all subsequent RESTORE or SWITCH commands that affect the specified datafile. If you do not issue this command before the datafile restore operation, then RMAN restores the file to its default location.
After you restore a datafile to a new location, then you can run SWITCH to rename the file in the control file to the NEWNAME. If you do not run SWITCH, then the restored file functions as a datafile copy and is recorded as such in the repository.
RMAN> run{
2> set newname for datafile 6 to '/u01/app/test1.dbf';
3> set newname for datafile 7 to '/u01/app/test2.dbf';
4> restore datafile 6,7;
5>  }


  RMAN> list copy;
specification does not match any archive log in the recovery catalog
List of Datafile Copies
Key     File S Completion Time Ckp SCN    Ckp Time        Name
------- ---- - --------------- ---------- --------------- ----
8       6    A 21-APR-12       832277     21-APR-12      
/u01/app/test1.dbf
9       7    A 21-APR-12       832277     21-APR-12      
/u01/app/test2.dbf
3、将表空间或者数据文件offline

  RMAN>  sql 'alter tablespace test offline';
sql statement: alter tablespace test offline
4、通过switch命令将数据文件信息写入到控制文件中
RMAN> run{  
2> switch datafile all;                   #switch命令必须在run块里面
3> }

5、recover表空间或者数据文件
RMAN> recover datafile 6,7;
Starting recover at 21-APR-12
using channel ORA_DISK_1
starting media recovery
media recovery complete, elapsed time: 00:00:02
Finished recover at 21-APR-12
6、将表空间或者数据文件online
RMAN> sql 'alter tablespace test online';
sql statement: alter tablespace test online

查看数据文件信息,大功告成
SQL> select file_name,status,file_id from dba_data_files;
FILE_NAME                                                         STATUS                    FILE_ID
-------------------------------------                ---------------------------        
---------------------------
  /u01/app/oradata/orcl/users01.dbf                 AVAILABLE                  5
/u01/app/oradata/orcl/sysaux01.dbf               AVAILABLE                  2
/u01/app/oradata/orcl/undotbs01.dbf             AVAILABLE                  3
/u01/app/oradata/orcl/system01.dbf               AVAILABLE                  1
/u01/app/oradata/orcl/example01.dbf             AVAILABLE                  4
/u01/app/test1.dbf                                          AVAILABLE                   6
/u01/app/test2.dbf                                          AVAILABLE                   7
  利用手工备份,然后拷贝数据到指定位置
  1、将表空间至于备份状态SQL> alter tablespace test begin backup;
Tablespace altered.

  2、拷贝数据文件到目标位置
$cp /home/oracle/test*.dbf /u01/app/test*

  3、将表空间至于offline状态
SQL> alter tablespace test end backup;
Tablespace altered.
SQL> alter tablespace test offline;Tablespace altered.
  3、重命名数据文件
  SQL> alter tablespace test rename datafile '/home/oracle/test1.dbf' to
'/u01/app/test1.dbf';
Tablespace altered.

SQL> alter tablespace test rename datafile '/home/oracle/test2.dbf' to
'/u01/app/test2.dbf';
Tablespace altered.

  4、对test表空间应用日志
SQL> recover tablespace test;
Media recovery complete.

  5、将test表空间至于online
SQL> alter tablespace test online;
Tablespace altered.

  6、查看表空间状态
SQL> select file_name from dba_data_files;
FILE_NAME
-----------------------------------------------------------------
/u01/app/oradata/orcl/users01.dbf
/u01/app/oradata/orcl/sysaux01.dbf
/u01/app/oradata/orcl/undotbs01.dbf
/u01/app/oradata/orcl/system01.dbf
/u01/app/oradata/orcl/example01.dbf
/u01/app/test1.dbf
/u01/app/test2.dbf
7 rows selected.
总结:以上介绍了如何更改非空数据文件的路径,笔者认为,rman操作的方式相对复杂,但对系统影响最小。手工备份的方式比较简单,但会产生更多的redo,如果表空间不是特别大的话亦可考虑。至于imp/exp导出的方式,对系统影响最大,个人认为需要结合实际情况分析。

参考至:http://docs.oracle.com/cd/B10500_01/server.920/a96565/rcmsynta50.htm
           http://docs.oracle.com/cd/B10500_01/server.920/a96565/rcmsynta56.htm
           http://www.cnblogs.com/rootq/archive/2010/03/05/1678969.html
本文原创,转载请注明出处、作者
如有错误,欢迎指正
邮箱:czmcj@163.com

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-247447-1-1.html 上篇帖子: 理解Oracle中的监听动态注册 下篇帖子: Oracle 相关的专业术语 说明
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表