阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Oracle数据库启动和关闭

109次阅读
没有评论

共计 5718 个字符,预计需要花费 15 分钟才能阅读完成。

要了解 Oracle 数据库的启动和停止需要先了解“实例”(instance)和“数据库”(database)这两个名词的定义:

  • 数据库 (database):物理操作系统文件或磁盘(disk) 的集合。

  • 实例(instance):一组 Oracle 后台进程 / 线程以及一个共享内存区,这些内存由同一个计算机上运行的线程 / 进程所共享。

这两个词有时可以互换使用,不过二者的概念完全不同。实例和数据库之间的关系是:数据库可以由多个实例 mount 和 open,而实例可以在任何时间点 mount 和 open 一个数据库。

Oracle System Identifier (SID)

SID 是 Oracle 实例在服务器上的唯一名字,在 UNIX 和 Linux 机器上,Oracle 用 SID 和 Oracle home 值来创建共享内存的键值,即 SID 和 Oracle home 指定一个实例,SID 也是用来定位参数文件。

有了对以上概念的认识,下面来看 Oracle 数据库的启动和关闭过程。

1、Oracle 实例和数据库的启动

启动 Oracle 数据库的方式有很多种,最简单的启动 Oracle 数据库的方式是就是使用 sqlplus 执行 startup 命令。

先来看官方给的图:

Oracle 数据库启动和关闭

从上图可以看出库 Oracle 从 shutdown 状态到 open 状态经历以下阶段:

1) 启动实例,不 mount 数据库

实例被启动,但还没关联数据库,对应的命令是 startup nomount

  1. Searches for a server parameter file in a platform-specific default location and, if not found, for a text initialization parameter file (specifying STARTUP with the SPFILE or PFILE parameters overrides the default behavior)

  2. Reads the parameter file to determine the values of initialization parameters

  3. Allocates the SGA based on the initialization parameter settings

  4. Starts the Oracle background processes

  5. Opens the alert log and trace files and writes all explicit parameter settings to the alert log in valid parameter syntax

2) 数据库被 mount

实例被启动,打开数据库的控制文件关联一个数据库。数据库对用户还是 close 状态。对就的命令是 alter database mount;

To mount the database, the instance obtains the names of the database control files specified in the CONTROL_FILES initialization parameter and opens the files. Oracle Database reads the control files to find the names of the data files and the online redo log files that it will attempt to access when opening the database.

In a mounted database, the database is closed and accessible only to database administrators. Administrators can keep the database closed while completing specific maintenance operations. However, the database is not available for normal operations.

3) 数据库被 open

实例被启动,关联的数据库被 open。数据库中的数据可以被用户访问。对应的命令是 alter database open。

  • Opens the online data files in tablespaces other than undo tablespaces

    If a tablespace was offline when the database was previously shut down (see “Online and Offline Tablespaces”), then the tablespace and its corresponding data files will be offline when the database reopens.

  • Acquires an undo tablespace

    If multiple undo tablespaces exists, then the UNDO_TABLESPACE initialization parameter designates the undo tablespace to use. If this parameter is not set, then the first available undo tablespace is chosen.

  • Opens the online redo log files

2、Oracle 实例和数据库的关闭

通常关闭 Oracle 数据库使用 sqlplus 执行 shutdown 命令

再看官方给的图:

Oracle 数据库启动和关闭

从上图中也可以看出从数据库 open 状态到 shutdown 状态也经历三个阶段:

1) 数据库被关闭

数据库还是 mount 状态,但在线数据文件和日志文件被关闭了。

The database close operation is implicit in a database shutdown. The nature of the operation depends on whether the database shutdown is normal or abnormal.

When a database is closed as part of a SHUTDOWN with any option other than ABORT, Oracle Database writes data in the SGA to the data files and online redo log files. Next, the database closes online data files and online redo log files. Any offline data files of offline tablespaces have been closed already. When the database reopens, any tablespace that was offline remains offline.

At this stage, the database is closed and inaccessible for normal operations. The control files remain open after a database is closed.

 

