OCP課程26:管理Ⅰ之管理資料庫例項

stonebox1122發表於2016-01-05

課程目標:

  • 啟動和停止資料庫及元件
  • 使用EM
  • 使用SQL*PLUS訪問資料庫
  • 修改資料庫的初始化引數
  • 描述資料庫的啟動階段
  • 描述資料庫的關閉選項
  • 檢視報警日誌
  • 動態效能檢視

1、管理框架

clipboard[1]

Oracle資料庫的管理框架主要包含三個部分

  • 被管理的資料庫例項
  • 允許連線到資料庫的監聽,使用Datab Control管理資料庫一定要開啟監聽
  • 管理介面,使用Database Control管理單個資料庫,如果使用Grid Control管理多個資料庫,被管理資料庫上面需要安裝Management agent,11g預設已經安裝上了。

也就是說Enterprise Manager分為Database Control和Grid Control,Database Control只能管理單個資料庫,而Grid Control可以管理多個資料庫。

2、啟動和停止Database Control

clipboard[2]

確認監聽已經啟動。

[ ~]# su - oracle
[
~]$ lsnrctl status
LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 20-OCT-2015 22:27:13
Copyright (c) 1991, 2013, Oracle.  All rights reserved.
Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=1521))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 11.2.0.4.0 - Production
Start Date                20-OCT-2015 08:02:59
Uptime                    0 days 14 hr. 24 min. 13 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u01/app/grid/product/11.2.0/grid/network/admin/listener.ora
Listener Log File         /u01/app/grid/diag/tnslsnr/oracletest1/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=oracletest1)(PORT=1521)))
Services Summary...
Service "+ASM" has 1 instance(s).
  Instance "+ASM", status READY, has 1 handler(s) for this service...
Service "stone" has 1 instance(s).
  Instance "stone", status READY, has 1 handler(s) for this service...
Service "stoneXDB" has 1 instance(s).
  Instance "stone", status READY, has 1 handler(s) for this service...
The command completed successfully

確認資料庫已經啟動。
[
~]$ sqlplus /nolog
SQL*Plus: Release 11.2.0.4.0 Production on Tue Oct 20 22:27:18 2015
Copyright (c) 1982, 2013, Oracle.  All rights reserved.
SQL> conn / as sysdba
Connected.
SQL> select open_mode from v$database;
OPEN_MODE
--------------------
READ WRITE
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options

設定SID,指定針對哪一個資料庫進行管理
[
~]$ . oraenv
ORACLE_SID = [stone] ? stone
The Oracle base remains unchanged with value /u01/app/oracle
[
~]$ echo $ORACLE_SID
stone
[
~]$ echo $ORACLE_BASE
/u01/app/oracle

直接輸入emctl目錄回車會顯示該命令的用法。
[
~]$ emctl
Oracle Enterprise Manager 11g Database Control Release 11.2.0.4.0
Copyright (c) 1996, 2013 Oracle Corporation.  All rights reserved.
   Oracle Enterprise Manager 10g Database Control commands:
        emctl start | stop dbconsole
        emctl status | secure | setpasswd dbconsole
        emctl config dbconsole -heap_size -max_perm_size
       emctl status agent
       emctl status agent -secure [-omsurl <
*>]
       emctl getversion
       emctl reload | upload | clearstate | getversion agent
       emctl reload agent dynamicproperties [:]....
       emctl config agent
       emctl config agent updateTZ
       emctl config agent getTZ
       emctl resetTZ agent
       emctl config agent credentials [[:]]
       emctl gensudoprops
       emctl clearsudoprops
Blackout Usage :
       emctl start blackout [-nodeLevel] [[:]].... [-d ]
       emctl stop blackout
       emctl status blackout [[:]]....
The following are valid options for blackouts
defaults to local node target if not specified.
If -nodeLevel is specified after ,the blackout will be applied to all targets and any target list that follows will be ignored.
Duration is specified in [days] hh:mm
        emctl getemhome
        emctl ilint
Em Key Commands Usage :
emctl config emkey -emkeyfile [-force] [-sysman_pwd ]
emctl config emkey -emkey [-emkeyfile ] [-force] [-sysman_pwd ]
emctl config emkey -repos [-emkeyfile ] [-force] [-sysman_pwd ]
emctl config emkey -remove_from_repos [-sysman_pwd ]
emctl config emkey -copy_to_repos [-sysman_pwd ]
emctl status emkey [-sysman_pwd ]
Secure DBConsole Usage :
emctl secure dbconsole -sysman_pwd [-passwd_file ]
        [-host ] [-sid ] [-reset] [-secure_port ]
        [-cipher_suites ] [-sign_alg ]
        [-root_dc ] [-root_country ] [-root_state ] [-root_loc ]
        [-root_org ] [-root_unit ] [-root_email ]
        [-wallet ] [-wallet_pwd ] [-trust_certs_loc ]
emctl secure status dbconsole
Register Targettype Usage :
emctl register oms targettype [-o ] OR
emctl register oms targettype [-o ]
檢視em的狀態。
[
~]$ emctl status dbconsole
Oracle Enterprise Manager 11g Database Control Release 11.2.0.4.0
Copyright (c) 1996, 2013 Oracle Corporation.  All rights reserved.

