read only打开不完全恢复的数据库的限制...

有人说不完全恢复后无法使用read only打开数据库查看恢复结果,必须使用resetlog打开。如果使用resetlog打开,redo会被重置,以前的备份也会失效,也就无法继续恢复了。
其实,不完全恢复是可以用read only打开的,只不过有点限制.




下面来做个实验:
1.建立表mydb.rotest
2.drop mydb.rotest

那么假如我想恢复rotest,就需要做不完全恢复到scn599679。(当然不完全恢复的方法有许多...这里讨论一下用全库的不完全恢复)
Z:\>sqlplus " / as sysdba"

SQL*Plus: Release 10.2.0.4.0 - Production on Thu Feb 26 09:27:54 2009

Copyright (c) 1982, 2007, Oracle.  All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SYS AS SYSDBA> conn mydb
Enter password:
Connected.
MYDB > select current_scn from v$database;

CURRENT_SCN
-----------
     599655

Elapsed: 00:00:00.09
MYDB > create table rotest as select * from v$version;

Table created.

Elapsed: 00:00:00.65
MYDB > select current_scn from v$database;

CURRENT_SCN
-----------
     599679

Elapsed: 00:00:00.04
MYDB > drop table rotest;

Table dropped.

Elapsed: 00:00:01.51
MYDB > select current_scn from v$database;

CURRENT_SCN
-----------
     599698

Elapsed: 00:00:00.01
SYS AS SYSDBA> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SYS AS SYSDBA> startup mount
ORACLE instance started.

Total System Global Area  276824064 bytes
Fixed Size                  1296260 bytes
Variable Size              92276860 bytes
Database Buffers          180355072 bytes
Redo Buffers                2895872 bytes
Database mounted.
SYS AS SYSDBA>
SYS AS SYSDBA> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

方法一:直接不完全恢复Z:\>rman target /

Recovery Manager: Release 10.2.0.4.0 - Production on Thu Feb 26 09:35:36 2009

Copyright (c) 1982, 2007, Oracle.  All rights reserved.

connected to target database: TEST (DBID=1977886605, not open)

RMAN> list backup;

using target database control file instead of recovery catalog

List of Backup Sets
===================

BS Key  Size       Device Type Elapsed Time Completion Time
------- ---------- ----------- ------------ ---------------
1       26.92M     DISK        00:00:02     26-FEB-09
        BP Key: 1   Status: AVAILABLE  Compressed: NO  Tag: TAG20090226T092546
        Piece Name: D:\ORACLE\ORA102\DATABASE\01K8ANKR_1_1

  List of Archived Logs in backup set 1
  Thrd Seq     Low SCN    Low Time  Next SCN   Next Time
  ---- ------- ---------- --------- ---------- ---------
  1    9       138764     25-FEB-09 599586     26-FEB-09

BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
2       Full    249.92M    DISK        00:00:28     26-FEB-09
        BP Key: 2   Status: AVAILABLE  Compressed: NO  Tag: TAG20090226T092551
        Piece Name: D:\ORACLE\ORA102\DATABASE\02K8ANKV_1_1
  List of Datafiles in backup set 2
  File LV Type Ckp SCN    Ckp Time  Name
  ---- -- ---- ---------- --------- ----
  1       Full 599594     26-FEB-09 D:\ORACLE\ORADATA\TEST\SYSTEM01.DBF
  2       Full 599594     26-FEB-09 D:\ORACLE\ORADATA\TEST\UNDOTBS01.DBF
  3       Full 599594     26-FEB-09 D:\ORACLE\ORADATA\TEST\SYSAUX01.DBF
  4       Full 599594     26-FEB-09 D:\ORACLE\ORADATA\TEST\USERS01.DBF

BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
3       Full    6.80M      DISK        00:00:02     26-FEB-09
        BP Key: 3   Status: AVAILABLE  Compressed: NO  Tag: TAG20090226T092551
        Piece Name: D:\ORACLE\ORA102\DATABASE\03K8ANM3_1_1
  Control File Included: Ckp SCN: 599605       Ckp time: 26-FEB-09
  SPFILE Included: Modification time: 26-FEB-09

