番外:你真的了解 Oracle 的启动流程吗?

番外系列说明:该系列所有文章都将作为独立篇章进行知识点讲解,是对其他系列博文进行的补充说明,来自于博客园AskScuti。

主题关于数据库启动流程三个阶段

内容预览:本篇涉及数据库启动的三个阶段分别做了什么事及对应操作方式。

目录

1. 启动概述

2. 第一阶段

3. 第二阶段

4. 第三阶段

5. 汇总说明

 

1. 启动概述

  数据库启动从关闭到开启经历三个阶段:NOMOUNT、MOUNT和OPEN。每个阶段所能查看的动态性能视图也不一样,比如:在NOMOUNT阶段,可查询动态性能视图 v$instance,无法查询 v$database,因为后者里面的内容是在控制文件中查询的,而控制文件是在第二阶段才被加载。具体图例参考官方文档11g Release 2 (11.2) Database Concepts:Overview of Instance and Database Startup

2. 第一阶段

数据库启动的第一阶段将加载参数文件,按照参数文件具体值进行创建SGA,并开启一系列后台进程。

将数据库启动到NOMOUNT阶段

SQL> startup nomount
ORACLE instance started.

Total System Global Area  830930944 bytes
Fixed Size               2232920 bytes
Variable Size            591400360 bytes
Database Buffers         234881024 bytes
Redo Buffers              2416640 bytes

我们来查下当前实例状态

SQL> select status from v$instance;

STATUS
------------------------
STARTED

在 NOMOUNT 状态显示为:STARTED状态,也就是加载参数文件的第一阶段状态

查看日志确定动作,节选日志片段

 1 Starting up:
 2 Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
 3 With the Partitioning, OLAP, Data Mining and Real Application Testing options.
 4 ORACLE_HOME = /u01/app/oracle/product/11.2.0/dbhome_1
 5 System name:    Linux
 6 Node name:    henry
 7 Release:    3.10.0-862.el7.x86_64
 8 Version:    #1 SMP Wed Mar 21 18:14:51 EDT 2018
 9 Machine:    x86_64