Oracle Enterprise Manager 11g is running.
------------------------------------------------------------------
Logs are generated in directory /u01/app/oracle/product/11.2.0/dbhome_1/oracletest1_stone/sysman/log

EM主要是DBA使用,如果不使用可以把他關掉。

[ ~]$ emctl stop dbconsole
Oracle Enterprise Manager 11g Database Control Release 11.2.0.4.0
Copyright (c) 1996, 2013 Oracle Corporation.  All rights reserved.

Stopping Oracle Enterprise Manager 11g Database Control ...
...  Stopped.
[
~]$ emctl status dbconsole
Oracle Enterprise Manager 11g Database Control Release 11.2.0.4.0
Copyright (c) 1996, 2013 Oracle Corporation.  All rights reserved.

Oracle Enterprise Manager 11g is not running.

啟動em。
[
~]$ emctl start dbconsole
Oracle Enterprise Manager 11g Database Control Release 11.2.0.4.0
Copyright (c) 1996, 2013 Oracle Corporation.  All rights reserved.

Starting Oracle Enterprise Manager 11g Database Control ......... started.
------------------------------------------------------------------
Logs are generated in directory /u01/app/oracle/product/11.2.0/dbhome_1/oracletest1_stone/sysman/log

[ ~]$ emctl status dbconsole
Oracle Enterprise Manager 11g Database Control Release 11.2.0.4.0
Copyright (c) 1996, 2013 Oracle Corporation.  All rights reserved.

Oracle Enterprise Manager 11g is running.
------------------------------------------------------------------
Logs are generated in directory /u01/app/oracle/product/11.2.0/dbhome_1/oracletest1_stone/sysman/log

客戶端瀏覽器直接使用訪問不了,因為沒有使用DNS解析這個主機名,可以使用IP地址訪問https://192.168.230.139:1158/em/console,如果一定要使用機器名進行訪問,可以修改C:\Windows\System32\drivers\etc目錄下的hosts檔案,增加IP地址及對應的機器名即可。

clipboard[3]

輸入使用者名稱,這裡的使用者名稱是要具有管理資料字典許可權的使用者,預設為sys,sysman和system。例如使用HR就不行,使用sys登入,選擇連線身份為sysdba。

使用EM可以檢視告警資訊及效能走勢圖,建立和修改物件,執行備份和恢復等。下面為EM的主頁。

主頁上看到有資料庫的一些資訊,狀態,什麼時候啟動的,例項名稱,版本,主機,監聽,有沒有ASM,主機CPU情況,活動會話情況,響應時間,有沒有報警,空間,高可用,還可以看效能,可用性,伺服器,模式等等,後面會詳細介紹。

clipboard[4]


3、其他Oracle工具

clipboard[5]

SQL*Plus:

  • 執行資料庫管理操作
  • 執行SQL命令對資料庫進行增刪改查

SQL Developer:

  • 訪問資料庫例項的影像介面
  • 支援SQL和PL/SQL的開發
  • 跟隨Oracle預設安裝

管理I主要使用EM和SQL*Plus。


4、SQL*Plus

SQL*Plus是一個命令列工具,既可以執行在交換模式也可以執行在批處理模式。

可以使用命令列介面:

  • 輸入,編輯,執行,儲存,查詢和儲存SQL命令及PL/SQL塊
  • 格式化,計算,儲存和列印查詢結果
  • 列出表的欄位定義
  • 執行資料庫管理

啟動SQL*Plus步驟:

(1)開啟一個終端

(2)輸入 sqlplus / or /nolog

(3)如果使用/nolog選項,則需要使用 connect 進行連線

(4)輸入密碼

(1)可以在shell指令碼或者批處理呼叫SQL*Plus。

例子:編寫一個shell指令碼,呼叫SQL*Plus進行查詢

[ ~]$ vi mybat.sh

#!/bin/bash

sqlplus hr/hr<<eof </eof<>

select count(*) from employees;

select employee_id,last_name,salary from employees where employee_id=100;

quit

eof

[ ~]$ sh mybat.sh

SQL*Plus: Release 11.2.0.4.0 Production on Mon Dec 14 16:56:18 2015

Copyright (c) 1982, 2013, Oracle.  All rights reserved.

Connected to:

Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

With the Partitioning, Automatic Storage Management, OLAP, Data Mining

and Real Application Testing options

SQL>

  COUNT(*)

----------

       107

SQL>

EMPLOYEE_ID LAST_NAME                     SALARY

----------- ------------------------- ----------

        100 King                           24000

SQL> Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

With the Partitioning, Automatic Storage Management, OLAP, Data Mining

and Real Application Testing options

實際當中的運維就是這樣,寫一些管理指令碼,獲取資料庫當中的關鍵資訊,比如資料庫狀態,表空間等,自動執行,然後發郵件。

(2)還可以在sqlplus命令後面直接執行sql指令碼。

[ ~]$ vim mytest.sql

select * from departments where location_id=1400;

quit

[ ~]$ sqlplus hr/hr @mytest.sql

SQL*Plus: Release 11.2.0.4.0 Production on Mon Dec 14 17:01:48 2015

Copyright (c) 1982, 2013, Oracle.  All rights reserved.

Connected to:

Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

With the Partitioning, Automatic Storage Management, OLAP, Data Mining

and Real Application Testing options

