最初了解Oracle数据库的时候,仅仅认为数据库的状态只有两种,启动(Open
)和关闭(shutdown
)。随着对Oracle认识的不断加深,才逐步了解到在启动和关闭两种状态外,还有另外两种状态未挂载(nomount
)和挂载(mount
)。
启动和关闭很容易理解,分别代表数据库的可用状态和不可用状态。在启动状态下,Oracle数据库可以正常提供服务,进行增删改查的事务操作,平时见到最多的也是这种状态。而关闭状态下,数据库无法连接到对应的实例,最基本的查询操作也无法进行,完全程度上的不可用状态,也是日常中常见的一种数据库状态。
shutdown状态
数据库实例为关闭状态,此状态下的数据库无法连接到实例,通过sqlplus / as sysdba
连接到数据库时会看到Connected to idle instance的提示,表示数据库实例当前是不可用的。
# sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Sat Mar 19 13:52:19 2022
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to an idle instance.
SQL>
可以理解为shutdown
状态下的数据库为数据库的初始化状态,这个状态下可以将数据库启动到指定的状态上(除关闭状态之外的其他状态)。例如,将数据库启动到nomount
状态
SQL> startup nomount
ORACLE instance started.
Total System Global Area 1937457152 bytes
Fixed Size 2254464 bytes
Variable Size 587204992 bytes
Database Buffers 1325400064 bytes
Redo Buffers 22597632 bytes
SQL>
nomount状态
通过startup nomount
启动数据库,启动过程中数据库会读取参数文件将数据库启动到指定的nomount状态上,并启动数据库中的一些后台进程(PMON、DBRM、LMON、DBW0、LGWR、CKPT等等)。启动具体详情参见数据库alert日志
启动日志如下
Sat Mar 19 13:54:25 2022
Adjusting the default value of parameter parallel_max_servers
from 1600 to 120 due to the value of parameter processes (150)
Starting ORACLE instance (normal)
...
Starting up:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, OLAP, Data Mining
and Real Application Testing options.
...
Using parameter settings in server-side pfile /oracle/11.2.0/dbs/inittest1.ora
...
Sat Mar 19 13:54:31 2022
PMON started with pid=2, OS id=191701
Sat Mar 19 13:54:31 2022
PSP0 started with pid=3, OS id=191703
Sat Mar 19 13:54:32 2022
VKTM started with pid=4, OS id=191716 at elevated priority
VKTM running at (1)millisec precision with DBRM quantum (100)ms
Sat Mar 19 13:54:32 2022
GEN0 started with pid=5, OS id=191720
Sat Mar 19 13:54:32 2022
DIAG started with pid=6, OS id=191722
Sat Mar 19 13:54:32 2022
DBRM started with pid=7, OS id=191724
Sat Mar 19 13:54:32 2022
PING started with pid=8, OS id=191726
Sat Mar 19 13:54:32 2022
ACMS started with pid=9, OS id=191728
Sat Mar 19 13:54:32 2022
DIA0 started with pid=10, OS id=191730
Sat Mar 19 13:54:32 2022
LMON started with pid=11, OS id=191732
Sat Mar 19 13:54:32 2022
LMD0 started with pid=12, OS id=191734
* Load Monitor used for high load check
* New Low - High Load Threshold Range = [38400 - 51200]
Sat Mar 19 13:54:33 2022
LMS0 started with pid=13, OS id=191736 at elevated priority
Sat Mar 19 13:54:33 2022
LMS1 started with pid=14, OS id=191741 at elevated priority
Sat Mar 19 13:54:33 2022
LMS2 started with pid=15, OS id=191745 at elevated priority
Sat Mar 19 13:54:33 2022
RMS0 started with pid=16, OS id=191749
Sat Mar 19 13:54:33 2022
LMHB started with pid=17, OS id=191751
Sat Mar 19 13:54:33 2022
MMAN started with pid=18, OS id=191753
Sat Mar 19 13:54:33 2022
DBW0 started with pid=19, OS id=191755
Sat Mar 19 13:54:33 2022
DBW1 started with pid=20, OS id=191757
Sat Mar 19 13:54:33 2022
DBW2 started with pid=21, OS id=191759
Sat Mar 19 13:54:33 2022
DBW0 started with pid=19, OS id=191755
Sat Mar 19 13:54:33 2022
DBW1 started with pid=20, OS id=191757
Sat Mar 19 13:54:33 2022
DBW2 started with pid=21, OS id=191759
Sat Mar 19 13:54:33 2022
DBW3 started with pid=22, OS id=191761
Sat Mar 19 13:54:33 2022
DBW4 started with pid=23, OS id=191763
Sat Mar 19 13:54:33 2022
LGWR started with pid=24, OS id=191765
Sat Mar 19 13:54:33 2022
CKPT started with pid=25, OS id=191767
Sat Mar 19 13:54:33 2022
SMON started with pid=26, OS id=191769
Sat Mar 19 13:54:33 2022
RECO started with pid=27, OS id=191771
Sat Mar 19 13:54:33 2022
RBAL started with pid=28, OS id=191773
Sat Mar 19 13:54:33 2022
ASMB started with pid=29, OS id=191775
Sat Mar 19 13:54:33 2022
MMON started with pid=30, OS id=191777
Sat Mar 19 13:54:33 2022
MMNL started with pid=31, OS id=191781
nomount状态下,数据库处于未挂载状态下,数据库实例可以访问,但无法进行DDL事务操作。此时查询数据库的状态为Dismounted
#crsctl stat res -t | grep -A2 ora.test.db
ora.test.db
1 ONLINE INTERMEDIATE node1 Dismounted
2 ONLINE ONLINE node2 Open
查询实例的状态为running
# srvctl status database -d test
Instance test1 is running on node node1
Instance test2 is running on node node2
进入数据库中进行尝试创建表失败,报错ORA-01109: database not open,说明尽管可以连接到实例,但数据库仍未处于open状态
SQL> create table test_nomount(status varchar(10));
create table test_nomount(status varchar(10))
*
ERROR at line 1:
ORA-01109: database not open
查询数据库的一些参数,例如数据库监听、数据库启动日志
SQL> show parameter listener
NAME TYPE VALUE
------------------------------------ ---------------------- ------------------------------
listener_networks string
local_listener string (ADDRESS=(PROTOCOL=TCP)(HOST=172.xx.xx.x)(PORT=1521))
remote_listener string oracle-scan:1521
SQL> show parameter dump
NAME TYPE VALUE
------------------------------------ ---------------------- ------------------------------
background_dump_dest string /oracle/diag/rdbms/test/test1/trace
core_dump_dest string /oracle/diag/rdbms/test/test1/cdump
user_dump_dest string /oracle/diag/rdbms/test/test1/trace
nomount状态下的数据库,可以将数据库进一步启动到mount状态下
SQL> alter database mount;
Database altered.
mount状态
mount状态下,数据库已经挂载,但数据库仍未打开。此时,查询数据库资源状态为Mounted (Closed)。但当数据库启动到mount状态时,数据库中的redo日志组会被挂载(但并未启用)。见启动到mount状态的数据库alert日志
Sat Mar 19 14:31:40 2022
Adjusting the default value of parameter parallel_max_servers
from 1600 to 120 due to the value of parameter processes (150)
Starting ORACLE instance (normal)
alter database mount
Sat Mar 19 14:31:53 2022
Successful mount of redo thread 1, with mount id 2397027473
Sat Mar 19 14:31:53 2022
Database mounted in Shared Mode (CLUSTER_DATABASE=TRUE)
Lost write protection disabled
nodepleted: alter database mount
查看数据库资源状态
# crsctl stat res -t | grep -A2 ora.test.db
ora.test.db
1 ONLINE INTERMEDIATE node1 Mounted (Closed)
2 ONLINE ONLINE node2 Open
实例状态相比nomount状态下的实例没有发生变化。实际上,数据库在nomount状态下已经可以访问了,在mount状态下自然也能访问。
此时,数据库中的数据文件、控制文件等都可以正常访问。
SQL> select NAME,FILE#,STATUS from v$datafile;
NAME FILE# STATUS
-------------------------------------------------- ---------- --------------
+DATADG/test/datafile/system.401.1098876775 1 SYSTEM
+DATADG/test/datafile/sysaux.402.1098876777 2 ONLINE
+DATADG/test/datafile/undotbs1.403.1098876777 3 ONLINE
+DATADG/test/datafile/users.404.1098876777 4 ONLINE
+DATADG/test/datafile/undotbs2.409.1098876893 5 ONLINE
SQL> elect GROUP#,THREAD#,STATUS from v$log;
GROUP# THREAD# STATUS
---------- ---------- --------------------------------
1 1 CURRENT
2 1 INACTIVE
3 2 INACTIVE
4 2 CURRENT
尝试访问数据库中的dba_data_files视图时报错,无法访问,此时数据文件没有打开
SQL> select * from dba_data_files;
select * from dba_data_files
*
ERROR at line 1:
ORA-01219: database not open: queries allowed on fixed tables/views only
此状态下的数据库可以执行alter database open;
将数据库启动到Open状态
SQL> alter database open;
Database altered.
Open状态
Open状态下的数据库为可用状态,所有的后台服务、日志组、数据文件等都可以正常访问,且数据库开始提供事务服务。再次看看讲数据库启动到open状态时日志中中记录了什么,以下日志段为数据库从mount状态启动到open状态的日志输出
Sat Mar 19 14:51:11 2022
alter database open
Picked broadcast on nodemit scheme to generate SCNs
Sat Mar 19 14:51:12 2022
Thread 1 opened at log sequence 5
Current log# 1 seq# 5 mem# 0: +DATADG/test/onlinelog/group_1.406.1098876853
Successful open of redo thread 1
MTTR advisory is disabled because FAST_START_MTTR_TARGET is not set
Sat Mar 19 14:51:12 2022
SMON: enabling cache recovery
[171895] Successfully onlined Undo Tablespace 2.
Undo initialization finished serial:0 start:1294695214 end:1294695444 diff:230 (2 seconds)
Verifying file header nodepatibility for 11g tablespace encryption..
Verifying 11g file header nodepatibility for tablespace encryption nodepleted
SMON: enabling tx recovery
Database Characterset is AL32UTF8
Opening with Resource Manager plan: default_plan
Sat Mar 19 14:51:12 2022
Starting background process VKRM
Sat Mar 19 14:51:12 2022
VKRM started with pid=40, OS id=9815
Starting background process GTX0
Sat Mar 19 14:51:12 2022
GTX0 started with pid=41, OS id=9817
Starting background process RCBG
Sat Mar 19 14:51:12 2022
RCBG started with pid=42, OS id=9819
replication_dependency_tracking turned off (no async multimaster replication found)
Starting background process QMNC
Sat Mar 19 14:51:13 2022
QMNC started with pid=43, OS id=9823
nodepleted: alter database open
Sat Mar 19 14:51:13 2022
minact-scn: Inst 1 is a slave inc#:12 mmon proc-id:191777 status:0x2
minact-scn status: grec-scn:0x0000.00000000 gmin-scn:0x0000.00000000 gcalc-scn:0x0000.00000000
Sat Mar 19 14:51:13 2022
Starting background process CJQ0
Sat Mar 19 14:51:14 2022
CJQ0 started with pid=47, OS id=9886
Setting Resource Manager plan SCHEDULER[0x32DE]:DEFAULT_MAINTENANCE_PLAN via scheduler window
Setting Resource Manager plan DEFAULT_MAINTENANCE_PLAN via parameter
可以看到,其中最要中的是关于redo日志的应用,也即是说,redo日志是在Oracle数据库启动到Open状态下才被应用的。突然想起,Oracle奉行的日志先行原则,redo日志是用来记录事务中的,但在mount状态下显然数据库并不会产生事务(仅提供对一些参数文件的查询),因此在mount状态下仅仅挂载了redo日志组并没有启用。
在redo日志被打开后,数据库立刻进行了事务回滚(Undo initialization finished serial:0 start:)。接着设置数据库字符集(Database Characterset is AL32UTF8),最后,数据库完全开启。
在看到网上一篇博客中写道:"OPEN,这种模式将启动实例,加载并打开数据库"这里时,有点误差,数据库的实例在数据库启动到nomount状态后已经被启动,并非是在open阶段被启动的。
此时数据库资源应为Open状态
#crsctl stat res -t | grep -A2 ora.test.db
ora.test.db
1 ONLINE ONLINE node1 Open
2 ONLINE ONLINE node2 Open