BS Key  Size       Device Type Elapsed Time Completion Time
------- ---------- ----------- ------------ ---------------
4       4.00K      DISK        00:00:02     26-FEB-09
        BP Key: 4   Status: AVAILABLE  Compressed: NO  Tag: TAG20090226T092630
        Piece Name: D:\ORACLE\ORA102\DATABASE\04K8ANM6_1_1

  List of Archived Logs in backup set 4
  Thrd Seq     Low SCN    Low Time  Next SCN   Next Time
  ---- ------- ---------- --------- ---------- ---------
  1    10      599586     26-FEB-09 599610     26-FEB-09

RMAN> run {
2> set until scn 599679;
3> restore database;
4> recover database;
5> }

executing command: SET until clause

Starting restore at 26-FEB-09
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=156 devtype=DISK

channel ORA_DISK_1: starting datafile backupset restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
restoring datafile 00001 to D:\ORACLE\ORADATA\TEST\SYSTEM01.DBF
restoring datafile 00002 to D:\ORACLE\ORADATA\TEST\UNDOTBS01.DBF
restoring datafile 00003 to D:\ORACLE\ORADATA\TEST\SYSAUX01.DBF
restoring datafile 00004 to D:\ORACLE\ORADATA\TEST\USERS01.DBF
channel ORA_DISK_1: reading from backup piece D:\ORACLE\ORA102\DATABASE\02K8ANKV_1_1
channel ORA_DISK_1: restored backup piece 1
piece handle=D:\ORACLE\ORA102\DATABASE\02K8ANKV_1_1 tag=TAG20090226T092551
channel ORA_DISK_1: restore complete, elapsed time: 00:00:36
Finished restore at 26-FEB-09

Starting recover at 26-FEB-09
using channel ORA_DISK_1

starting media recovery
media recovery complete, elapsed time: 00:00:04

Finished recover at 26-FEB-09

RMAN> exit


Recovery Manager complete.

Z:\>sqlplus " / as sysdba"

SQL*Plus: Release 10.2.0.4.0 - Production on Thu Feb 26 09:37:25 2009

Copyright (c) 1982, 2007, Oracle.  All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SYS AS SYSDBA> alter database open read only;
alter database open read only
*
ERROR at line 1:
ORA-16005: database requires recovery


Elapsed: 00:00:00.18

这时候发现无法readonly打开数据库,为什么呢?
SYS AS SYSDBA> select checkpoint_change#,last_change# from v$datafile;

CHECKPOINT_CHANGE# LAST_CHANGE#
------------------ ------------
            599999       599999
            599999       599999
            599999       599999
            599999       599999

Elapsed: 00:00:00.01
SYS AS SYSDBA> select checkpoint_change# from v$datafile_header;

CHECKPOINT_CHANGE#
------------------
            599685
            599685
            599685
            599685

Elapsed: 00:00:00.14
SYS AS SYSDBA> select checkpoint_change# ,current_scn from v$database;

CHECKPOINT_CHANGE# CURRENT_SCN
------------------ -----------
            599999           0

Elapsed: 00:00:00.00

可以看到controlfile中记录的数据文件checkpoint_change#和数据文件头的checkpoint_change#无法对应,这是因为使用的是原controlfile,而数据文件是通过备份恢复出来的,当你做read only open的时候,怀疑oracle为了冻结数据文件的scn会对比这两个值,当他们不相同的时候,oracle会认为这个不不一致的数据库,所以无法read only opn.

既然是因为这个,我们重新作恢复,只不过这次用备份的controlfile.

SYS AS SYSDBA> startup force nomount
ORACLE instance started.

Total System Global Area  276824064 bytes
Fixed Size                  1296260 bytes
Variable Size              92276860 bytes
Database Buffers          180355072 bytes
Redo Buffers                2895872 bytes
SYS AS SYSDBA> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

Z:\>rman target /

Recovery Manager: Release 10.2.0.4.0 - Production on Thu Feb 26 09:51:00 2009

Copyright (c) 1982, 2007, Oracle.  All rights reserved.

connected to target database: TEST (not mounted)