DEPARTMENT_ID DEPARTMENT_NAME                MANAGER_ID LOCATION_ID

------------- ------------------------------ ---------- -----------

           60 IT                                    103        1400

Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

With the Partitioning, Automatic Storage Management, OLAP, Data Mining

and Real Application Testing options

(3)也可以進入sqlplus後執行指令碼。

[ ~]$ sqlplus hr/hr

SQL*Plus: Release 11.2.0.4.0 Production on Mon Dec 14 17:05:53 2015

Copyright (c) 1982, 2013, Oracle.  All rights reserved.

Connected to:

Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

With the Partitioning, Automatic Storage Management, OLAP, Data Mining

and Real Application Testing options

SQL> @mytest.sql

DEPARTMENT_ID DEPARTMENT_NAME                MANAGER_ID LOCATION_ID

------------- ------------------------------ ---------- -----------

           60 IT                                    103        1400

Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

With the Partitioning, Automatic Storage Management, OLAP, Data Mining

and Real Application Testing options

(4)sqlplus裡面執行作業系統的命令

SQL> ! ls -ltr

SQL> get mytest.sql

SQL> @?/rdbms/admin/awrrpt.sql

其中?表示$ORACLE_HOME,所有的作業系統通用,$ORACLE_HOME/rdbms/admin/這個目錄是Oracle的建立資料庫的指令碼及管理指令碼。

SQL> !ls -ltr $ORACLE_HOME/rdbms/admin/


5、初始化引數檔案及引數

(1)初始化引數檔案

clipboard[6]

當啟動資料庫的時候,會去讀初始化引數檔案。有2種型別的引數檔案

  • Server Parameter File(spfile):優先讀取,二進位制檔案,只能由資料庫進行讀寫,不能進行手動編輯,位於Oracle例項所在伺服器上,不受資料庫關閉的影響,預設的名稱為spfile.ora,Oracle在啟動的時候會自動去找這個名稱的檔案。
  • Text initialization parameter file:文字檔案,資料庫只能讀取,不能寫入,可以使用文字編輯軟體手動進行編輯,不受資料庫關閉的影響,預設的名稱為init.ora,如果Oracle在啟動的時候沒有找到spfile,則會自動找這個名稱的檔案。

Oracle推薦使用spfile維護初始化引數。

注意:在Linux下,Oracle會在$ORACLE_HOEM/dbs目錄下搜尋引數檔案,如果使用ASM,則spfile位於ASM磁碟組上,這種情況下,init.ora必須位於$ORACLE_HOEM/dbs目錄下,並在其中指定spfile的位置。

初始化引數值型別:

  • Boolean
  • String
  • Integer
  • Parameter file
  • Reserved
  • Big Integer

Derived Parameter Values

有一些引數的值是根據另外引數的值計算過來的,最好不要直接去修改這些引數的值,如果去修改,就會覆蓋掉計算的值。例如sessions這個引數的值是根據processes這個引數計算過來的,如果processes變化了,則sessions也會跟著變化。

Operating System-Dependent Parameter Values

有一些引數的有效值或者取值範圍依賴於作業系統。例如DB_FILE_MULTIBLOCK_READ_COUNT引數指定在一次連續掃描中單次I/O操作可以讀取的最大塊數量,這個引數取決於作業系統平臺。

初始化引數分為基本和高階2類,在大多數情況下,只需要設定和調整大約30個基本引數就可以使資料庫獲得合理的效能。在極少數情況才需要調整高階引數獲取最佳化的效能。Oracle 11gR2大約有314個高階引數。

例子:檢視當前資料庫的基本引數

SQL> select name,value from v$parameter where isbasic='TRUE';

NAME                           VALUE

------------------------------ ----------------------------------------------------------------------

processes                      300

sessions                       472

nls_language                   AMERICAN

nls_territory                  AMERICA

sga_target                     0

control_files                  +DATA/stone/controlfile/current.260.893370773, +FRA/stone/controlfile/

                               current.256.893370773

db_block_size                  8192

compatible                     11.2.0.4.0

log_archive_dest_1

NAME                           VALUE

------------------------------ ----------------------------------------------------------------------

log_archive_dest_2

log_archive_dest_state_1       enable

log_archive_dest_state_2       enable

cluster_database               FALSE

db_create_file_dest            +DATA

db_create_online_log_dest_1

db_create_online_log_dest_2

db_recovery_file_dest          +FRA

db_recovery_file_dest_size     4236247040

undo_tablespace                UNDOTBS1

instance_number                0

NAME                           VALUE

------------------------------ ----------------------------------------------------------------------

ldap_directory_sysauth         no

remote_login_passwordfile      EXCLUSIVE

db_domain

shared_servers                 1

remote_listener

db_name                        stone

db_unique_name                 stone

open_cursors                   300

star_transformation_enabled    FALSE

pga_aggregate_target           0

30 rows selected.

(2)初始化引數

