数据库的启动和关闭过程

ORACLE SERVER由实例(Instance)和数据库(database)组成,数据库是指存储在磁盘上的一组物理文件。ORACLE根据ORACLE_SID来寻找参数文件启动实例。实例是由一组后台进程和一块共享内存区域(SGA)组成。后台进程是数据库和操作系统进行交互的通道。

数据库启动

Oracle  的启动需要经历四个状态,SHUTDOWN –>NOMOUNT –>MOUNT –>OPEN

  • SHUTDOWN: 原始状态
  • NOMOUNT: 启动实例(后台进程开启 SGA组建成功)
  • MOUNT: 打开控制文件
  • OPEN: 打开控制文件中描述的文件,并对外提供数据库的正常服务
  • 启动命令对应的状态:

    startup nomount        -----启动实例,不装载数据库
    startup mount          -----启动实例,装载数据库,不打开数据库 
    alter database mount   -----前提是在nomount状态下
    
    startup                -----startup后面不加其它信息的话,系统会为我们直接启动到第4个状态  
    alter database open    ----前提是mount状态下 

    查看状态的转换

    clip_image002

    SQL> startup nomount
    ORACLE instance started.
    
    Total System Global Area 3290345472 bytes
    Fixed Size            2217832 bytes
    Variable Size         1795164312 bytes
    Database Buffers     1476395008 bytes
    Redo Buffers           16568320 bytes
    SQL> select status from v$instance;
    
    STATUS
    ------------
    STARTED
    
    SQL> alter database mount;
    
    Database altered.
    
    SQL> select status from v$instance;
    
    STATUS
    ------------
    MOUNTED
    
    SQL> alter database open;
    
    Database altered.
    
    SQL> select status from v$instance;
    
    STATUS
    ------------
    OPEN

    1:shutdown

    第一状态没什么好解释的,oracle的所有文件都静静的躺在磁盘里,一切都还未开始,属于关机状态

    2:nomount

  • 读取参数文件
  • 分配SGA
  • 启动后台进程
  • 开放alertSID.log文件和跟踪文件
  • Starting the instance (nomount)

    1. Reading the initialization file from $ORACLE_HOME/dbs in the following order:

    -first spfileSID.ora

    -if not found then, spfile.ora

    -if not found then, initSID.ora

               Specifying the PFILE parameter with STARTUP overrides the default behavior.

    2. Allocating the SGA

    3. Starting the background processes

    4. Opening the alertSID.log file and the trace files

         The  database must be named with the DB_NAME parameter either in the initialization Parameter file or in the STARTUP command.

    参看启动参数文件

    [oracle@oracledb dbs]$ cd $ORACLE_HOME/dbs
    [oracle@oracledb dbs]$ pwd
    /usr/oracle/app/product/11.2.0/dbhome_1/dbs
    [oracle@oracledb dbs]$ ls
    hc_orcl.dat init.ora initorcl.ora lkORCL orapworcl spfileorcl.ora

    startup 启动次序 spfile优先于pfile。查找文件的顺序是 spfileSID.ora-〉spfile.ora-〉initSID.ora-〉init.ora。 如果都找不到的话,那么将启动失败。

    参照:oracle的参数文件:pfile和spfile

    init.ora 内容

    [oracle@oracledb dbs]$ cat init.ora 
    # 
    # $Header: rdbms/admin/init.ora /main/23 2009/05/15 13:35:38 ysarig Exp $ 
    # 
    # Copyright (c) 1991, 1997, 1998 by Oracle Corporation
    # NAME
    #   init.ora
    # FUNCTION
    # NOTES
    # MODIFIED
    #     ysarig     05/14/09  - Updating compatible to 11.2
    #     ysarig     08/13/07  - Fixing the sample for 11g
    #     atsukerm   08/06/98 -  fix for 8.1.
    #     hpiao      06/05/97 -  fix for 803
    #     glavash    05/12/97 -  add oracle_trace_enable comment
    #     hpiao      04/22/97 -  remove ifile=, events=, etc.
    #     alingelb   09/19/94 -  remove vms-specific stuff
    #     dpawson    07/07/93 -  add more comments regarded archive start
    #     maporter   10/29/92 -  Add vms_sga_use_gblpagfile=TRUE 
    #     jloaiza    03/07/92 -  change ALPHA to BETA 
    #     danderso   02/26/92 -  change db_block_cache_protect to _db_block_cache_p
    #     ghallmar   02/03/92 -  db_directory -> db_domain 
    #     maporter   01/12/92 -  merge changes from branch 1.8.308.1 
    #     maporter   12/21/91 -  bug 76493: Add control_files parameter 
    #     wbridge    12/03/91 -  use of %c in archive format is discouraged 
    #     ghallmar   12/02/91 -  add global_names=true, db_directory=us.acme.com 
    #     thayes     11/27/91 -  Change default for cache_clone 
    #     jloaiza    08/13/91 -         merge changes from branch 1.7.100.1 
    #     jloaiza    07/31/91 -         add debug stuff 
    #     rlim       04/29/91 -         removal of char_is_varchar2 
    #   Bridge     03/12/91 - log_allocation no longer exists
    #   Wijaya     02/05/91 - remove obsolete parameters
    #
    ##############################################################################
    # Example INIT.ORA file
    #
    # This file is provided by Oracle Corporation to help you start by providing
    # a starting point to customize your RDBMS installation for your site. 
    # 
    # NOTE: The values that are used in this file are only intended to be used
    # as a starting point. You may want to adjust/tune those values to your
    # specific hardware and needs. You may also consider using Database
    # Configuration Assistant tool (DBCA) to create INIT file and to size your
    # initial set of tablespaces based on the user input.
    ###############################################################################
    
    # Change '<ORACLE_BASE>' to point to the oracle base (the one you specify at
    # install time)
    
    db_name='ORCL'
    memory_target=1G
    processes = 150
    audit_file_dest='<ORACLE_BASE>/admin/orcl/adump'
    audit_trail ='db'
    db_block_size=8192
    db_domain=''
    db_recovery_file_dest='<ORACLE_BASE>/flash_recovery_area'
    db_recovery_file_dest_size=2G
    diagnostic_dest='<ORACLE_BASE>'
    dispatchers='(PROTOCOL=TCP) (SERVICE=ORCLXDB)'
    open_cursors=300 
    remote_login_passwordfile='EXCLUSIVE'
    undo_tablespace='UNDOTBS1'
    # You may want to ensure that control files are created on separate physical
    # devices
    control_files = (ora_control1, ora_control2)
    compatible ='11.2.0'
    View Code

    spfile文件内容:

    [oracle@oracledb dbs]$ strings spfileorcl.ora 
    
    orcl.__db_cache_size=1476395008
    
    orcl.__java_pool_size=16777216
    
    orcl.__large_pool_size=16777216
    
    orcl.__oracle_base='/usr/oracle/app'#ORACLE_BASE set from environment
    
    orcl.__pga_aggregate_target=1325400064
    
    orcl.__sga_target=1979711488
    
    orcl.__shared_io_pool_size=0
    
    orcl.__shared_pool_size=436207616
    
    orcl.__streams_pool_size=0
    
    *.audit_file_dest='/usr/oracle/app/admin/orcl/adump'
    
    *.audit_trail='db'
    
    *.compatible='11.2.0.0.0'
    
    *.control_files='/usr/oracle/app/oradata/orcl/control01.ctl','/usr/
    
    oracle/app/flash_recovery_area/orcl/control02.ctl'
    
    *.db_block_size=8192
    
    *.db_domain=''
    
    *.db_name='orcl'
    
    *.db_recovery_file_dest='/usr/oracle/app/flash_recovery_area'
    
    *.db_recovery_file_dest_size=4070572032
    
    *.diagnostic_dest='/usr/oracle/app'
    
    *.dispatchers='(PROTOCOL=TCP) (SERVICE=orclXDB)'
    
    *.memory_target=3300917248
    
    *.open_cursors=300
    
    *.processes=150
    
    *.remote_login_passwordfile='EXCLUSIVE'
    
    *.undo_tablespace='UNDOTBS1'
    
    [oracle@oracledb dbs]$
    View Code

    3:mount状态

  • 把一个数据库和启动的实例关联起来
  • 在参数文件(spfile/pfile)中找到控制文件进行读取
  • 读取控制文件,获得的数据文件和联机重做日志文件,然而,在这个时候没有进行检查以验证存在的数据文件和联机重做日志文件
  • Mounting a database includes the following tasks:

        1. Associating a database with  a previously started instance

        2. Locating and opening the control files specified in the parameter file

        3. Reading the control files to obtain the names and status of the data files and online redo log files.However,no checks are performed to verify the existence of the data files and online redo log files at this time.

    查看控制文件:

    [oracle@oracledb orcl]$ ls   /usr/oracle/app/oradata/orcl/
    control01.ctl example01.dbf redo01.log redo02.log redo03.log sysaux01.dbf system01.dbf temp01.dbf undotbs01.dbf users01.dbf

    控制文件内容

    [oracle@oracledb orcl]$ strings control01.ctl 
    }|{z
    TORCL
    }Ot5
    <Qt5@
    q5 l
    1=t5
    q5ORCL
    q5ORCL
    }Ot5
    orcl
    }Ot5
    >Qt5
    orcl
    }Ot5
    '1t5
    '1t5
    '1t5
    '1t5
    >Qt5
    >Qt5
    >Qt5
    >Qt5
    >Qt55
    >Qt5
    >Qt5
    >Qt5
    >Qt51
    >Qt5
    q5em
    }Ot5
    }Ot5
    }Ot54
    }Ot5~
    }Ot50
    q5em
    /usr/oracle/app/oradata/orcl/redo03.log
    /usr/oracle/app/oradata/orcl/redo02.log
    /usr/oracle/app/oradata/orcl/redo01.log
    /usr/oracle/app/oradata/orcl/users01.dbf
    /usr/oracle/app/oradata/orcl/undotbs01.dbf
    /usr/oracle/app/oradata/orcl/sysaux01.dbf
    /usr/oracle/app/oradata/orcl/system01.dbf
    /usr/oracle/app/oradata/orcl/temp01.dbf
    /usr/oracle/app/oradata/orcl/example01.dbf
    /usr/oracle/app/oradata/orcl/redo03.log
    /usr/oracle/app/oradata/orcl/redo02.log
    /usr/oracle/app/oradata/orcl/redo01.log
    /usr/oracle/app/oradata/orcl/users01.dbf
    /usr/oracle/app/oradata/orcl/undotbs01.dbf
    /usr/oracle/app/oradata/orcl/sysaux01.dbf
    /usr/oracle/app/oradata/orcl/system01.dbf
    /usr/oracle/app/oradata/orcl/temp01.dbf
    /usr/oracle/app/oradata/orcl/example01.dbf
    SYSTEM
    SYSAUX
    UNDOTBS1
    USERS
    TEMP
    EXAMPLE
    SYSTEM
    SYSAUX
    UNDOTBS1
    USERS
    TEMP
    EXAMPLE
    q5qA
    q5nw
    3mr5
    3mr5H6
    wr5-
    '1t5
    q5qA
    q5nw
    3mr5
    3mr5H6
    wr5-
    usr/oracle/app/oradata/orcl/example01.dbf
    /usr/oracle/app/oradata/orcl/example01.dbf
    orcl
    HbE#v
    orcl
    HbE#v
    orcl
    UNNAMED_INSTANCE_2
    UNNAMED_INSTANCE_3
    UNNAMED_INSTANCE_4
    UNNAMED_INSTANCE_5
    UNNAMED_INSTANCE_6
    UNNAMED_INSTANCE_7
    UNNAMED_INSTANCE_8
    orcl
    UNNAMED_INSTANCE_2
    UNNAMED_INSTANCE_3
    UNNAMED_INSTANCE_4
    UNNAMED_INSTANCE_5
    UNNAMED_INSTANCE_6
    UNNAMED_INSTANCE_7
    UNNAMED_INSTANCE_8
    ACM unit testing operation
    LSB Database Guard
    Supplemental Log Data DDL
    LSB Role Change Support
    RFS block and kill across RAC
    RAC-wide SGA
    ACM unit testing operation
    LSB Database Guard
    Supplemental Log Data DDL
    LSB Role Change Support
    RFS block and kill across RAC
    RAC-wide SGA
    View Code

    4、open

    打开数据库包括下列任务:

  • 打开在线数据日志文件
  • 打开联机重做日志文件
  • 如果任何数据文件或非线性重做日志文件不存在,当您试图打开的数据库,服务器返回错误。
  • 最后阶段,该服务器验证所有数据文件和联机重做日志文件可以打开并检查数据库的一致性。首先会检查数据文件头中检查点计数与控制文件中检查点计数是否一致。其次检查数据文件头的开始SCN和控制文件中记录该文件的结束SCN是否一致,如果控制文件中结束的SCN等于数据文件头中开始的SCN,说明不需要恢复。如果需要,该系统监控进程开始实例恢复。
  • 1. opening the online data log files

    2. opening the onling redo log files

    3. If any of the datafiles or noline redo log files are not present when you attempt to open the database ,the oracle server returns an error.

    4. During this final stage,the oracle server verfies that all the data files and online redo log files can be opened and checks the consistency of the database . If necessary , the SMON  background process initiates instance recovery.

    Oracle数据库关闭

    1、正常关闭

    正常关闭数据库所用的选项是normal,数据库在关闭前将检查所有的连接,并且发出命令后不允许再有新的用户连接,在等待所有连接都断开后再关闭数据库,再次启动数据库不需要任何恢复过程。

    联入sys用户

    sql>shutdown normal;

    2、紧急关闭

    该方式用在某些紧急的情况下,比如通知马上停电,此时需要紧急关闭数据库以应付这些情况。这种方式用的选项是immediate,在这种方式下并不等待所有的用户断开连接再关闭,而是由系统断开连接,然后关闭数据库。

    sql>shutdown immediate;

    一旦执行了这条命令,则将当前正在处理的sql语句马上停止,然后将所有未提交的事务回退,并且不等待当前联入数据库的用户断开连接,而是由系统强行将各个联接断开。在下次启动数据库时要执行恢复动作,不过是由系统自动执行的,用户不必去了解它。

    3、异常关闭

    异常关闭选项是abort,此种方式下系统并不做任何检查和断开用户操作以及回退操作,而是直接将数据库现场撤销,这样现场中的数据库数据当然就无效了,数据库自然也就被关掉了。

    sql>shutdown abort;

    以abort方式关闭数据库时只有一行关闭信息表示关闭了数据库现场。以abort方式关闭的数据库再次启动时必须要进行恢复动作,这些恢复操作同样是系统自动来完成的,需要的时间较长。

    原文地址:https://www.cnblogs.com/xqzt/p/4998765.html