10 VM name:    VMWare Version: 6
11 Using parameter settings in server-side spfile /u01/app/oracle/product/11.2.0/dbhome_1/dbs/spfilePROD1.ora
12 System parameters with non-default values:
13   processes                = 150
14   sessions                 = 247
15   memory_target            = 796M
16   control_files            = "/u01/app/oracle/oradata/PROD1/control01.ctl"
17   control_files            = "/u01/app/oracle/oradata/PROD1/control02.ctl"
18   control_files            = "/u01/app/oracle/oradata/PROD1/control03.ctl"
19   control_files            = "/u01/app/oracle/oradata/PROD1/control04.ctl"
20   control_files            = "/u01/app/oracle/oradata/PROD1/control05.ctl"
21   control_files            = "/u01/app/oracle/oradata/PROD1/control06.ctl"
22   control_files            = "/u01/app/oracle/oradata/PROD1/control07.ctl"
23   control_files            = "/u01/app/oracle/oradata/PROD1/control08.ctl"
24   db_block_size            = 8192
25   compatible               = "11.2.0.0.0"
26   log_archive_dest_1       = "location=/u01/app/oracle/archive1"
27   db_recovery_file_dest    = "/u01/app/oracle/fast_recovery_area"
28   db_recovery_file_dest_size= 4122M
29   undo_tablespace          = "UNDOTBS1"
30   remote_login_passwordfile= "EXCLUSIVE"
31   db_domain                = ""
32   dispatchers              = "(PROTOCOL=TCP) (SERVICE=PROD1XDB)"
33   local_listener           = "(ADDRESS=(PROTOCOL=tcp)(HOST=henry)(PORT=1566))"
34   local_listener           = "(ADDRESS=(PROTOCOL=tcp)(HOST=henry)(PORT=1521))"
35   local_listener           = "(ADDRESS=(PROTOCOL=tcp)(HOST=henry)(PORT=1234))"
36   job_queue_processes      = 1000
37   audit_file_dest          = "/u01/app/oracle/admin/PROD1/adump"
38   audit_trail              = "DB"
39   db_name                  = "PROD1"
40   open_cursors             = 300
41   diagnostic_dest          = "/u01/app/oracle"
42 Wed May 22 10:21:56 2019
43 PMON started with pid=2, OS id=14957 
44 Wed May 22 10:21:56 2019
45 PSP0 started with pid=3, OS id=14960 
46 Wed May 22 10:21:57 2019
47 VKTM started with pid=4, OS id=14962 at elevated priority
48 VKTM running at (1)millisec precision with DBRM quantum (100)ms
49 Wed May 22 10:21:58 2019
50 GEN0 started with pid=5, OS id=14967 
51 Wed May 22 10:21:58 2019
52 DIAG started with pid=6, OS id=14969 
53 Wed May 22 10:21:58 2019
54 DBRM started with pid=7, OS id=14971 
55 Wed May 22 10:21:58 2019
56 DIA0 started with pid=8, OS id=14973 
57 Wed May 22 10:21:58 2019
58 MMAN started with pid=9, OS id=14975 
59 Wed May 22 10:21:58 2019
60 DBW0 started with pid=10, OS id=14977 
61 Wed May 22 10:21:58 2019
62 LGWR started with pid=11, OS id=14979 
63 Wed May 22 10:21:58 2019
64 CKPT started with pid=12, OS id=14981 
65 Wed May 22 10:21:58 2019
66 SMON started with pid=13, OS id=14983 
67 Wed May 22 10:21:58 2019
68 RECO started with pid=14, OS id=14986 
69 Wed May 22 10:21:58 2019
70 MMON started with pid=15, OS id=14988 
71 Wed May 22 10:21:58 2019
72 MMNL started with pid=16, OS id=14990 
73 starting up 1 dispatcher(s) for network address '(ADDRESS=(PARTIAL=YES)(PROTOCOL=TCP))'...
74 starting up 1 shared server(s) ...
75 ORACLE_BASE from environment = /u01/app/oracle
alert_PROD1.log

可以看到,在第一阶段启动过程中有这么一句话:Using parameter settings in server-side spfile /u01/app/oracle/product/11.2.0/dbhome_1/dbs/spfilePROD1.ora 。默认使用的是动态参数文件,就是按照这个动态参数文件里面的值开始开辟(创建)SGA内存区,包括但不限于整体SGA大小,数据库缓冲区大小,日志缓冲区大小等等。后面可以看到开启了一系列后台进程(这里有6个进程很重要:CKPT、SMON、PMON、DBWn、LGWR、ARCn,其中前5个是必须开启的,第6个是否启动是和当前数据库是否为归档模式有关,如果是归档模式,则启动,如果是非归档模式,则没有此进程)

系统命令查看后台进程