常用的基本引數有如下:

  • DB_NAME和DB_DOMAIN:組成全域性資料庫名
  • DB_RECOVERY_FILE_DEST和DB_RECOVERY_FILE_DEST_SIZE:指定快速恢復區位置和大小
  • SGA_TARGET:指定SGA的總大小
  • UNDO_TABLESPACE:指定undo表空間
  • COMPATIBLE:啟用或者禁用影響磁碟檔案格式的資料庫特性
  • CONTROL_FILES:控制檔名稱,Oracle建議多路複用控制檔案,可以從1到8個,不同平臺預設值不同
  • DB_FILES:資料檔案的最大數量,預設值為200
  • PROCESSES:同時連線資料庫的作業系統使用者程式最大數量,包括後臺程式,預設值為100。
  • DB_BLOCK_SIZE:所有表空間使用的標準資料庫塊大小,建立資料庫時指定,後面不能更改,範圍從2k到32k,預設為8k
  • DB_CACHE_SIZE:標準塊緩衝區快取大小,如果設定了SGA_TARGET,則預設值為0。最小16MB,一般為4MB*cpu_count
  • SGA_TARGET:指定了SGA的總大小,如果設定了SGA,則下面的引數都為0,表示由系統自動調整大小,如果手動設定了非零值,則為最小值。
    • Buffer cache (DB_CACHE_SIZE)
    • Shared pool (SHARED_POOL_SIZE)
    • Large pool (LARGE_POOL_SIZE)
    • Java pool (JAVA_POOL_SIZE)
    • Streams pool (STREAMS_POOL_SIZE)
  • MEMORY_TARGET:指定Oracle可用的記憶體
    • 在文字初始化引數檔案init.ora中,如果只設定了MEMORY_TARGET,沒有設定MEMORY_MAX_TARGET,則資料庫自動設定MEMORY_MAX_TARGET的值為MEMORY_TARGET的值;如果只設定了MEMORY_MAX_TARGET,沒有設定MEMORY_TARGET,則MEMORY_TARGET的值預設為0。
    • 資料庫啟動後,可以動態調整MEMORY_TARGET為非零值,但不能超過MEMORY_MAX_TARGET。可以使用ALTER SYSTEM對MEMORY_TARGET進行修改,範圍從152MB到MEMORY_MAX_TARGET。
  • PGA_AGGREGATE_TARGET:分配給所有伺服器程式的PGA的總大小,不是在SGA中的,Oracle可用總記憶體減去SGA就是PGA,取值範圍為10MB到4096GB-1,預設值為10MB或者SGA的20%
  • SHARED_POOL_SIZE:共享池大小,如果設定了SGA,則預設為0,否則32位系統為48MB,64位系統為128MB
  • UNDO_MANAGEMENT:指定undo表空間管理方式,如果設定為auto,則使用Automatic Undo Management管理方式,使用undo表空間進行undo空間分配,否則使用回滾段進行undo空間分配。可以設定為auto或者manual,如果沒有指定,則預設為auto

(3)使用SQL*Plus檢視引數

例子:使用v$parameter檢視當前會話的當前引數值。

SQL> select name,value from v$parameter;

NAME                           VALUE

------------------------------ ----------------------------------------------------------------------

lock_name_space

processes                      300

sessions                       472

timed_statistics               TRUE

timed_os_statistics            0

resource_limit                 FALSE

。。。

cell_offloadgroup_name

352 rows selected.

例子:使用show parameter檢視包含某個字串的引數

SQL> show parameter shared_pool_size

NAME                                 TYPE        VALUE

------------------------------------ ----------- ------------------------------

shared_pool_size                     big integer 0

SQL> show parameter para

NAME                                 TYPE        VALUE

------------------------------------ ----------- ------------------------------

cell_offload_parameters              string

fast_start_parallel_rollback         string      LOW

parallel_adaptive_multi_user         boolean     TRUE

關於初始化引數的其他資料字典檢視:

  • V$SPPARAMETER:顯示伺服器引數檔案的內容,如果沒有使用伺服器引數檔案啟動例項,則ISSPECIFIED欄位的記錄都為FALSE
  • V$SYSTEM_PARAMETER:當前生效的初始化引數資訊

(4)修改初始化引數

clipboard[7]

兩種初始化引數型別:

  • Static parameters(靜態引數):影響例項或者整個資料庫,只能透過修改init.ora或者SPFILE檔案,需要資料庫重啟才能生效,大約有110個。
  • Dynamic parameters(動態引數):可以在資料庫中使用alter session或者alter system進行修改,大約有234個,分為2類
    • session-level parameters:隻影響當前使用者會話,例如nls引數,會話結束就失效了
    • system-level parameters:影響整個資料庫和所有會話,例如sga_target或者歸檔位置,需要使用scope選項,如果要讓引數永久生效,就需要指定scope=both將引數設定新增到spfile中去

使用alter system設定或者修改初始化引數的值,scope選項指定修改的範圍

  • scope=spfile:只對伺服器引數檔案spfile進行修改,不對當前例項進行修改,在下次啟動時永久生效,只能用於靜態引數
  • scope=memory:只對記憶體中的引數進行修改,當前例項立即生效,對於動態引數,不會去更新spfile,所以不會永久有效,靜態引數不能使用這個選項
  • scope=both:既修改伺服器引數檔案也修改記憶體中的引數,當前例項立即生效,對於動態引數,會去更新spfile,所以永久有效,靜態引數不能使用這個選項

如果例項沒有使用spfile啟動,則指定scope=spfile或者scope=both會報錯。如果使用spfile啟動例項,則預設選項是scope=both,如果使用文字引數檔案啟動例項,則預設選項是scope=memory。

