[20160329]表空间与数据文件.txt

简介: [20160329]表空间与数据文件.txt --昨天跟别人聊天,提到招聘DBA,一些dba这些基本的概念不清楚. --表空间可以是一个逻辑的概念,包含多个数据文件.

[20160329]表空间与数据文件.txt

--昨天跟别人聊天,提到招聘DBA,一些dba这些基本的概念不清楚.
--表空间可以是一个逻辑的概念,包含多个数据文件.而一个数据文件仅仅属于一个表空间.

--表空间offline,一般不需要recover 恢复.除非加入immediate 参数.
--而数据文件offline,一定需要恢复,才能online.如果是非归档模式必须在后面加入drop参数(自己曾经对于这存在混乱).
--不要误解后面这个drop不是删除的意思,我以前理解就存在错误.
--而是表示可能无法恢复.

--表空间可以设置为read only,数据文件不行.

--通过例子说明其中的细节:

1.环境:
SCOTT@book> @ &r/ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

CREATE TABLESPACE TEA DATAFILE
  '/mnt/ramdisk/book/tea01.dbf' AUTOEXTEND OFF
LOGGING
ONLINE
EXTENT MANAGEMENT LOCAL AUTOALLOCATE
BLOCKSIZE 8K
SEGMENT SPACE MANAGEMENT AUTO
FLASHBACK ON;

2.表空间offline:
SYS@book> alter tablespace tea offline;
Tablespace altered.

SYS@book> alter tablespace tea online;
Tablespace altered.
-- 不需要恢复。

SYS@book> alter tablespace tea offline immediate;
Tablespace altered.

SYS@book> alter tablespace tea online;
alter tablespace tea online
*
ERROR at line 1:
ORA-01113: file 7 needs media recovery
ORA-01110: data file 7: '/mnt/ramdisk/book/tea01.dbf'

-- 可以发现加入参数immediate,不会发文件检查点,需要恢复。
SYS@book> recover datafile 7;
Media recovery complete.

SYS@book> alter tablespace tea online;
Tablespace altered.

3.数据文件offline:
SYS@book> alter database datafile 7 offline;
Database altered.

SYS@book> alter database datafile 7 online;
alter database datafile 7 online
*
ERROR at line 1:
ORA-01113: file 7 needs media recovery
ORA-01110: data file 7: '/mnt/ramdisk/book/tea01.dbf'
--数据文件offline,要online必须恢复。也就是offline不会发文件检查点。

SYS@book> recover datafile 7;
Media recovery complete.

SYS@book> alter database datafile 7 online;
Database altered.

--我的测试库是设置在归档模式的,加不加drop一样.
SYS@book> archive log list
Database log mode              Archive Mode
Automatic archival             Enabled
Archive destination            /u01/app/oracle/archivelog/book
Oldest online log sequence     16
Next log sequence to archive   18
Current log sequence           18

SYS@book> alter database datafile 7 offline drop;
Database altered.

SYS@book> alter database datafile 7 online;
alter database datafile 7 online
*
ERROR at line 1:
ORA-01113: file 7 needs media recovery
ORA-01110: data file 7: '/mnt/ramdisk/book/tea01.dbf'


SYS@book> recover datafile 7;
Media recovery complete.

SYS@book> alter database datafile 7 online;
Database altered.

4.数据文件offline,再online可以不需要恢复吗?

--只要先表空间offline,或者read only

SYS@book> alter tablespace tea offline;
Tablespace altered.

SYS@book> alter database datafile 7 online;
Database altered.

--注意这个时候不需要恢复,虽然数据文件online了,但是表空间并没有online。

