番外系列说明:该系列所有文章都将作为独立篇章进行知识点讲解,是对其他系列博文进行的补充说明,来自于博客园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状态,也就是加载参数文件的第一阶段状态

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

 Starting up:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options.
ORACLE_HOME = /u01/app/oracle/product/11.2.0/dbhome_1
System name: Linux
Node name: henry
Release: 3.10.0-862.el7.x86_64
Version: #1 SMP Wed Mar 21 18:14:51 EDT 2018
Machine: x86_64
VM name: VMWare Version: 6
Using parameter settings in server-side spfile /u01/app/oracle/product/11.2.0/dbhome_1/dbs/spfilePROD1.ora
System parameters with non-default values:
processes = 150
sessions = 247
memory_target = 796M
control_files = "/u01/app/oracle/oradata/PROD1/control01.ctl"
control_files = "/u01/app/oracle/oradata/PROD1/control02.ctl"
control_files = "/u01/app/oracle/oradata/PROD1/control03.ctl"
control_files = "/u01/app/oracle/oradata/PROD1/control04.ctl"
control_files = "/u01/app/oracle/oradata/PROD1/control05.ctl"
control_files = "/u01/app/oracle/oradata/PROD1/control06.ctl"
control_files = "/u01/app/oracle/oradata/PROD1/control07.ctl"
control_files = "/u01/app/oracle/oradata/PROD1/control08.ctl"
db_block_size = 8192
compatible = "11.2.0.0.0"
log_archive_dest_1 = "location=/u01/app/oracle/archive1"
db_recovery_file_dest = "/u01/app/oracle/fast_recovery_area"
db_recovery_file_dest_size= 4122M
undo_tablespace = "UNDOTBS1"
remote_login_passwordfile= "EXCLUSIVE"
db_domain = ""
dispatchers = "(PROTOCOL=TCP) (SERVICE=PROD1XDB)"
local_listener = "(ADDRESS=(PROTOCOL=tcp)(HOST=henry)(PORT=1566))"
local_listener = "(ADDRESS=(PROTOCOL=tcp)(HOST=henry)(PORT=1521))"
local_listener = "(ADDRESS=(PROTOCOL=tcp)(HOST=henry)(PORT=1234))"
job_queue_processes = 1000
audit_file_dest = "/u01/app/oracle/admin/PROD1/adump"
audit_trail = "DB"
db_name = "PROD1"
open_cursors = 300
diagnostic_dest = "/u01/app/oracle"
Wed May 22 10:21:56 2019
PMON started with pid=2, OS id=14957
Wed May 22 10:21:56 2019
PSP0 started with pid=3, OS id=14960
Wed May 22 10:21:57 2019
VKTM started with pid=4, OS id=14962 at elevated priority
VKTM running at (1)millisec precision with DBRM quantum (100)ms
Wed May 22 10:21:58 2019
GEN0 started with pid=5, OS id=14967
Wed May 22 10:21:58 2019
DIAG started with pid=6, OS id=14969
Wed May 22 10:21:58 2019
DBRM started with pid=7, OS id=14971
Wed May 22 10:21:58 2019
DIA0 started with pid=8, OS id=14973
Wed May 22 10:21:58 2019
MMAN started with pid=9, OS id=14975
Wed May 22 10:21:58 2019
DBW0 started with pid=10, OS id=14977
Wed May 22 10:21:58 2019
LGWR started with pid=11, OS id=14979
Wed May 22 10:21:58 2019
CKPT started with pid=12, OS id=14981
Wed May 22 10:21:58 2019
SMON started with pid=13, OS id=14983
Wed May 22 10:21:58 2019
RECO started with pid=14, OS id=14986
Wed May 22 10:21:58 2019
MMON started with pid=15, OS id=14988
Wed May 22 10:21:58 2019
MMNL started with pid=16, OS id=14990
starting up 1 dispatcher(s) for network address '(ADDRESS=(PARTIAL=YES)(PROTOCOL=TCP))'...
starting up 1 shared server(s) ...
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 属于数据库关闭四种模式中的其中一种,在数据库关闭的四种模式博文中进行介绍。

最新文章

  1. 3.聚类–K-means的Java实现
  2. Oracle11g +Win 64+PLSQL9.0
  3. vs2015连接oracle 11g(.net自带方式 using System.Data.OracleClient;)
  4. Day5_作业
  5. js传值
  6. webservice cxf error:java.lang.IllegalArgumentException: Argument(s) "type" can't be null.
  7. Codeforces Round #379 (Div. 2) E. Anton and Tree 树的直径
  8. JAVA核心技术--继承
  9. Org-mode 任务添加提醒
  10. ASP.NET MVC and jqGrid 学习笔记 2-如何从本地获得数据
  11. MySQL 存储过程删除大表
  12. poj2253 Frogger Dijkstra变形
  13. 【javascript】回调函数
  14. Android简易实战教程--第二十三话《绚丽的菜单项》
  15. Oracle dblink详解
  16. golang 实现文件传输小demo
  17. 阿里云物联网套件(iot)设备间通信(M2M)在web端的实践
  18. [Java in NetBeans] Lesson 17. File Input/Output.
  19. 使用Windows Server 2003搭建一个asp+access网站
  20. springmvc+spring框架

热门文章

  1. MATLAB中冒号的用法解析
  2. 【WPF学习】第十八章 多点触控输入
  3. AGC011-E Increasing Numbers
  4. C# monitor keyboard and print pressed key
  5. 9个常用的正则表达式-sunziren
  6. 图片上传的进度条-jquery
  7. generalization error
  8. 数据结构(集合)学习之List
  9. Auto-scaling scikit-learn with Apache Spark
  10. Projected coordinate systems 和 wkid