對於某些動態引數,還可以指定deferred關鍵字,表示以後的會話才生效。deferred對以下引數有效:

  • backup_tape_io_slaves
  • recyclebin
  • audit_file_dest
  • object_cache_optimal_size
  • object_cache_max_size_percent
  • sort_area_size
  • sort_area_retained_size
  • olap_page_pool_size

欄位

欄位值

說明

isses_modifiable

true

表示這個引數可以使用alter session修改

false

表示不能使用alter session命令修改

issys_modifiable

immediate

表示這次對這個引數的修改會在當前所有會話中就"立即"發生作用, 即修改立即生效.

deferred

表示這次修改對當前會話不發生作用, 在以後開啟的會話中起作用, 故它有"推遲"影響的效果. 修改該引數值時需要使用alter system set...deferred. 如果不加deferred關鍵字則報錯ORA-02096: specified initialization parameter is not modifiable with this option.

false

表示不能使用alter system命令修改, 只能alter system ...... scope=spfile

例子:在會話級別修改nls_date_format引數

SQL> select sysdate from dual;

SYSDATE

------------

14-DEC-15

SQL> alter session set nls_date_format='mon dd yyyy';

Session altered.

SQL> select sysdate from dual;

SYSDATE

--------------

dec 14 2015

例子:修改系統引數sec_max_failed_login_attempts並加上註釋,下次重啟生效

SQL> alter system set sec_max_failed_login_attempts=8 comment='Reduce from 10 for tighter security.' scope=spfile;

System altered.

(5)使用EM檢視資料庫的引數

clipboard[8]

所有靜態引數

clipboard[9]

基本靜態引數

clipboard[10]

基本動態引數

clipboard[11]

(6)使用EM檢視當前會話的引數

基本靜態引數

clipboard[12]

基本動態引數

clipboard[13]


6、啟動和關閉資料庫

使用EM關閉資料庫,需要輸入作業系統的使用者名稱和密碼以及資料庫的使用者名稱和密碼,使用關閉資料庫的預設選項immediate。

clipboard[14]

clipboard[15]

clipboard[16]

clipboard[17]

clipboard[18]

可以使用EM,srvctl或者sql*plus啟動資料庫。這裡使用https://192.168.230.139:1158/em登入EM,輸入作業系統使用者名稱和密碼,點選“繼續”,然後點選“啟動”。

clipboard[19]

clipboard[20]

輸入作業系統的使用者名稱和密碼以及資料庫的使用者名稱和密碼

clipboard[21]

選擇“啟動資料庫和從屬資源”。

clipboard[22]

clipboard[23]

clipboard[24]

clipboard[25]


7、啟動資料庫例項的三種模式

(1)nomount

clipboard[26]

在資料庫建立、重建控制檔案或者某些備份和恢復時候,需要將例項啟動到nomount模式

啟動例項到nomount包括下面的內容:

  • 在$ORACLE_HOME/dbs目錄下按順序查詢查詢下面檔案:
    • spfile.ora
    • spfile.ora
    • init.ora
  • 分配SGA
  • 啟動後臺程式
  • 開啟alter_.log和trace檔案

(2)mount

clipboard[27]

mount資料庫包括下面的內容:

  • 從引數檔案中找到控制檔案的位置並開啟
  • 從控制檔案中讀取資料檔案和聯機重做日誌檔案的檔名和狀態,但是不去驗證資料檔案和聯機重做日誌檔案是否存在

可以在mount狀態執行特定的維護操作,比如:

  • 重新命名資料檔案
  • 修改資料庫的歸檔模式
  • 執行完全的資料庫恢復

(3)open

clipboard[28]

開啟資料庫包括:

  • 開啟資料檔案
  • 開啟聯機重做日誌檔案

任何一個資料檔案或者聯機重做日誌檔案不存在將會返回錯誤。Oracle驗證所有資料檔案和聯機重做日誌檔案,檢查他們的一致性。

例子:啟動資料庫的選項

SQL> startup

ORACLE instance started.

Total System Global Area  835104768 bytes

Fixed Size                  2257840 bytes

Variable Size             549456976 bytes

Database Buffers          281018368 bytes

Redo Buffers                2371584 bytes

Database mounted.

Database opened.

SQL> startup nomount

ORACLE instance started.

Total System Global Area  835104768 bytes

Fixed Size                  2257840 bytes

Variable Size             549456976 bytes

Database Buffers          281018368 bytes

Redo Buffers                2371584 bytes

SQL> alter database mount;

Database altered.

SQL> alter database open;

Database altered.

[ ~]$ srvctl stop database -d stone

[ ~]$ srvctl status database -d stone

Database is not running.

[ ~]$ srvctl start database -d stone -o mount

[ ~]$ srvctl status database -d stone

Database is running.


8、關閉資料庫的選項

clipboard[29]

關閉資料庫有四個選項:

  • abort:下次啟動需要例項恢復,如非必要,不要使用,其他選項都不起作用了才用
  • immediate:最常用的選項,回滾沒有提交的事務,斷開所有連線,下次啟動無需例項恢復,EM預設關閉選項
  • transactional:允許完成現有事務,但不能開始新的事務,下次啟動無需例項恢復
  • normal:等待會話斷開,不能建立新的連線,下次啟動無需例項恢復,預設的關閉選項