SQL> !ps -ef | grep ora_
oracle   16030     1  0 10:35 ?        00:00:00 ora_pmon_PROD1
oracle   16032     1  0 10:35 ?        00:00:00 ora_psp0_PROD1
oracle   16034     1 14 10:35 ?        00:00:04 ora_vktm_PROD1
oracle   16038     1  0 10:35 ?        00:00:00 ora_gen0_PROD1
oracle   16040     1  0 10:35 ?        00:00:00 ora_diag_PROD1
oracle   16042     1  0 10:35 ?        00:00:00 ora_dbrm_PROD1
oracle   16044     1  0 10:35 ?        00:00:00 ora_dia0_PROD1
oracle   16046     1  2 10:35 ?        00:00:00 ora_mman_PROD1
oracle   16048     1  0 10:35 ?        00:00:00 ora_dbw0_PROD1
oracle   16050     1  0 10:35 ?        00:00:00 ora_lgwr_PROD1
oracle   16052     1  0 10:35 ?        00:00:00 ora_ckpt_PROD1
oracle   16054     1  0 10:35 ?        00:00:00 ora_smon_PROD1
oracle   16056     1  0 10:35 ?        00:00:00 ora_reco_PROD1
oracle   16058     1  3 10:35 ?        00:00:01 ora_mmon_PROD1
oracle   16060     1  0 10:35 ?        00:00:00 ora_mmnl_PROD1
oracle   16062     1  0 10:35 ?        00:00:00 ora_d000_PROD1
oracle   16064     1  0 10:35 ?        00:00:00 ora_s000_PROD1
oracle   16131     1  0 10:35 ?        00:00:00 ora_arc0_PROD1
oracle   16134     1  7 10:35 ?        00:00:01 ora_arc1_PROD1
oracle   16136     1  0 10:35 ?        00:00:00 ora_arc2_PROD1
oracle   16138     1  0 10:35 ?        00:00:00 ora_arc3_PROD1
oracle   16143     1  0 10:35 ?        00:00:00 ora_qmnc_PROD1
oracle   16161     1  2 10:35 ?        00:00:00 ora_cjq0_PROD1
oracle   16166     1  5 10:35 ?        00:00:00 ora_j000_PROD1
oracle   16168     1  2 10:35 ?        00:00:00 ora_j001_PROD1
oracle   16170     1  0 10:35 ?        00:00:00 ora_j002_PROD1
oracle   16173     1  1 10:35 ?        00:00:00 ora_q000_PROD1
oracle   16176     1  0 10:35 ?        00:00:00 ora_q001_PROD1
oracle   16186 14851  0 10:35 pts/1    00:00:00 /bin/bash -c ps -ef | grep ora_
oracle   16188 16186  0 10:35 pts/1    00:00:00 grep ora_

其中,归档模式下,ARCH进程默认开启4个,可通过修改参数 log_archive_max_processes 最多可以开启30个,查看官方文档11g Release 2 (11.2) Database Administrator's Guide:Adjusting the Number of Archiver Processes

 

3. 第二阶段

数据库启动的第二阶段将加载控制文件,为打开数据文件和日志文件做准备。

将数据库更改到MOUNT状态

SQL> alter database mount;

Database altered.

查看日志片段,可看到在控制文件里面记录了当前数据库的DBID(唯一ID,类似于身份证号),有了这个,就确定了接下来要打开哪个数据库了。

alter database mount
Wed May 22 11:09:49 2019
Successful mount of redo thread 1, with mount id 2230329145
Database mounted in Exclusive Mode
Lost write protection disabled
Completed: alter database mount

数据库在MOUNT阶段后,就可以查询 v$database 数据字典了,刚才说过,它里面的东西都是从控制文件中查出来的。

SQL> select open_mode from v$database;

OPEN_MODE
-------------------------------------
MOUNTED

也可以查询数据文件、日志文件等,因为这些动态性能视图查的都是控制文件里面的内容。

SQL> select name from v$datafile;

NAME
--------------------------------------------------
/u01/app/oracle/oradata/PROD1/system01.dbf
/u01/app/oracle/oradata/PROD1/sysaux01.dbf
/u01/app/oracle/oradata/PROD1/undotbs01.dbf
/u01/app/oracle/oradata/PROD1/users01.dbf
/u01/app/oracle/oradata/PROD1/example01.dbf
/u01/app/oracle/oradata/PROD1/abc01.dbf
/u01/app/oracle/oradata/PROD1/abcd01.dbf
/u01/app/oracle/oradata/PROD1/tbs_c01.dbf
/u01/app/oracle/oradata/PROD1/aaa01.dbf
/u01/app/oracle/oradata/PROD1/aaa02.dbf

10 rows selected.

但是无法查询数据文件里面的具体内容,因为数据文件还没有被加载,数据库还没有被打开。

 

4. 第三阶段

将数据库开启

SQL> alter database open;

Database altered.

因为控制文件里面指定了数据文件、在线日志文件等具体路径,因此在OPEN阶段为数据库运行做最后检查确认并打开所有文件。至此,数据库完成启动。

SQL> select ename,sal from scott.emp;

ENAME      SAL
---------- -----
SMITH      800
ALLEN      1600
WARD       1250
JONES      2975
MARTIN     1250
BLAKE      2850
CLARK      2450
SCOTT      3000
KING       5000
TURNER     1500
ADAMS      1100
JAMES      950
FORD       3000
MILLER     1300

