您的位置:首页 > 其它

问题2:重新命名或者移动数据文件、日志文件到新的位置

2014-09-05 16:54 537 查看
目的:在某些情况下,数据文件或者日志文件必须被重新命名或迁移到新的位置上,然而想要让这些文件仍然有效,那么就需要为这些文件定义一个物理的名字或者物理位置,例如,在以下情况发生时,就需要重新命名数据文件或者日志文件:

1.出于性能或者维护原因,需要将一个数据库文件移动到一个不同的磁盘上

2.从备份中恢复一个数据文件,但是这个以前存放数据文件的磁盘已经崩溃,那么就需要将这个数据文件重新分配到一个新的磁盘上

3.在操作系统级别,已经移动或者重命名一个数据文件,但是在数据库层即数据库内却没有进行重命名,在数据库启动的过程中得到 ORA-01157 和 ORA-01110 报错信息。如果数据库处于打开状态下,在操作系统级别重命名了数据文件或者移动了数据文件到其他的位置,当通过normal 或者 immediate 方式关闭数据库的时候,会得到 ORA-01116 和 ORA-01110 报错;

以下将通过三个方面来对本主题进行相关的讨论:

I 数据库打开的状态下,重命名或者移动数据文件

II 数据库关闭的状态下,重命名或者移动数据文件

III 重命名或者移动日志文件

【注意】

如果要重命名或者移动的数据文件是 SYSTEM 表空间中的文件,那么,必须用第 II 种方法来达到最终的目的即“数据库关闭的状态下,重命名或者移动数据文件”,因为在数据库打开的状态下,不能够让 SYSTEM 表空间出于 OFFLINE 即离线的状态;

下面分别来对不同的情景进行说明:

I 数据库打开的状态下,重命名或者移动数据文件

在数据库出于打开的状态下,可以对数据文件进行重新命名或者移动,但是,表空间必须被置于READ-ONLY状态。在 READ-ONLY 状态下,用户可以对这个表空间进行SELECT操作,但不能对表空间进行INSERTS UPDATES 和 DELETES 操作。表空间READ-ONLY状态需要的总时间,取决于数据文件的大小和将一个数据文件从一个位置拷贝到一个新位置的时间消耗

将表空间置于READ-ONLY状态,冻结文件头,从而阻止数据文件头被更新。只有在数据文件处于 READ-ONLY状态下,才有可能在数据库出于打开的状态下,对文件进行拷贝。

通过以下的步骤来达到重命名或者移动数据文件的目的,本例中,以 USERS 表空间为数据文件所在的表空间:

1. 查看 USERS表空间中有多少数据文件

SQL>select file_name, status from dba_data_files where tablespace_name = 'USERS';

FILE_NAME STATUS

------------------------------------------------------

/u01/oradata/radius/users01.dbf AVAILABLE

2. 确保,在返回的查询结果中,所有数据文件的状态即STATUS的值都是 AVAILABLE

3. 将 USERS表空间置为 READ ONLY状态

altertablespace users read only;

4. 确保 USERS表空间在数据字典中已经被置于 READ ONLY状态

SQL>select tablespace_name, status from dba_tablespaces where tablespace_name ='USERS';

TABLESPACE_NAME STATUS

---------------------------------------

USERS READ ONLY

5. 使用操作系统命令,将数据文件拷贝到一个新的位置。当数据文件被拷贝到一个新的位置之后,需要做的也是最重要的就是要比较两个数据文件的大小是否一致,因为可能在复制的过程中出现数据丢失的情况

【注意】

同样,可以利用上面的方法,进行数据文件的重命名,而不是改变数据文件的位置。

然而,在 Windows 上可能会出现类似于以下的报错信息:

"Cannotrename USERS01: It is being used by another person or program.

Closeany programs that might be using the file and try again."

以上的报错不是真正的限制,因为,这个过程的目的是移动数据文件到新的位置上,以便进行存储的管理,在上面只是重命名文件名没有生效,这个操作可以在停机的时间进行操作。