例子:關閉資料庫選項

SQL> shutdown immediate

Database closed.

Database dismounted.

ORACLE instance shut down.

SQL> shutdown abort

ORACLE instance shut down.

[ ~]$ srvctl stop database -d stone -o abort


9、檢視告警日誌

clipboard[30]

每個資料庫都有一個名字為alert_.ora的告警日誌檔案,如果設定了$ORACLE_BASE,則預設位於$ORACLE_BASE/diag/rdbms///trace目錄下。

告警檔案按照時間先後順序記錄如下資訊:

  • 使用非預設的初始化引數啟動資料庫
  • 所有的內部錯誤(ORA-600),塊錯誤(ORA-1578),死鎖錯誤(ORA-60)
  • 管理操作,例如使用SQL語句建立,修改,刪除資料庫和表空間,使用EM或者SQL*Plus啟動、關閉資料庫,歸檔日誌以及恢復資料庫
  • 專有服務模式或者共享服務模式排程程式的一些資訊和錯誤
  • 物化檢視自動重新整理錯誤

由於告警檔案的大小會不斷增長,所以需要經常檢視,定期備份並刪除。當資料庫重新去寫告警檔案的時候,會自動重新建立一個。

Oracle還有一個XML版本的告警檔案,位於$ORACLE_BASE/diag/rdbms///alert目錄下。

例子:檢視告警日誌檔案位置

SQL> select * from v$diag_info;

   INST_ID NAME                      VALUE

---------- ------------------------- ----------------------------------------------------------------------

         1 Diag Enabled              TRUE

         1 ADR Base                  /u01/app/oracle

         1 ADR Home                  /u01/app/oracle/diag/rdbms/stone/stone

         1 Diag Trace                /u01/app/oracle/diag/rdbms/stone/stone/trace

         1 Diag Alert                /u01/app/oracle/diag/rdbms/stone/stone/alert

         1 Diag Incident             /u01/app/oracle/diag/rdbms/stone/stone/incident

         1 Diag Cdump                /u01/app/oracle/diag/rdbms/stone/stone/cdump

         1 Health Monitor            /u01/app/oracle/diag/rdbms/stone/stone/hm

         1 Default Trace File        /u01/app/oracle/diag/rdbms/stone/stone/trace/stone_ora_63954.trc

         1 Active Problem Count      0

         1 Active Incident Count     0

11 rows selected.

例子:使用tail檢視告警日誌

[ ~]$ tail -10 /u01/app/oracle/diag/rdbms/stone/stone/trace/alert_stone.log

Warning: VKTM detected a time drift.

Time drifts can result in an unexpected behavior such as time-outs. Please check trace file for more details.

Tue Dec 15 08:14:22 2015

Starting background process SMCO

Tue Dec 15 08:14:25 2015

SMCO started with pid=21, OS id=62385

Tue Dec 15 08:14:33 2015

Closing scheduler window

Closing Resource Manager plan via scheduler window

Clearing Resource Manager plan via parameter

例子:使用EM檢視告警日誌

clipboard[31]

clipboard[32]


10、使用跟蹤檔案(trace file)

clipboard[33]

每一個伺服器程式和後臺程式都會寫入到相關的跟蹤檔案中,當程式檢測到一個內部錯誤,會將錯誤資訊dump到跟蹤檔案。一般後臺程式產生的跟蹤檔案的檔名都包含有這個程式的名字。我們可以透過跟蹤檔案去調整應用或者例項。可以使用tkprof命令來閱讀跟蹤檔案。

從11g開始,Oracle提供了一個全新的系統級診斷資訊架構,稱之為自動診斷知識庫ADR(Automatic diagnostic repository),統一管理Oracle所有的診斷資訊,包括跟蹤檔案,告警日誌,健康監控報告等,所以位於$ORACLE_HOME之外。

ADR的根目錄稱之為ADR base,由引數DIAGNOSTIC_DEST設定,如果沒有設定該引數,則:

  • 如果設定了ORACLE_BASE,則DIAGNOSTIC_DEST就為ORACLE_BASE
  • 如果沒有設定ORACLE_BASE,則DIAGNOSTIC_DEST為ORACLE_HOME/log

某種型別的診斷資訊所在目錄稱之為ADR home,在ADR base目錄下,具體路徑為./diag/product_type/db_id/instance_id

例子:檢視引數DIAGNOSTIC_DEST

SQL> show parameter diagnostic_dest

NAME                                 TYPE        VALUE

------------------------------------ ----------- ------------------------------

diagnostic_dest                      string      /u01/app/oracle


11、動態效能檢視

clipboard[34]

動態效能檢視是基於資料庫伺服器記憶體結構建立的的虛擬表,其中一些可以在資料庫mount或者open之前就可以檢視。可以使用dict檢視檢視,一般以“v$”開頭,超過590個,動態效能檢視包括以下資訊:

  • 會話
  • 檔案狀態
  • 備份狀態
  • 記憶體使用和分配
  • 系統和會話引數
  • SQL執行
  • 統計資訊和指標

例子:透過v$sql檢視sql執行時間超過2000ms的sql語句及執行次數

SQL> select sql_text,executions from v$sql where cpu_time>2000000;

SQL_TEXT                                                                                             EXECUTIONS

