五、数据文件
数据文件是数据库中最重要的一个要求,是所有的数据库内容存放的地方
datafile是按照表空间为组织单位,表空间的构成是按照段区块为层次---数据文件的逻辑结构
datafile就是表空间的物理文件。
datafile中的内容是通过dbwr把data buffer cache中的dirty buffer data 写入的
dbwr触发:
ckpt触发,dirty buffer 到一定量的时候,data buffer caceh 的内存空间快用完了
查询空闲内存空间超时,表空间offline/read only,表的drop, truncate 开始执备份表空间的时候
--查看数据文件
SQL> select file# ,name,status,enabled,bytes/1024/1024 M from v$datafile;
FILE# NAME STATUS ENABLED M
---------- -------------------------------------------------- ------- ---------- ----------
1 /u01/app/oracle/oradata/orcl/system01.dbf SYSTEM READ WRITE 490
2 /u01/app/oracle/oradata/orcl/undotbs01.dbf ONLINE READ WRITE 30
3 /u01/app/oracle/oradata/orcl/sysaux01.dbf ONLINE READ WRITE 250
4 /u01/app/oracle/oradata/orcl/users01.dbf ONLINE READ WRITE 5
5 /u01/app/oracle/oradata/orcl/example01.dbf ONLINE READ WRITE 100
6 /u01/app/oracle/oradata/orcl/tbs1_1.dbf ONLINE READ WRITE 100
7 /u01/app/oracle/oradata/orcl/tbs1_2.dbf ONLINE READ WRITE 60
SQL> select tablespace_name,file_name,user_bytes/1024/1024 M from dba_data_files;
TABLESPACE_NAME FILE_NAME M
------------------------------ ------------------------------------------------------- ----------
USERS01 /u01/app/oracle/oradata/orcl/users01.dbf 4.9375
SYSAUX /u01/app/oracle/oradata/orcl/sysaux01.dbf 249.9375
UNDOTBS1 /u01/app/oracle/oradata/orcl/undotbs01.dbf 29.9375
SYSTEM /u01/app/oracle/oradata/orcl/system01.dbf 489.9375
EXAMPLE /u01/app/oracle/oradata/orcl/example01.dbf 99.9375
TBS1 /u01/app/oracle/oradata/orcl/tbs1_1.dbf 99.9375
TBS1 /u01/app/oracle/oradata/orcl/tbs1_2.dbf 59.9375
--数据文件的增加与删除
--为TBS1增加一个数据文件
SQL> alter tablespace tbs1 add datafile '/u01/app/oracle/oradata/orcl/tbs1_3.dbf' size 10m autoextend on;
--还有一种创建表空间时不指定大小,而是重用一个存在的dbf要用resue参数,reuse 是把原有的结构拿来用,里面的数据是用不了的,因为早都没有了。
--删表空间中的数据文件,10g以后的版本才用以下功能,在之前版本的就只能把表空间给干掉。
SQL> alter tablespace tbs1 drop datafile '/u01/app/oracle/oradata/orcl/tbs1_3.dbf';
--有数据的数据文件不能删。第一个不能删,如果要删就把表空间干掉,数据文件中有数据写入的文件也不能删除,如下
SQL> alter tablespace tbs1 drop datafile 7; --7是v$datafile中的file#.
alter tablespace tbs1 drop datafile 7
*
ERROR at line 1:
ORA-03262: the file is non-empty
数据文件的日志记录
只有temp表空间是nologing 别的都是logging的
日志记录的几种模式
nologin 并不是不记录日志而是记录的很少
logging 正常记录日志信息
force logging 记录的日志比logging还详细 记录信息非常多 用于DG data guard 容灾的情况
SQL> select tablespace_name ,logging,force_logging from dba_tablespaces;
TABLESPACE_NAME LOGGING FOR
------------------------------ --------- ---
SYSTEM LOGGING NO
UNDOTBS1 LOGGING NO
SYSAUX LOGGING NO
TEMP NOLOGGING NO
USERS01 LOGGING NO
EXAMPLE NOLOGGING NO
TBS1 LOGGING NO
--修改表空间中日志文件记录的方式
SQL> alter tablespace tablespace_name nologging | logging | force logging;
SQL> alter tablespace example logging;
Tablespace altered.
SQL> alter tablespace tbs1 force logging;
Tablespace altered.
SQL> select tablespace_name ,logging,force_logging from dba_tablespaces;
TABLESPACE_NAME LOGGING FOR
------------------------------ --------- ---
SYSTEM LOGGING NO
UNDOTBS1 LOGGING NO
SYSAUX LOGGING NO
TEMP NOLOGGING NO
USERS01 LOGGING NO
EXAMPLE LOGGING NO
TBS1 LOGGING YES
--在非归档模式下更改数据文件位置
如果数据库处于非归档模式下,可以通过如下步骤更改文件路径:
1.关闭数据库
2.系统级进行文件复制
3.启动数据库到mount状态
3.通过SQL修改数据文件位置
4.打开数据库
以下是实际操作的步骤示范:
1.数据库处于非归档模式
SQL> archive log list; Database log mode No Archive Mode Automatic archival Enabled Archive destination /opt/oracle/oradata/conner/archive Oldest online log sequence 150 Current log sequence 153 |
SQL> select name from v$datafile; NAME ------------------------------------------------------------ /opt/oracle/oradata/conner/system01.dbf /opt/oracle/oradata/conner/undotbs01.dbf /opt/oracle/oradata/conner/users01.dbf /opt/oracle/test.dbf |
test02.dbf 是在system表空间里添加的数据文件,用的是alter tablespace system add datafile ' ...' size 10m ;这个语句。 test03.dbf 是在users 表空间里用add 语句添加的数据文件。
SQL> shutdown immediate; Database closed. Database dismounted. ORACLE instance shut down. |
SQL> ! cp /opt/oracle/test.dbf /opt/oracle/oradata/conner/test.dbf |
SQL> startup mount; ORACLE instance started. Total System Global Area 101782828 bytes Fixed Size 451884 bytes Variable Size 37748736 bytes Database Buffers 62914560 bytes Redo Buffers 667648 bytes Database mounted. SQL> select name from v$datafile; NAME ----------------------------------------------------------- /opt/oracle/oradata/conner/system01.dbf /opt/oracle/oradata/conner/undotbs01.dbf /opt/oracle/oradata/conner/users01.dbf /opt/oracle/test.dbf |
SQL> alter database rename file '/opt/oracle/test.dbf' to '/opt/oracle/oradata/conner/test.dbf'; Database altered.
我自己把三个数据文件都用这个语句改变了位置。 SQL> alter database open; Database altered. SQL> select name from v$datafile; NAME ---------------------------------------------------------------- /opt/oracle/oradata/conner/system01.dbf /opt/oracle/oradata/conner/undotbs01.dbf /opt/oracle/oradata/conner/users01.dbf /opt/oracle/oradata/conner/test.dbf SQL> |
我自己的测试,三个文件都能成功。可见,不论是users 还是system表空间。其下的数据文件都可以改变位置。
其实可以采用的方法有多种,本文继续补充几种常用方法:
采用offline的方式
第一步,将表空间offline
alter tablespace tablespace_name offline;
第二步,cp文件到新的目录并rename修改控制文件
第三步,将相应表空间online
当然这种方式同样会影响期间的表空间使用。
以下是简单的示范步骤:
1.将表空间offline
[oracle@jumper oracle]$ sqlplus "/ as sysdba" SQL*Plus: Release 9.2.0.4.0 - Production on Sat Nov 12 18:14:21 2005 Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved. Connected to: Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production With the Partitioning option JServer Release 9.2.0.4.0 - Production SQL> archive log list; Database log mode No Archive Mode Automatic archival Enabled Archive destination /opt/oracle/oradata/conner/archive Oldest online log sequence 6 Current log sequence 9 SQL> select name from v$datafile; NAME --------------------------------------------------------- /opt/oracle/oradata/conner/system01.dbf /opt/oracle/oradata/conner/undotbs01.dbf /opt/oracle/oradata/conner/users01.dbf SQL> alter tablespace users offline; Tablespace altered. |
SQL> ! cp /opt/oracle/oradata/conner/users01.dbf /opt/oracle/oradata/users01.dbf SQL> alter tablespace users rename datafile '/opt/oracle/oradata/conner/users01.dbf' to '/opt/oracle/oradata/users01.dbf'; Tablespace altered. |
SQL> alter tablespace users online; Tablespace altered. SQL> select name from v$datafile; NAME ------------------------------------------------------------------------ /opt/oracle/oradata/conner/system01.dbf /opt/oracle/oradata/conner/undotbs01.dbf /opt/oracle/oradata/users01.dbf |
以上两种方法,可能在要求较高的24x7系统中不适合采用,因为会使表空间长时间不可用。
特别是当表空间数据文件巨大时,物理拷贝可能需要较长的时间。
本文推荐另外一种方法,可以做一个折中,以下是一个简单的步骤说明:
1.将表空间置于只读
只读状态可以使数据仍然可为用户访问.
alter tablespace tablespace_name read only;
2.物理拷贝文件
3.将表空间offline
alter tablespace tablespace_name offline;
4.rename数据文件
alter database rename file 'old_dir_file' to 'new_dir_file';
5.将表空间联机
alter tablespace tablespace_name online;
6.将表空间置于read write模式
alter tablespace tablespace_name read write;
以下是示范步骤:
[oracle@jumper oracle]$ sqlplus "/ as sysdba" SQL*Plus: Release 9.2.0.4.0 - Production on Sat Nov 12 21:10:49 2005 Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved. Connected to: Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production With the Partitioning option JServer Release 9.2.0.4.0 - Production SQL> archive log list; Database log mode Archive Mode Automatic archival Enabled Archive destination /opt/oracle/oradata/conner/archive Oldest online log sequence 7 Next log sequence to archive 10 Current log sequence 10 SQL> select name from v$datafile; NAME -------------------------------------------------------------------------- /opt/oracle/oradata/conner/system01.dbf /opt/oracle/oradata/conner/undotbs01.dbf /opt/oracle/oradata/conner/users01.dbf SQL> alter tablespace users read only; Tablespace altered. |
SQL> ! cp /opt/oracle/oradata/conner/users01.dbf /opt/oracle/oradata/users01.dbf |
SQL> alter tablespace users offline; Tablespace altered. |
SQL> alter database rename file '/opt/oracle/oradata/conner/users01.dbf' to '/opt/oracle/oradata/users01.dbf'; Database altered. |
SQL> alter tablespace users online; Tablespace altered. |
SQL> alter tablespace users read write; Tablespace altered. SQL> select name from v$datafile; NAME -------------------------------------------------------------------------- /opt/oracle/oradata/conner/system01.dbf /opt/oracle/oradata/conner/undotbs01.dbf /opt/oracle/oradata/users01.dbf SQL> |