6. 当数据文件已经拷贝到新的位置之后,将USERS表空间置于 OFFLINE状态

altertablespace users offline;

7. 当表空间被置于 OFFLINE状态时,需要做的是在数据库层重新命名数据文件到新的位置上,这个操作会更新控制文件中的数据文件的位置信息

alter database rename file '/u01/oradata/radius/users01.dbf' to '/u01/newlocation/users01.dbf';

【注意】

需要对这个表空间中的所有的文件进行同样的操作;同样,可以使用 alter tablespace... rename datafile 语法来达到同样的目的

8. 当确认所有的 rename操作完成之后,将 USERS表空间上线即
ONLINE

alter tablespace users online;

9. 当将表空间重新置为 ONLINE状态,需要将表空间重新置为 READ WRITE即读写状态

alter tablespace users read write;

10. 检查表空间的状态是否为READ WRITE状态

SQL>select tablespace_name, status from dba_tablespaces where tablespace_name ='USERS';

TABLESPACE_NAME STATUS

---------------------------------------

USERS ONLINE

11. 查看修改后的数据文件的位置

SQL>select file_name, status from dba_data_files where tablespace_name = 'USERS';

FILE_NAME STATUS

------------------------------------------------------

/u01/newlocation/users01.dbf AVAILABLE

12. 同样,可以用下面的方式来验证控制文件是否已经被更新

1)将控制文件 TRACE 出来即通过这个操作产生一个控制文件的拷贝

alter database backup controlfile to trace;

2)trace 出来的文件 在 user_dump_dest 目录下,进行查看

/************************************************************************************/

[oracle@10gr2udump]$ cat radius_ora_3672.trc

/u01/app/oracle/admin/radius/udump/radius_ora_3672.trc

OracleDatabase 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production

Withthe Partitioning, OLAP, Data Mining and Real Application Testing options

ORACLE_HOME= /u01/app/oracle/product/10.2.0/db_1

Systemname: Linux

Nodename: 10gr2

Release: 2.6.18-371.el5

Version: #1 SMP Tue Oct 1 08:35:08 EDT 2013

Machine: x86_64

Instancename: radius

Redothread mounted by this instance: 1

Oracleprocess number: 21

Unixprocess pid: 3672, image: oracle@10gr2 (TNS V1-V3)

***ACTION NAME:() 2014-09-05 01:52:51.673

***MODULE NAME:(sqlplus@10gr2 (TNS V1-V3)) 2014-09-05 01:52:51.673

***SERVICE NAME:(SYS$USERS) 2014-09-05 01:52:51.673

***SESSION ID:(148.285) 2014-09-05 01:52:51.673

***2014-09-05 01:52:51.673

--The following are current System-scope REDO Log Archival related

--parameters and can be included in the database initialization file.

--

--LOG_ARCHIVE_DEST=''

--LOG_ARCHIVE_DUPLEX_DEST=''

--

--LOG_ARCHIVE_FORMAT=%t_%s_%r.arc

--

--DB_UNIQUE_NAME="radius"

--

--LOG_ARCHIVE_CONFIG='SEND, RECEIVE, NODG_CONFIG'

--LOG_ARCHIVE_MAX_PROCESSES=2

--STANDBY_FILE_MANAGEMENT=MANUAL

--STANDBY_ARCHIVE_DEST=?/dbs/arch

--FAL_CLIENT=''

--FAL_SERVER=''

--

--LOG_ARCHIVE_DEST_1='LOCATION=/u01/backup/'

--LOG_ARCHIVE_DEST_1='OPTIONAL REOPEN=300 NODELAY'

--LOG_ARCHIVE_DEST_1='ARCH NOAFFIRM NOEXPEDITE NOVERIFY SYNC'

--LOG_ARCHIVE_DEST_1='REGISTER NOALTERNATE NODEPENDENCY'