---------------------------------------------------------------------------------------------------- ----------

BEGIN EMDW_LOG.set_context(MGMT_JOB_ENGINE.MODULE_NAME, :1); MGMT_JOB_ENGINE.get_scheduled_steps(:2,       1936

:3, :4, :5); EMDW_LOG.set_context; END;

例子:透過v$session檢視檢視從昨天開始透過機器“WORKGROUP\SHILEI-PC”連線的資料庫的會話資訊

SQL> select sid,serial#,machine,logon_time from v$session where machine='WORKGROUP\SHILEI-PC' and logon_time>sysdate-1;

       SID    SERIAL# MACHINE                                                          LOGON_TIME

---------- ---------- ---------------------------------------------------------------- ------------

        70        115 WORKGROUP\SHILEI-PC                                              15-DEC-15

例子:透過v$lock檢視檢視阻塞其他會話的會話sid及持有鎖的時間

在會話1的sys使用者下執行

SQL> select userenv('sid') from dual;

USERENV('SID')

--------------

            46

SQL> update hr.employees set salary=10000 where employee_id=100;

1 row updated.

在會話2的sys使用者下執行

SQL> select userenv('sid') from dual;

USERENV('SID')

--------------

            73

SQL> update hr.employees set salary=10000 where employee_id=100;

在會話3的sys使用者執行

SQL> select sid,ctime from v$lock where block>0;

       SID      CTIME

---------- ----------

        46         20

還可以具體檢視阻塞了哪一個會話

SQL> select a.sid,b.sid

  2  from v$lock a,v$lock b

  3  where b.id1=a.id1 and a.id2=b.id2

  4  and a.sid<>b.sid

  5  and b.request<>0;

       SID        SID

---------- ----------

        46         73

動態效能檢視使用指導:

  • 動態效能檢視的所有者是sys使用者
  • 在例項啟動,mount和open階段對應不同的檢視
  • 可以透過V$FIXED_TABLE檢視所有檢視的名字
  • 以v$開頭,是動態的,不保證讀一致性

12、資料字典檢視

在前面SQL的章節已經講過了。

clipboard[35]

例子:hr使用者使用user_tables檢視檢視錶名及所在表空間

SQL> select table_name,tablespace_name from user_tables;

TABLE_NAME                     TABLESPACE_NAME

------------------------------ ------------------------------

LOCATIONS                      EXAMPLE

DEPARTMENTS                    EXAMPLE

JOBS                           EXAMPLE

COUNTRIES

EMPLOYEES                      EXAMPLE

JOB_HISTORY                    EXAMPLE

REGIONS                        EXAMPLE

7 rows selected.

例子:sys使用者使用all_sequences檢視相關使用者的序列

SQL> select sequence_name,min_value,max_value,increment_by

  2  from all_sequences

  3  where sequence_owner in ('MDSYS','XDB');

SEQUENCE_NAME                   MIN_VALUE  MAX_VALUE INCREMENT_BY

------------------------------ ---------- ---------- ------------

FT_SQ$                                  1 1.0000E+28            1

NTV2_SEQUENCE                           1 1.0000E+28            1

例子:sys使用者使用dba_users檢視當前使用者狀態為open的使用者

SQL> select username,account_status from dba_users where account_status='OPEN';

USERNAME                       ACCOUNT_STATUS

------------------------------ --------------------------------

SYSTEM                         OPEN

SYS                            OPEN

SYSMAN                         OPEN

HR                             OPEN

DBSNMP                         OPEN

MGMT_VIEW                      OPEN

6 rows selected.

13、相關習題:

(1)You want to check the details of few errors that users have reported. You search for the alert log file and execute few commands to find the location of the alert log file.   View the Exhibit and check the commands executed.   What is the location of the alert_orcl.log file?

Exhibit:

clipboard[36]

A.  ORACLE_HOME/dbs
B.  ORACEL_HOME/rdbms
C.  /u01/app/oracle/admin/orcl/adump
D.  /u01/app/oracle/flash_recovery_area
E.  ORACLE_BASE/diag/rdbms/orcl/alert
F.  ORACLE_BASE/diag/rdbms/orcl/orcl/trace

答案:F

(2)Note the functionalities of various background processes:

1. Perform recovery at instance startup.

2. Free the resources used by a user process when it fails.

3. Cleanup the database buffer cache when a process fails.

4. Dynamically register database services with listeners.

5. Monitor sessions for idle session timeout.

6. Cleanup unused temporary segments.

7. Record the checkpoint information in control file.

Which option has the correct functionalities listed for a background process?

A.Checkpoint (CKPT): 1, 2, 5

B.System Monitor (SMON): 1, 6

C.Process Monitor (PMON): 4, 6, 7

D.Database Writer (DBWR): 1, 3, 4

答案:B

(3)Identify the two situations in which the alert log file is updated with details. (Choose two.)
A. Running a query on a table returns "ORA-600: Internal Error"
B. Inserting a value in a table returns "ORA-01722: Invalid Number"
C. Creating a table returns "ORA-00955: name is already used by an existing object'
D. Inserting a value in a table returns "ORA-00001: unique constraint (SYS.PK_TECHP) violated."
E. Rebuilding an index using ALTER INDEX ... REBUILD fails with an error "ORA-01578: ORACLE data block corrupted (file # 14, block @ 50)."