RMAN> run {
2> restore controlfile from 'D:\ORACLE\ORA102\DATABASE\03K8ANM3_1_1';
3> mount database;
4> set until scn 599679;
5> restore database;
6> recover database;
7> }

Starting restore at 26-FEB-09
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=156 devtype=DISK

channel ORA_DISK_1: restoring control file
channel ORA_DISK_1: restore complete, elapsed time: 00:00:03
output filename=D:\ORACLE\ORADATA\TEST\CONTROL01.CTL
output filename=D:\ORACLE\ORADATA\TEST\CONTROL02.CTL
output filename=D:\ORACLE\ORADATA\TEST\CONTROL03.CTL
Finished restore at 26-FEB-09

database mounted
released channel: ORA_DISK_1

executing command: SET until clause

Starting restore at 26-FEB-09
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=156 devtype=DISK

channel ORA_DISK_1: starting datafile backupset restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
restoring datafile 00001 to D:\ORACLE\ORADATA\TEST\SYSTEM01.DBF
restoring datafile 00002 to D:\ORACLE\ORADATA\TEST\UNDOTBS01.DBF
restoring datafile 00003 to D:\ORACLE\ORADATA\TEST\SYSAUX01.DBF
restoring datafile 00004 to D:\ORACLE\ORADATA\TEST\USERS01.DBF
channel ORA_DISK_1: reading from backup piece D:\ORACLE\ORA102\DATABASE\02K8ANKV_1_1
channel ORA_DISK_1: restored backup piece 1
piece handle=D:\ORACLE\ORA102\DATABASE\02K8ANKV_1_1 tag=TAG20090226T092551
channel ORA_DISK_1: restore complete, elapsed time: 00:01:05
Finished restore at 26-FEB-09

Starting recover at 26-FEB-09
using channel ORA_DISK_1

starting media recovery

archive log thread 1 sequence 10 is already on disk as file D:\ORACLE\ORADATA\TEST\REDO01.LOG
archive log thread 1 sequence 11 is already on disk as file D:\ORACLE\ORADATA\TEST\REDO02.LOG
archive log filename=D:\ORACLE\ORADATA\TEST\REDO01.LOG thread=1 sequence=10
archive log filename=D:\ORACLE\ORADATA\TEST\REDO02.LOG thread=1 sequence=11
media recovery complete, elapsed time: 00:00:03
Finished recover at 26-FEB-09

RMAN> exit


Recovery Manager complete.

Z:\>sqlplus " / as sysdba"

SQL*Plus: Release 10.2.0.4.0 - Production on Thu Feb 26 09:52:47 2009

Copyright (c) 1982, 2007, Oracle.  All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SYS AS SYSDBA> select checkpoint_change#,last_change# from v$datafile;

CHECKPOINT_CHANGE# LAST_CHANGE#
------------------ ------------
            599685
            599685
            599685
            599685

Elapsed: 00:00:00.04
SYS AS SYSDBA> select checkpoint_change# from v$datafile_header;

CHECKPOINT_CHANGE#
------------------
            599685
            599685
            599685
            599685

Elapsed: 00:00:00.07
SYS AS SYSDBA> select checkpoint_change# ,current_scn from v$database;

CHECKPOINT_CHANGE# CURRENT_SCN
------------------ -----------
            599586           0

Elapsed: 00:00:00.01
SYS AS SYSDBA> alter database open read only;

Database altered.

Elapsed: 00:00:02.32

由于控制文件来自于备份,在做recover的时候,控制文件也一起被recover,所以两个地方存储的checkpoint_change#就一致了,也就可以read only打开了,这个时候,也就可以读取mydb.rotest了
SYS AS SYSDBA> select * from mydb.rotest;

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Prod
PL/SQL Release 10.2.0.4.0 - Production
CORE    10.2.0.4.0      Production
TNS for 32-bit Windows: Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - Production

Elapsed: 00:00:00.09
SYS AS SYSDBA> startup force mount;
ORACLE instance started.

Total System Global Area  276824064 bytes
Fixed Size                  1296260 bytes
Variable Size              92276860 bytes
Database Buffers          180355072 bytes
Redo Buffers                2895872 bytes
Database mounted.
SYS AS SYSDBA> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