14 rows selected.

 

5. 汇总说明

对于初学者,数据库分步启动命令常与启动命令选项搞混淆。

你可以把数据库启动到NOMOUNT状态,仅加载参数文件:前提是数据库处于关闭状态

SQL> startup nomount
ORACLE instance started.

Total System Global Area  830930944 bytes
Fixed Size               2232920 bytes
Variable Size            591400360 bytes
Database Buffers         234881024 bytes
Redo Buffers              2416640 bytes

你也可以把数据库直接启动到MOUNT状态,加载参数文件和控制文件:前提是数据库处于关闭状态

SQL> startup mount
ORACLE instance started.

Total System Global Area  830930944 bytes
Fixed Size               2232920 bytes
Variable Size            591400360 bytes
Database Buffers         234881024 bytes
Redo Buffers              2416640 bytes
Database mounted.

你也可以直接把数据库启动到OPEN状态,加载参数文件和控制文件,并打开其他所有文件:前提是数据库处于关闭状态

SQL> startup
ORACLE instance started.

Total System Global Area  830930944 bytes
Fixed Size               2232920 bytes
Variable Size            591400360 bytes
Database Buffers         234881024 bytes
Redo Buffers              2416640 bytes
Database mounted.
Database opened.

请注意:

如果处于NOMOUNT状态,之后要打开数据库,可以先将状态更改为MOUNT然后再将状态更改为OPEN,就是不能跨阶段

SQL> startup nomount 
ORACLE instance started.

Total System Global Area  830930944 bytes
Fixed Size               2232920 bytes
Variable Size            591400360 bytes
Database Buffers         234881024 bytes
Redo Buffers              2416640 bytes
SQL> alter database mount;

Database altered.

SQL> alter database open;

Database altered.

也可以在NOMOUNT状态,将数据库关闭,然后直接STARTUP。

SQL> startup nomount
ORACLE instance started.

Total System Global Area  830930944 bytes
Fixed Size               2232920 bytes
Variable Size            591400360 bytes
Database Buffers         234881024 bytes
Redo Buffers              2416640 bytes
SQL> shutdown immediate
ORA-01507: database not mounted


ORACLE instance shut down.
SQL> startup
ORACLE instance started.

Total System Global Area  830930944 bytes
Fixed Size               2232920 bytes
Variable Size            591400360 bytes
Database Buffers         234881024 bytes
Redo Buffers              2416640 bytes
Database mounted.
Database opened.

如果处于MOUNT状态,打开数据库需要用ALTER语句。

SQL> select status from v$instance;

STATUS
------------------------
MOUNTED

SQL> alter database open;

Database altered.

或者直接关闭数据库,发出STARTUP命令启动。

最后,老手常用的 startup force 命令,初学者的你请慎用!因为你不知道这个命令是在何时且在什么场景下才能使用,实验测试环境可以随便。这个命令包含两个动作:shutdown abort 和 startup 。

SQL> startup force;
ORACLE instance started.

Total System Global Area  830930944 bytes
Fixed Size               2232920 bytes
Variable Size            591400360 bytes
Database Buffers         234881024 bytes
Redo Buffers              2416640 bytes
Database mounted.
Database opened.

查看日志片段

Wed May 22 11:54:00 2019
Shutting down instance (abort)
License high water mark = 3
USER (ospid: 23787): terminating the instance
Instance terminated by USER, pid = 23787
Wed May 22 11:54:01 2019
Instance shutdown complete

注意shutdown abort,它比较特殊,属于非一致性关闭,类似于服务器突然断电。生产库中使用这个命令,是很有可能丢失数据的,因为日志来不及写。shutdown abort 属于数据库关闭四种模式中的其中一种,在数据库关闭的四种模式博文中进行介绍。

转载于:https://www.cnblogs.com/askscuti/p/10905127.html

你可能感兴趣的:(番外:你真的了解 Oracle 的启动流程吗?)