答案:AE

(4)Which statements listed below describe the data dictionary views?
   1. These are stored in the SYSTEM tablespace
   2. These are the based on the virtual tables
   3. These are owned by the SYS user
   4. These can be queried by a normal user only if O7_DICTIONARY_ACCESSIBLILITY parameter is set to TRUE
   5. The V$FIXED_TABLE view can be queried to list the names of these views
A.  1 and 3
B.  2,3 and 5
C.  1,2, and 5
D.  2,3,4 and 5

答案:A

(5)You have an ORDERS table with the following structure:
   Name                      Null?             Type
   ------------------------- -------- -------------------------
   OID                        NUMBER(6)
   ODATE                      DATE
   CCODE                      NUMBER(6)
   OAMT                      NUMBER(10,2)
The table has data in the ODATE column for all rows. Many orders are placed in a single day. You need to ensure that the ODATE column must contain data for every order in future.
Which method would serve the purpose?
A.  Modify the column using the ALTER TABLE ... MODIFY command.
B.  Add a UNIQUE constraint to the column using the ALTER TABLE ... ADD CONSTRAINT command.
C.  Add a NOT NULL constraint to the column using the ALTER TABLE ... ADD CONSTRAINT command.
D.  Add a PRIMARY KEY constraint to the column using the ALTER TABLE ... ADD CONSTRAINT command.

答案:A

(6)You have two tables with referential integrity enforced between them. You need to insert data to the child table first because it is going to be a long transaction and data for the parent table will be available in a later stage, which can be inserted as part of the same transaction.
View the Exhibit to examine the commands used to create tables.
Which action would you take to delay the referential integrity checking until the end of the transaction?
clipboard[37]
A.  Set the constraint to deferred before starting the transaction
B.  Alter the constraint to NOVALIDATE state before starting the transaction
C.  Enable the resumable mode for the session before starting the transaction
D.  Set the COMMIT_WAIT parameter to FORCE_WAIT for the session before starting the transaction

答案:A

(7)Automatic Shared Memory Management is disabled for your database instance.  You  realize that  there  are  cases  of  SQL  statements  performing  poorly  because  of  repeated  parsing activity, resulting in degradation of performance.
What would be your next step to improve performance?
A.  Run the SQL Access Advisor
B.  Run the memory Advisor for the SGA
C.  Run the memory Advisor for the PGA
D.  Run the memory advisor for the shared pool
E.  Run the memory advisor for the buffer cache

答案:D

(8)You  have  issued  a  SHUTDOWN  ABORT  command  to  bring  down  your  database  instance. Consider the steps that will be performed later when you open the database:
   1. SGA is allocated.
   2. Control file is read.
   3. Redo log files are read.
   4. Instance recovery is started.
   5. Background processes are started.
   6. Data files are checked for consistency.
   7. Server parameter file or the initialization parameter file is read. 
Which option has the correct order in which these steps occur?
A. 7, 1, 5, 2, 3, 6, 4
B. 1, 2, 3, 7, 5, 6, 4
C. 7, 1, 4, 5, 2, 3, 6
D. 1, 7, 5, 4, 2, 3, 6

答案:A

(9)You used the IMMEDIATE option to shutdown your database instance.  Consider the steps that will be performed later when you open the database:

Which option has the correct order in which these steps occur?

clipboard[38]

A. 7, 1, 5, 2, 3, 6, 4
B. 1, 5, 7, 2, 3, 6; step 4 is not required
C. 7, 1, 5, 2, 3, 6 step 4 is not required
D. 1, 2, 3, 5, 6, 4; step 7 is not required

答案:C

(10)You have executed this command to change the size of the database buffer cache:
   SQL> ALTER SYSTEM SET DB_CACHE_SIZE=2516582;
   System altered.
To verify the change in size, you executed this command:
   SQL> SHOW PARAMETER DB_CACHE_SIZE
   NAME                           TYPE                VALUE
   --------------------        ----------------    ------------------
   db_cache_size                big integer           4194304
Why is the value set to 4194304 and not to 2516582?
A.  Because 4194304 is the granule size
B.  Because 4194304 is the standard block size
C.  Because 4194304 is the largest nonstandard block size defined in the database
D.  Because 4194304 is the total size of data already available in the database buffer cach

答案:A

(11)View the Exhibit and examine the output of the query.
What do you infer from this?
clipboard[39]
A.  The SGA_TARGET is a static parameter.
B.  The instance is started, but the database is not yet open.
C.  The server parameter file (SPFILE) was used to start the instance.
D.  The SGA_TARGET parameter does not have any effect on the database instance until the SGA_MAX_SIZE parameter is specified.

答案:C

(12)Identify the two situations in which you use the alert log file in your database to check the details. (Choose two.)
A.  Running a query on a table returns "ORA-600: Internal Error."
B.  Inserting a value in a table returns "ORA-01722: invalid number."
C.  Creating a table returns "ORA-00955: name is already used by an existing object."
D.  Inserting a value in a table returns "ORA-00001: unique constraint (SYS.PK_TECHP) violated."
E.  Inserting a row in a table returns "ORA-00060: deadlock detected while waiting for resource."

答案:AE

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28536251/viewspace-1972163/,如需轉載,請註明出處,否則將追究法律責任。

相關文章