--LOG_ARCHIVE_DEST_1='NOMAX_FAILURE NOQUOTA_SIZE NOQUOTA_USED NODB_UNIQUE_NAME'

--LOG_ARCHIVE_DEST_1='VALID_FOR=(PRIMARY_ROLE,ONLINE_LOGFILES)'

--LOG_ARCHIVE_DEST_STATE_1=ENABLE

--

--Below are two sets of SQL statements, each of which creates a new

--control file and uses it to open the database. The first set opens

--the database with the NORESETLOGS option and should be used only if

--the current versions of all online logs are available. The second

--set opens the database with the RESETLOGS option and should be used

--if online logs are unavailable.

--The appropriate set of statements can be copied from the trace into

--a script file, edited as necessary, and executed when there is a

--need to re-create the control file.

--

-- Set #1. NORESETLOGS case

--

--The following commands will create a new control file and use it

--to open the database.

--Data used by Recovery Manager will be lost.

--Additional logs may be required for media recovery of offline

--Use this only if the current versions of all online logs are

--available.

--After mounting the created controlfile, the following SQL

--statement will place the database in the appropriate

--protection mode:

-- ALTER DATABASE SET STANDBY DATABASE TOMAXIMIZE PERFORMANCE

STARTUPNOMOUNT

CREATECONTROLFILE REUSE DATABASE "RADIUS" NORESETLOGS ARCHIVELOG

MAXLOGFILES 16

MAXLOGMEMBERS 3

MAXDATAFILES 100

MAXINSTANCES 8

MAXLOGHISTORY 292

LOGFILE

GROUP 1 '/u01/oradata/radius/redo01.log' SIZE 50M,

GROUP 2 '/u01/oradata/radius/redo02.log' SIZE 50M,

GROUP 3 '/u01/oradata/radius/redo03.log' SIZE 50M

--STANDBY LOGFILE

DATAFILE

'/u01/oradata/radius/system01.dbf',

'/u01/oradata/radius/undotbs01.dbf',

'/u01/oradata/radius/sysaux01.dbf',

'/u01/newlocation/users01.dbf',

'/u01/oradata/radius/example01.dbf',

CHARACTERSET US7ASCII

;

--Commands to re-create incarnation table

--Below log names MUST be changed to existing filenames on

--disk. Any one log file from each branch can be used to

--re-create incarnation records.

--ALTER DATABASE REGISTER LOGFILE '/u01/backup/1_1_649125521.arc';

--ALTER DATABASE REGISTER LOGFILE '/u01/backup/1_1_857311814.arc';

--Recovery is required if any of the datafiles are restored backups,

--or if the last shutdown was not normal or immediate.

RECOVERDATABASE

--All logs need archiving and a log switch is needed.

ALTERSYSTEM ARCHIVE LOG ALL;

--Database can now be opened normally.

ALTERDATABASE OPEN;

--Commands to add tempfiles to temporary tablespaces.

--Online tempfiles have complete space information.

--Other tempfiles may require adjustment.

ALTERTABLESPACE TEMP ADD TEMPFILE '/u01/oradata/radius/temp01.dbf'

SIZE 20971520 REUSE AUTOEXTEND ON NEXT 655360 MAXSIZE 32767M;

--End of tempfile additions.

--

-- Set #2. RESETLOGS case

--

--The following commands will create a new control file and use it

--to open the database.

--Data used by Recovery Manager will be lost.

--The contents of online logs will be lost and all backups will

--be invalidated. Use this only if online logs are damaged.

--After mounting the created controlfile, the following SQL

--statement will place the database in the appropriate

--protection mode:

-- ALTER DATABASE SET STANDBY DATABASE TOMAXIMIZE PERFORMANCE

STARTUPNOMOUNT

CREATECONTROLFILE REUSE DATABASE "RADIUS" RESETLOGS ARCHIVELOG

MAXLOGFILES 16