下面可以把mydb.rotest exp出来,然后继续作恢复,到结束,这样数据库,就恢复到了最新状态,不会丢数据(虽然必须用resetlogs打开)
Z:\>rman target /

Recovery Manager: Release 10.2.0.4.0 - Production on Thu Feb 26 09:56:06 2009

Copyright (c) 1982, 2007, Oracle.  All rights reserved.

connected to target database: TEST (DBID=1977886605, not open)

RMAN> recover database;

Starting recover at 26-FEB-09
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=156 devtype=DISK

starting media recovery

archive log thread 1 sequence 11 is already on disk as file D:\ORACLE\ORADATA\TEST\REDO02.LOG
archive log filename=D:\ORACLE\ORADATA\TEST\REDO02.LOG thread=1 sequence=11
media recovery complete, elapsed time: 00:00:01
Finished recover at 26-FEB-09

RMAN> exit


Recovery Manager complete.

Z:\>sqlplus " / as sysdba"

SQL*Plus: Release 10.2.0.4.0 - Production on Thu Feb 26 09:56:28 2009

Copyright (c) 1982, 2007, Oracle.  All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SYS AS SYSDBA> alter database open read only;

Database altered.

Elapsed: 00:00:02.12
SYS AS SYSDBA> select * from mydb.rotest;
select * from mydb.rotest
                   *
ERROR at line 1:
ORA-00942: table or view does not exist


Elapsed: 00:00:00.03
SYS AS SYSDBA>

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/14876437/viewspace-557892/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/14876437/viewspace-557892/

在 SQL Server 中,`is_read_only=1` 是用于将数据库设置为只读状态的一个选项。当一个数据库被设置为只读时,用户可以查询数据,但能对数据进行任何修改操作,包括插入、更新和删除等操作[^1]。 ### 设置数据库为只读 要将 SQL Server 数据库设置为只读,可以通过以下几种方式实现: #### 使用 T-SQL 命令 可以通过 `ALTER DATABASE` 命令来更改数据库的状态: ```sql ALTER DATABASE [YourDatabaseName] SET READ_ONLY WITH NO_WAIT; ``` 这条命令会立即将指定的数据库设置为只读模式。如果当前有活动的事务正在使用该数据库,则需要等待这些事务完成才能成功执行此命令。为了防止长时间等待,可以使用 `WITH NO_WAIT` 选项,这会导致如果存在阻塞事务,则命令立即失败并返回错误信息。 #### 使用 SQL Server Management Studio (SSMS) 1. 打开 SQL Server Management Studio 并连接到目标服务器。 2. 在对象资源管理器中找到要设置为只读的数据库。 3. 右键点击该数据库,选择“属性”。 4. 在弹出的窗口中选择“选项”页签。 5. 在“状态”部分,找到“数据库为只读”选项,并将其值更改为“True”。 6. 单击“确定”保存更改。 ### 注意事项 - **权限要求**:只有具有适当权限的用户才能更改数据库的状态。通常,这需要 db_owner 角色成员资格或相应的权限。 - **影响范围**:一旦数据库被设置为只读,所有尝试写入数据的操作都将失败,包括但限于 INSERT、UPDATE 和 DELETE 操作。 - **性能考虑**:在某些情况下,将数据库设置为只读可以提高查询性能,因为它减少了锁定和日志记录的需求。 - **恢复方法**:若想再次允许对该数据库进行写操作,需将数据库重新设置为可读写模式: ```sql ALTER DATABASE [YourDatabaseName] SET READ_WRITE; ``` 此外,在特定场景下,如创建快照时,SQL Server 也会自动将快照设置为只读模式,确保其内容会被意外更改[^2]。 对于想要限制特定表而非整个数据库的情况,SQL Server 提供了同的机制,例如通过文件组管理和分区策略来实现部分数据集的只读特性[^3]。 ### 总结 设置 SQL Server 数据库为只读是一个有效的手段,用来保护数据免受未经授权的更改。无论是通过 T-SQL 还是 SSMS,都可以简便地完成这一任务。过,在实施之前应仔细评估其对应用程序和其他依赖于写入操作的服务可能产生的影响。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值