数据库控制文件中的SCN详解
在的启动和关闭数据库的时候控制文件起着重要的作用,从官方文档中我们可以知道控制文件中保存着下列的信息: 数据库的名称及数据库创建时间等 所有的数据文件、重做日志文件的名称和位置信息。 表空间的信息。 Offline 数据文件的信息。 重做日志及归档日志
在的启动和关闭数据库的时候控制文件起着重要的作用,从官方文档中我们可以知道控制 文件中保存着下列的信息:
数据库的名称及数据库创建时间等
所有的数据文件、重做日志文件的名称和位置信息。
表空间的信息。
Offline数据文件的信息。
重做日志及归档日志的信息。
备份集及备份文件的信息
检查点及SCN信息等。
正常关闭数据库时,控制文件中的SCN值
在控制文件的数据文件信息部分,对于每一个数据文件都有一个Checkpoint SCN 和 Stop SCN,在数据库启动时要对其进行校验。
我们知道数据库在一致性关闭的时候控制文件中的SCN是怎样的呢?
1.一致性关闭数据库。Dump控制文件
SYS@ENMOEDU > shutdown immediate
SYS@ENMOEDU > startup mount
Database mounted.
SYS@ENMOEDU > oradebug setmypid;
Statement processed.
SYS@ENMOEDU > oradebug dump controlf 3;
Statement processed.
2.数据库的相关信息,上次数据库成功完成检查点的信息
***************************************************************************
DATABASE ENTRY
***************************************************************************
(size = 316, compat size = 316, div max = 1, div in-use = 1,
last-recid= 0, old-recno = 0, last-recno = 0)
(extent = 1, blkno = 1, numrecs = 1)
01/13/2014 16:13:27
DB Name "ENMOEDU"
Database flags = 0x00404001 0x00001000
Controlfile Creation Timestamp 01/13/2014 16:13:28
Incmplt recovery scn: 0x0000.00000000
Resetlogs scn: 0x0000.000fc451 Resetlogs Timestamp 02/05/2014 15:44:46
Prior resetlogs scn: 0x0000.000fa657 Prior resetlogs Timestamp 02/05/2014 14:15:27
Redo Version: compatible=0xb200000
#Data files = 6, #Online files = 6
Database checkpoint: Thread=1 scn: 0x0000.0011b73b
Threads: #Enabled=1, #Open=0, Head=0, Tail=0
3.检查点进程记录。Low cache rba是恢复的起点,此时是fffffff说明数据库不需要恢复。On disk rba是恢复的终点。
***************************************************************************
CHECKPOINT PROGRESS RECORDS
***************************************************************************
(size = 8180, compat size = 8180, div max = 11, div in-use = 0,
last-recid= 0, old-recno = 0, last-recno = 0)
(extent = 1, blkno = 2, numrecs = 11)
THREAD #1 - status:0x1 flags:0x0 dirty:0
low cache rba:(0xffffffff.ffffffff.ffff) on disk rba:(0x6.6f7.0)
on disk scn: 0x0000.0011b58f 02/19/2014 21:58:48
resetlogs scn: 0x0000.000fc451 02/05/2014 15:44:46
heartbeat: 839919586 mount id: 99161229
Flashback log tail log# 2 thread# 1 seq 2 block 5337 byte 0
4.Redo线程记录中记录了检查点的信息,与数据库的检查点一样
***************************************************************************
REDO THREAD RECORDS
***************************************************************************
(size = 256, compat size = 256, div max = 8, div in-use = 1,
last-recid= 0, old-recno = 0, last-recno = 0)
(extent = 1, blkno = 9, numrecs = 8)
THREAD #1 - status:0xbe thread links forward:0 back:0
#logs:3 first:1 last:3 current:3 last used seq#:0x6
enabled at scn: 0x0000.000fc451 02/05/2014 15:44:46
disabled at scn: 0x0000.00000000 01/01/1988 00:00:00
opened at 02/19/2014 21:57:43 by instance ENMOEDU
Checkpointed at scn: 0x0000.0011b73b 02/19/2014 21:58:52
5.数据文件中记录的检查点信息与DATABASE ENTRY记录的一样
***************************************************************************
DATA FILE RECORDS
***************************************************************************
DATA FILE #6:
name #13: /u01/app/oracle/oradata/ENMOEDU/sample01.dbf
creation size=12800 block size=8192 status=0xe head=13 tail=13 dup=1
tablespace 7, index=7 krfil=6 prev_file=0
unrecoverable scn: 0x0000.001143f4 02/10/2014 15:34:23
Checkpoint cnt:7 scn: 0x0000.0011b73b 02/19/2014 21:58:52
Stop scn: 0x0000.0011b73b 02/19/2014 21:58:52
Creation Checkpointed at scn: 0x0000.00111736 02/10/2014 15:28:26
thread:1 rba:(0x4.1f5.10)
enabled threads: 01000000 00000000 00000000 00000000 00000000 00000000
由于数据库是一致性关闭,数据文件中的Checkpoint scn 与Stop scn,database scn和redo中记录的scn值都一样,所以在下次启动数据库的时候可以直接启动,不用进行恢复。
数据库异常关闭的时候的SCN的值。
1.如果数据库异常关闭,则不会执行任何的检查点。通过shutdown abort可以模拟一次异常,当使用shutdown abort方式关闭数据库时,此时的SCN值是怎么样的。
SYS@ENMOEDU > shutdown abort
ORACLE instance shut down.
SYS@ENMOEDU > startup mount
Database mounted.
SYS@ENMOEDU > oradebug setmypid;
Statement processed.
SYS@ENMOEDU > oradebug dump controlf 3;
Statement processed.
2.在DATABASE ENTRY中我们可以看到数据库thread checkpoint信息。
***************************************************************************
DATABASE ENTRY
***************************************************************************
(size = 316, compat size = 316, div max = 1, div in-use = 1,
last-recid= 0, old-recno = 0, last-recno = 0)
(extent = 1, blkno = 1, numrecs = 1)
01/13/2014 16:13:27
DB Name "ENMOEDU"
Database flags = 0x00404001 0x00001200
Controlfile Creation Timestamp 01/13/2014 16:13:28
Incmplt recovery scn: 0x0000.00000000
Resetlogs scn: 0x0000.000fc451 Resetlogs Timestamp 02/05/2014 15:44:46
Prior resetlogs scn: 0x0000.000fa657 Prior resetlogs Timestamp 02/05/2014 14:15:27
Redo Version: compatible=0xb200000
#Data files = 6, #Online files = 6
Database checkpoint: Thread=1 scn: 0x0000.0011b73e
Threads: #Enabled=1, #Open=1, Head=1, Tail=1
3. 检查点信息其中包含 low chache rba 和on disk ora信息。low cache rba(recovery block address)是恢复的起点,on disk rba(确定已经写到磁盘上的重做值)是恢复的终点,
***************************************************************************
CHECKPOINT PROGRESS RECORDS
***************************************************************************
(size = 8180, compat size = 8180, div max = 11, div in-use = 0,
last-recid= 0, old-recno = 0, last-recno = 0)
(extent = 1, blkno = 2, numrecs = 11)
THREAD #1 - status:0x2 flags:0x0 dirty:510
low cache rba:(0x6.18e2.0) on disk rba:(0x6.ca99.0)
on disk scn: 0x0000.0011e785 02/19/2014 22:55:08
resetlogs scn: 0x0000.000fc451 02/05/2014 15:44:46
heartbeat: 839992040 mount id: 99202971
Flashback log tail log# 3 thread# 1 seq 3 block 2244 byte 0
4. Redo线程的记录。
***************************************************************************
REDO THREAD RECORDS
***************************************************************************
(size = 256, compat size = 256, div max = 8, div in-use = 1,
last-recid= 0, old-recno = 0, last-recno = 0)
(extent = 1, blkno = 9, numrecs = 8)
THREAD #1 - status:0xbf thread links forward:0 back:0
#logs:3 first:1 last:3 current:3 last used seq#:0x6
enabled at scn: 0x0000.000fc451 02/05/2014 15:44:46
disabled at scn: 0x0000.00000000 01/01/1988 00:00:00
opened at 02/19/2014 22:49:27 by instance ENMOEDU
Checkpointed at scn: 0x0000.0011b73e 02/19/2014 22:49:27
thread:1 rba:(0x6.9ce.10)
enabled threads: 01000000 00000000 00000000 00000000 00000000 00000000
5. 数据文件的检查点信息;
DATA FILE #6:
name #13: /u01/app/oracle/oradata/ENMOEDU/sample01.dbf
creation size=12800 block size=8192 status=0xe head=13 tail=13 dup=1
tablespace 7, index=7 krfil=6 prev_file=0
unrecoverable scn: 0x0000.001143f4 02/10/2014 15:34:23
Checkpoint cnt:8 scn: 0x0000.0011b73e 02/19/2014 22:49:27
Stop scn: 0xffff.ffffffff 02/19/2014 21:58:52
Creation Checkpointed at scn: 0x0000.00111736 02/10/2014 15:28:26
thread:1 rba:(0x4.1f5.10)
enabled threads: 01000000 00000000 00000000 00000000 00000000 00000000
可以看出由于是异常关闭数据库,数据库没有完成最后的检查点,数据库文件的stop scn仍然设定为最大的ffffffff。由以上信息可以看出,各部分的checkpoint scn 都是一样的,但是数据文件的stop scn不等于checkpoint scn,意味着数据库的上一次的关闭没有执行完全检查点,是异常关闭数据库,此时启动数据库需要进行完全恢复。
在进行恢复的时候,从检查点信息中读取low cache rba的值确定日志文件的地址。再读取日志,从最后的检查点开始,应用所有的重做记录,这个过程叫做前滚。数据库可以被打开提供访问和使用,但是此时数据库中包含已提交的和没有提交的事务,使用undo段回滚。
再对数据库进行实例恢复的时候,我们从alert日志中可以看到的信息。
alter database open
Beginning crash recovery of 1 threads
parallel recovery started with 2 processes
Started redo scan
Completed redo scan
read 22747 KB redo, 510 data blocks need recovery
Started redo application at
Thread 1: logseq 6, block 6370
Recovery of Online Redo Log: Thread 1 Group 3 Seq 6 Reading mem 0
Mem# 0: /u01/app/oracle/oradata/ENMOEDU/redo03.log
Completed redo application of 3.07MB
Wed Feb 19 23:13:55 2014
Completed crash recovery at
Thread 1: logseq 6, block 51865, scn 1193381
510 data blocks read, 493 data blocks written, 22747 redo k-bytes read
Wed Feb 19 23:13:55 2014
在实例恢复的时候。 Alter中的logseq 6, block 6370就是日志中的检查点中记录的low cache rba:(0x6.18e2.0) 。恢复完成时的logseq 6, block 51865就是on disk rba:(0x6.ca99.0)。把十六进制的数换成十进制。
总结:当一致性关闭数据库的时候,关闭数据库之前数据库会执行一个检查点动作,每个数据文件的你Stop SCN会被设置为Checkpoint SCN。数据库不需要恢复。当数据库是异常关闭的时候,Stop SCN 来不及设置为Checkpoint SCN会是无穷大,在重启数据库的时候需要恢复

ToaddauserinMySQL,usetheCREATEUSERstatement.1)UseCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';tocreateauser.2)Enforcestrongpasswordpolicieswithvalidate_passwordpluginsettings.3)GrantspecificprivilegesusingGRANTstatement.4)Forremoteaccess,use

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