SYS@book> SELECT file#, CHECKPOINT_CHANGE#, CHECKPOINT_TIME,CREATION_CHANGE#  , RESETLOGS_CHANGE#,status, CHECKPOINT_COUNT,fuzzy,name,tablespace_name  FROM v$datafile_header;
FILE# CHECKPOINT_CHANGE# CHECKPOINT_TIME     CREATION_CHANGE# RESETLOGS_CHANGE# STATUS  CHECKPOINT_COUNT FUZ NAME                             TABLESPACE_NAME
----- ------------------ ------------------- ---------------- ----------------- ------- ---------------- --- -------------------------------- ----------------
    1        13227498882 2016-03-29 15:19:56                7       13227286650 ONLINE              1003 YES /mnt/ramdisk/book/system01.dbf   SYSTEM
    2        13227498882 2016-03-29 15:19:56             1834       13227286650 ONLINE               999 YES /mnt/ramdisk/book/sysaux01.dbf   SYSAUX
    3        13227498882 2016-03-29 15:19:56           923328       13227286650 ONLINE               919 YES /mnt/ramdisk/book/undotbs01.dbf  UNDOTBS1
    4        13227498882 2016-03-29 15:19:56            16143       13227286650 ONLINE              1003 YES /mnt/ramdisk/book/users01.dbf    USERS
    5        13227498882 2016-03-29 15:19:56           952916       13227286650 ONLINE               916 YES /mnt/ramdisk/book/example01.dbf  EXAMPLE
    6        13227498882 2016-03-29 15:19:56          1314508       13227286650 ONLINE               932 YES /mnt/ramdisk/book/sugar01.dbf    SUGAR
    7        13227499647 2016-03-29 15:30:21      13227207527       13227286650 ONLINE                41 YES /mnt/ramdisk/book/tea01.dbf      TEA
7 rows selected.

SYS@book> select * from scott.empx where rownum<=1;
select * from scott.empx where rownum<=1
                    *
ERROR at line 1:
ORA-00376: file 7 cannot be read at this time
ORA-01110: data file 7: '/mnt/ramdisk/book/tea01.dbf'

SYS@book> select tablespace_name,status from dba_tablespaces where tablespace_name in ('TEA','SYSTEM');
TABLESPACE_NAME                STATUS
------------------------------ ---------
SYSTEM                         ONLINE
TEA                            OFFLINE

SYS@book> alter tablespace tea online;
Tablespace altered.

SYS@book> select tablespace_name,status from dba_tablespaces where tablespace_name in ('TEA','SYSTEM');
TABLESPACE_NAME                STATUS
------------------------------ ---------
SYSTEM                         ONLINE
TEA                            ONLINE

SYS@book> alter tablespace tea online;
Tablespace altered.

SYS@book> select * from scott.empx where rownum<=1;
       EMPNO ENAME      JOB                MGR HIREDATE                     SAL         COMM       DEPTNO
------------ ---------- --------- ------------ ------------------- ------------ ------------ ------------
        7369 YYYY       CLERK             7902 1980-12-17 00:00:00          800                        20

--还有1种情况不需要恢复(不过这个是假的),就是在mount模式下online,等open时才需要恢复.实际上还是要恢复。

SYS@book> alter database datafile 7 offline;
Database altered.

SYS@book> alter database datafile 7 online;
alter database datafile 7 online
*
ERROR at line 1:
ORA-01113: file 7 needs media recovery
ORA-01110: data file 7: '/mnt/ramdisk/book/tea01.dbf'

shutdown immediate

SYS@book> startup mount
ORACLE instance started.
Total System Global Area    634732544 bytes
Fixed Size                    2255792 bytes
Variable Size               197133392 bytes
Database Buffers            427819008 bytes
Redo Buffers                  7524352 bytes
Database mounted.

SYS@book> alter database datafile 7 online;
Database altered.
--可以发现在mount状态下online数据文件不需要恢复。

SYS@book> SELECT file#, CHECKPOINT_CHANGE#, CHECKPOINT_TIME,CREATION_CHANGE#  , RESETLOGS_CHANGE#,status, CHECKPOINT_COUNT,fuzzy,name,tablespace_name  FROM v$datafile_header;
FILE# CHECKPOINT_CHANGE# CHECKPOINT_TIME     CREATION_CHANGE# RESETLOGS_CHANGE# STATUS  CHECKPOINT_COUNT FUZ NAME                             TABLESPACE_NAME
----- ------------------ ------------------- ---------------- ----------------- ------- ---------------- --- -------------------------------- ---------------
    1        13227500957 2016-03-29 15:35:56                7       13227286650 ONLINE              1006 NO  /mnt/ramdisk/book/system01.dbf   SYSTEM
    2        13227500957 2016-03-29 15:35:56             1834       13227286650 ONLINE              1002 NO  /mnt/ramdisk/book/sysaux01.dbf   SYSAUX
    3        13227500957 2016-03-29 15:35:56           923328       13227286650 ONLINE               922 NO  /mnt/ramdisk/book/undotbs01.dbf  UNDOTBS1
    4        13227500957 2016-03-29 15:35:56            16143       13227286650 ONLINE              1006 NO  /mnt/ramdisk/book/users01.dbf    USERS
    5        13227500957 2016-03-29 15:35:56           952916       13227286650 ONLINE               919 NO  /mnt/ramdisk/book/example01.dbf  EXAMPLE
    6        13227500957 2016-03-29 15:35:56          1314508       13227286650 ONLINE               935 NO  /mnt/ramdisk/book/sugar01.dbf    SUGAR
    7        13227500662 2016-03-29 15:35:04      13227207527       13227286650 ONLINE                44 YES /mnt/ramdisk/book/tea01.dbf      TEA