MAXLOGMEMBERS 3

MAXDATAFILES 100

MAXINSTANCES 8

MAXLOGHISTORY 292

LOGFILE

GROUP 1 '/u01/oradata/radius/redo01.log' SIZE 50M,

GROUP 2 '/u01/oradata/radius/redo02.log' SIZE 50M,

GROUP 3 '/u01/oradata/radius/redo03.log' SIZE 50M

--STANDBY LOGFILE

DATAFILE

'/u01/oradata/radius/system01.dbf',

'/u01/oradata/radius/undotbs01.dbf',

'/u01/oradata/radius/sysaux01.dbf',

'/u01/newlocation/users01.dbf',

'/u01/oradata/radius/example01.dbf',

CHARACTERSET US7ASCII

;

--Commands to re-create incarnation table

--Below log names MUST be changed to existing filenames on

--disk. Any one log file from each branch can be used to

--re-create incarnation records.

--ALTER DATABASE REGISTER LOGFILE '/u01/backup/1_1_649125521.arc';

--ALTER DATABASE REGISTER LOGFILE '/u01/backup/1_1_857311814.arc';

--Recovery is required if any of the datafiles are restored backups,

--or if the last shutdown was not normal or immediate.

RECOVERDATABASE USING BACKUP CONTROLFILE

--Database can now be opened zeroing the online logs.

ALTERDATABASE OPEN RESETLOGS;

--Commands to add tempfiles to temporary tablespaces.

--Online tempfiles have complete space information.

--Other tempfiles may require adjustment.

ALTERTABLESPACE TEMP ADD TEMPFILE '/u01/oradata/radius/temp01.dbf'

SIZE 20971520 REUSE AUTOEXTEND ON NEXT 655360 MAXSIZE 32767M;

--End of tempfile additions.

--

/************************************************************************************/

同样,可以通过 v$datafile 视图来查看,因为 v$datafile 视图中的信息来自于控制文件

13. 在操作系统级别,在原来的旧的目录中,将旧的数据文件删除

II 数据库关闭的状态下,重命名或者移动数据文件

1. 查看 USERS表空间中,数据文件的位置

SQL>select file_name,tablespace_name,file_id from dba_data_files wheretablespace_name = 'USERS';

FILE_NAME TABLESPACE_NAME FILE_ID

--------------------------------------------------------------------------- -------

/u01/newlocation/users01.dbf USERS 4

2. 关闭数据库

shutdown immediate

3.在操作系统级别将数据文件重命名或者移动到新的位置上,或者移动新的位置上并且重命名

4. 启动数据库到 mount状态

startup mount;

【说明】:这个操作会读取控制文件,但是不会将数据文件 mount

5. 在数据库内重新命名数据文件

alter database rename file '/u01/newlocation/users01.dbf' to '/u01/oradata/radius/users01.dbf';

6. 打开数据库

alter database open;

7. 查询 v$dbfile视图,来确认修改已经成功

SQL>select * from v$dbfile order by 1;

FILE# NAME

---------------------------------------------

1/u01/oradata/radius/system01.dbf

2/u01/oradata/radius/undotbs01.dbf

3/u01/oradata/radius/sysaux01.dbf

4/u01/oradata/radius/users01.dbf

5 /u01/oradata/radius/example01.dbf

8. 在操作系统级别,将旧的数据文件从旧的目录位置中删除

III 重命名或者移动日志文件

1. 关闭数据库

2. 在操作系统级别,将日志文件重命名或者移动到新的位置上,或者移动新的位置上并且重命名

3. 将数据库启动到 mount状态

startup mount

4. 在数据库级别,重命名日志文件

alter database rename file '/full_path_of_old_location/and_redo_log_name' to'/full_path_of_new_location/and_redo_log_name or new_name';

5. 打开数据库

alter database open;

6. 在操作系统级别,将旧的数据文件从旧的目录位置中删除

--END--
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: