oracle動態監聽和靜態監聽的區別
一、什麼是註冊
註冊就是將資料庫作為一個服務註冊到監聽程式。
客戶端不需要知道資料庫名和例項名,只需要知道該資料庫對外提供的服務名就可以申請連線到資料庫。
這個服務名可能與例項名一樣,也有可能不一樣。
在資料庫伺服器啟動過程中,資料庫伺服器會向監聽程式註冊相應的服務(無論何時啟動一個數據庫,預設地都有兩條資訊註冊到監聽器中:資料庫伺服器對應的例項和服務)
相當於是這樣:在資料庫伺服器和客戶端之間有一監聽程式(Listener),在監聽程式中,會記錄相應資料庫對應的服務名(一個數據庫可能對應有多個服務名),當客戶端需要連線資料庫時,只需要提供服務名,就可以建立客戶端和伺服器之間的連線.
1.1、資料庫關閉的狀態下,只有sys使用者可連入資料庫,其他使用者不可連入。
資料庫關閉狀態:
SQL*Plus: Release 11.2.0.1.0 Production on Mon Nov 12 16:12:15 2018 Copyright (c) 1982, 2009, Oracle. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options SQL> shutdown immediate; Database closed. Database dismounted. ORACLE instance shut down. SQL> select open_mode from v$database; select open_mode from v$database * ERROR at line 1: ORA-01034: ORACLE not available Process ID: 15430 Session ID: 13 Serial number: 39
1.2、sys使用者可以連入資料庫,提示接入空例項
$ sqlplus sys/123 as sysdba SQL*Plus: Release 11.2.0.1.0 Production on Mon Nov 12 16:14:28 2018 Copyright (c) 1982, 2009, Oracle. All rights reserved. Connected to an idle instance. SQL>
1.3、除sys使用者的其他使用者連線不進資料庫
$ sqlplus system/123 SQL*Plus: Release 11.2.0.1.0 Production on Mon Nov 12 16:15:02 2018 Copyright (c) 1982, 2009, Oracle. All rights reserved. ERROR: ORA-01034: ORACLE not available ORA-27101: shared memory realm does not exist Linux-x86_64 Error: 2: No such file or directory Process ID: 0 Session ID: 0 Serial number: 0
二、如何查詢某服務是靜態監聽註冊還是動態監聽註冊
可以使用命令lsnrctl status來檢視某服務是靜態註冊還是動態註冊。
例項狀態為UNKNOWN值時表明此服務是靜態註冊的設定。
這時監聽器用來表明它不知道關於該例項的任何資訊,只有當客戶發出連線請求時,它才檢查該例項是否存在。
動態監聽的狀態是READY。
三、動態註冊
3.1、動態註冊不需要顯示的配置listener.ora檔案,例項MOUNT時,PMON程序就會根據instance_name,service_name引數將例項和服務動態註冊到listerer中。
配置listener.ora檔案為空:
動態監聽狀態:
資料庫關閉
$ sqlplus sys/justsoft123 as sysdba SQL*Plus: Release 11.2.0.1.0 Production on Mon Nov 12 16:21:41 2018 Copyright (c) 1982, 2009, Oracle. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options SQL> shutdown immediate; Database closed. Database dismounted. ORACLE instance shut down.
此時在查詢監聽狀態
此時資料庫是完全關閉的,這時定是無法遠端連線資料庫的
啟動資料庫到nomount狀及檢視監聽情況,並測試過在nomount狀態,客戶端同樣無法用sys使用者進行遠端連線資料庫的
啟動資料庫到mount狀態,檢視監聽狀態並測試資料庫連線是成功的
*********************************************************************************************************************************************************************************************
四、靜態監聽
靜態註冊指例項啟動時讀取listener.ora配置檔案,將例項和服務註冊到監聽程式。
優點:
無論何時啟動一個數據庫,預設都有兩條資訊註冊到監聽器中:例項和服務。
在資料庫未open狀態中(前提是已經在nomount或mount狀態),就可以遠端連線到資料庫,對資料庫進行操作--當然了要配置密碼檔案。
4.1、接下來改為靜態監聽
#cat /opt/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora # listener.ora Network Configuration File: /opt/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora # Generated by Oracle configuration tools. SID_LIST_LISTENER = (SID_LIST = (SID_DESC = (GLOBAL_DBNAME = orcl) (ORACLE_HOME = /opt/oracle/product/11.2.0/dbhome_1) (SID_NAME = orcl) ) ) LISTENER = (DESCRIPTION_LIST = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = in)(PORT = 1521)) ) ) ADR_BASE_LISTENER = /opt/oracle
重新啟動監聽及資料庫,檢視靜態監聽狀態:
資料庫關閉後:
靜態監聽時遠端資料庫處於關閉狀態,是需要啟動到nomount或者mount狀,客戶端(sys使用者)都可以連線上,可以在客戶進行操作啟動到mount或是open狀態。