7 rows selected.

SYS@book> alter database open;
alter database open
*
ERROR at line 1:
ORA-01113: file 7 needs media recovery
ORA-01110: data file 7: '/mnt/ramdisk/book/tea01.dbf'

--在open时提示要恢复数据文件7.

SYS@book> alter database datafile 7 offline;
Database altered.

SYS@book> alter database open;
Database altered.

SYS@book> recover datafile 7;
Media recovery complete.

SYS@book> alter database datafile 7 online;
Database altered.

SYS@book> select * from scott.empx where rownum<=1;
       EMPNO ENAME      JOB                MGR HIREDATE                     SAL         COMM       DEPTNO
------------ ---------- --------- ------------ ------------------- ------------ ------------ ------------
        7369 YYYY       CLERK             7902 1980-12-17 00:00:00          800                        20

5.测试表空间read only的情况:

SYS@book> alter tablespace tea read only ;
Tablespace altered.

SYS@book> alter database datafile 7 offline;
Database altered.

SYS@book> alter database datafile 7 online;
Database altered.

SYS@book> update scott.empx set ename ='aaaa' where empno=7369;
update scott.empx set ename ='aaaa' where empno=7369
             *
ERROR at line 1:
ORA-00372: file 7 cannot be modified at this time
ORA-01110: data file 7: '/mnt/ramdisk/book/tea01.dbf'

SYS@book> alter tablespace tea read write ;
Tablespace altered.

--总之,只要概念清晰,上面的操作很好理解。

目录
相关文章
|
Oracle 关系型数据库 数据库
9-4 Oracle管理表空间和数据文件
9-4 Oracle管理表空间和数据文件
123 0
|
XML 数据格式
数据文件
数据文件
71 0
|
Oracle 关系型数据库
Oracle管理表空间和数据文件
Oracle管理表空间和数据文件
203 0
|
Oracle 关系型数据库 数据库
[20171122]恢复数据文件块头5.txt
[20171122]恢复数据文件块头5.txt --//前几天做了恢复数据文件块头,通过备份文件直接取出文件块头,覆盖原来的数据块,然后修复. --//今天测试使用image copy来恢复.
1121 0
|
Oracle 关系型数据库 数据库
[20171114]恢复数据文件块头2.txt
[20171114]恢复数据文件块头2.txt --//曾经写过一篇[20161111]数据库文件头的修复.txt,但是利用大小相似的数据文件头覆盖来恢复,那是属于特种恢复.
1080 0
|
SQL Oracle 关系型数据库
[20170627]使用TSPITR恢复表空间.txt
[20170627]使用TSPITR恢复表空间.txt --//RMAN提供了一种实现所谓TSPITR(Tablespace Point-In-Time Recovery)的技术,通过简单的一个语句,就可以在主库不停库(很吸引人) --//的情况下,利用备份集和连续的归档日志,实现表空间级别的定点恢复。
897 0
|
机器学习/深度学习 SQL 关系型数据库
[20170520]利用undo表空间保护数据.txt
[20170520]利用undo表空间保护数据.txt --//undo表空间是用来记录前映像信息,也用来保证查询时一致性的.上个星期去听一些课,提到不打开归档情况下一些维护技巧, --//就是建立多个redo日志文件,用来保存日志,至少维持3-4天甚至1个星期的日志,这样可以一定程度减少错误以及会查问题.
856 0
|
Oracle 关系型数据库 数据库管理
[20161108]关于数据文件的问题.txt
[20161108]关于数据文件的问题.txt --昨天看了一些数据文件位图问题,今天探究数据文件的其他问题。 1.环境: SCOTT@book> @ &r/ver1 PORT_STRING                    VERSION     ...
866 0