If a SHUTDOWN ABORT or abnormal termination occurs, then the instance of an open database closes and shuts down the database instantaneously. Oracle Database does not write data in the buffers of the SGA to the data files and redo log files. The subsequent reopening of the database requires instance recovery, which Oracle Database performs automatically.

2) 数据库被 umount 

实例是启动的,但不再通过控制文件关联数据库。

After the database is closed, Oracle Database unmounts the database to disassociate it from the instance. After a database is unmounted, Oracle Database closes the control files of the database. At this point, the instance remains in memory.

3) 实例被 shutdown

实例被 shutdown。

The final step in database shutdown is shutting down the instance. When the database instance is shut down, the SGA is removed from memory and the background processes are terminated.

数据库关闭的 4 种模式:ABORT、IMMEDIATE、TRANSACTIONAL、NORMAL。下面的表格介绍了各模式下数据库的行为。

Database BehaviorABORTIMMEDIATETRANSACTIONALNORMAL

Permits new user connections

No

No

No

No

Waits until current sessions end

No

No

No

Yes

Waits until current transactions end

No

No

Yes

Yes

Performs a checkpoint and closes open files

No

Yes

Yes

Yes

  • SHUTDOWN ABORT

    This mode is intended for emergency situations, such as when no other form of shutdown is successful. This mode of shutdown is the fastest. However, a subsequent open of this database may take substantially longer because instance recovery must be performed to make the data files consistent.

    Note:

    Because SHUTDOWN ABORT does not checkpoint the open data files, instance recovery is necessary before the database can reopen. The other shutdown modes do not require instance recovery before the database can reopen.

  • SHUTDOWN IMMEDIATE

    This mode is typically the fastest next to SHUTDOWN ABORT. Oracle Database terminates any executing SQL statements and disconnects users. Active transactions are terminated and uncommitted changes are rolled back.

  • SHUTDOWN TRANSACTIONAL

    This mode prevents users from starting new transactions, but waits for all current transactions to complete before shutting down. This mode can take a significant amount of time depending on the nature of the current transactions.

  • SHUTDOWN NORMAL

    This is the default mode of shutdown. The database waits for all connected users to disconnect before shutting down.

下面通过实例演示 Oracle 数据库的启动和关闭过程,例子中 Oracle 版本为 11.2.0.1

1、启动数据库

当前没有任何进程和共享内存,设置好 ORACLE_SID 和 ORACLE_HOME 环境变量

Oracle 数据库启动和关闭

执行 sqlplus / as sysdba 连接到一个空实例,当前仍然没有共享内存,只增加了一个进程 oracletest 的进程

Oracle 数据库启动和关闭

使用 startup nomount 启动数据库实例,该命令默认查找 spfile 参数文件启动实例,也可以使用 startup nomount pfile=’/dir/init.ora’ 指定参数文件启动,在内存中分配共享内存并创建后台进程。

Oracle 数据库启动和关闭查看当前的实例状态,当前状态只能查少量的视图如 v$instance,但大部分视图无法查询如 v$database、v$datafile,会报错:ORA-01507: database not mounted

Oracle 数据库启动和关闭

使用 alter database mount 命令 mount 数据库,这种状态只能查询部分视图,dba 开头的大部分视图都不能查询会报错:ORA-01219: database not open: queries allowed on fixed tables/views only

Oracle 数据库启动和关闭

使用 alter database open 命令 open 数据库:

Oracle 数据库启动和关闭

当前数据库被打开,可以对外提供服务。

2、关闭数据库

Oracle 数据库启动和关闭整个启动和关闭的过程都会记录在 alert 日志文件中。11g 的 alert 日志目录是 $ORACLE_BASE/diag/rdbms/dbname/sid/trace。文件名为 alert_sid.log。

参考:http://docs.oracle.com/cd/E11882_01/server.112/e40540/startup.htm#CNCPT89043

Oracle Database 9i/10g/11g 编程艺术:深入数据库体系结构(第 2 版)PDF  http://www.linuxidc.com/Linux/2016-02/128078.htm

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-06/144586.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计5718字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中