要了解Oracle數(shù)據(jù)庫的啟動和停止需要先了解“實例”(instance)和“數(shù)據(jù)庫”(database)這兩個名詞的定義:
站在用戶的角度思考問題,與客戶深入溝通,找到新市網(wǎng)站設(shè)計與新市網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網(wǎng)站設(shè)計、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、主機(jī)域名、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋新市地區(qū)。
數(shù)據(jù)庫(database):物理操作系統(tǒng)文件或磁盤(disk)的集合。
實例(instance):一組Oracle后臺進(jìn)程/線程以及一個共享內(nèi)存區(qū),這些內(nèi)存由同一個計算機(jī)上運行的線程/進(jìn)程所共享。
這兩個詞有時可以互換使用,不過二者的概念完全不同。實例和數(shù)據(jù)庫之間的關(guān)系是:數(shù)據(jù)庫可以由多個實例mount和open,而實例可以在任何時間點mount和open一個數(shù)據(jù)庫。
Oracle System Identifier (SID)
SID是Oracle實例在服務(wù)器上的唯一名字,在UNIX和Linux機(jī)器上,Oracle用SID和Oracle home值來創(chuàng)建共享內(nèi)存的鍵值,即SID和Oracle home指定一個實例,SID也是用來定位參數(shù)文件。
有了對以上概念的認(rèn)識,下面來看Oracle數(shù)據(jù)庫的啟動和關(guān)閉過程。
1、Oracle實例和數(shù)據(jù)庫的啟動
啟動Oracle數(shù)據(jù)庫的方式有很多種,最簡單的啟動Oracle數(shù)據(jù)庫的方式是就是使用sqlplus執(zhí)行startup命令。
先來看官方給的圖:
從上圖可以看出庫Oracle從shutdown狀態(tài)到open狀態(tài)經(jīng)歷以下階段:
1) 啟動實例,不mount數(shù)據(jù)庫
實例被啟動,但還沒關(guān)聯(lián)數(shù)據(jù)庫,對應(yīng)的命令是startup nomount
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)
Reads the parameter file to determine the values of initialization parameters
Allocates the SGA based on the initialization parameter settings
Starts the Oracle background processes
Opens the alert log and trace files and writes all explicit parameter settings to the alert log in valid parameter syntax
2) 數(shù)據(jù)庫被mount
實例被啟動,打開數(shù)據(jù)庫的控制文件關(guān)聯(lián)一個數(shù)據(jù)庫。數(shù)據(jù)庫對用戶還是close狀態(tài)。對就的命令是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) 數(shù)據(jù)庫被open
實例被啟動,關(guān)聯(lián)的數(shù)據(jù)庫被open。數(shù)據(jù)庫中的數(shù)據(jù)可以被用戶訪問。對應(yīng)的命令是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實例和數(shù)據(jù)庫的關(guān)閉
通常關(guān)閉Oracle數(shù)據(jù)庫使用sqlplus執(zhí)行shutdown命令
再看官方給的圖:
從上圖中也可以看出從數(shù)據(jù)庫open狀態(tài)到shutdown狀態(tài)也經(jīng)歷三個階段:
1) 數(shù)據(jù)庫被關(guān)閉
數(shù)據(jù)庫還是mount狀態(tài),但在線數(shù)據(jù)文件和日志文件被關(guān)閉了。
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.
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) 數(shù)據(jù)庫被umount
實例是啟動的,但不再通過控制文件關(guān)聯(lián)數(shù)據(jù)庫。
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.
數(shù)據(jù)庫關(guān)閉的4種模式:ABORT、IMMEDIATE、TRANSACTIONAL、NORMAL。下面的表格介紹了各模式下數(shù)據(jù)庫的行為。
Database Behavior | ABORT | IMMEDIATE | TRANSACTIONAL | NORMAL |
---|---|---|---|---|
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數(shù)據(jù)庫的啟動和關(guān)閉過程,例子中Oracle版本為11.2.0.1
1、啟動數(shù)據(jù)庫
當(dāng)前沒有任何進(jìn)程和共享內(nèi)存,設(shè)置好ORACLE_SID和ORACLE_HOME環(huán)境變量
執(zhí)行sqlplus / as sysdba連接到一個空實例,當(dāng)前仍然沒有共享內(nèi)存,只增加了一個進(jìn)程oracletest的進(jìn)程
使用startup nomount啟動數(shù)據(jù)庫實例,該命令默認(rèn)查找spfile參數(shù)文件啟動實例,也可以使用startup nomount pfile='/dir/init.ora'指定參數(shù)文件啟動,在內(nèi)存中分配共享內(nèi)存并創(chuàng)建后臺進(jìn)程。
查看當(dāng)前的實例狀態(tài),當(dāng)前狀態(tài)只能查少量的視圖如v$instance,但大部分視圖無法查詢?nèi)鐅$database、v$datafile,會報錯:ORA-01507: database not mounted
使用alter database mount命令mount數(shù)據(jù)庫,這種狀態(tài)只能查詢部分視圖,dba開頭的大部分視圖都不能查詢會報錯:ORA-01219: database not open: queries allowed on fixed tables/views only
使用alter database open命令open數(shù)據(jù)庫:
當(dāng)前數(shù)據(jù)庫被打開,可以對外提供服務(wù)。
2、關(guān)閉數(shù)據(jù)庫
整個啟動和關(guān)閉的過程都會記錄在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
《9I10G11G編程藝術(shù) 深入數(shù)據(jù)庫體系結(jié)構(gòu) 》