【學習目標】
Oracle 監聽器是一個服務器端程序,用於監聽所有來自客戶端的請求,並為其提供數 據庫服務。因此對監聽器的管理與維護相當重要。
本章主要內容是描述對Oracle 監聽器的配置與管理。
【本章要點】
• 配置監聽
• 區分獨占、共享模式
• 區分動態、靜態注冊
【關鍵術語】
Listener 監聽Tnsname 服務名
Dedicated Server 獨占服務Shared Server 共享服務
為了方便對Oracle 數據庫進行管理,一般以下情況應該對Oracle進行網絡配置。
• 在客戶端對服務器端數據庫進行管理(網絡客戶端管理)
• 在一台服務器上管理多個數據庫(本地管理)
在客戶端上管理,只需要安裝客戶端管理軟件即可。客戶端為了連接特定的服務器和數 據庫,需要對網絡進行配置,通過網絡可以連接到不同的Oracle 數據庫服務器。
如圖1-1所示,首先需要在數據庫服務器端配置偵聽程序,然后需要在客戶端配置網絡 服務名。
一、原理解析
這里我們借騰訊軟件來打個比方。
二、配置偵聽器(LISTENER)
網絡配置可以使用網絡配置管理器Net Manager 來完成。Net Manager 用於網絡連接 服務名、偵聽器等的創建和設置。
在同一台服務器上可以配置多個偵聽器,但偵聽的端口號不能相同。不同的偵聽器可以 偵聽對同一個數據庫的請求,同一個偵聽器也可以偵聽對不同數據庫的請求。這樣當一個偵 聽器忙時,另外的偵聽器仍然可以為客戶端請求提供服務,減少用戶連接的等待時間。 偵聽器的配置信息存入對應的配置文件listener.ora(注:使用Net Manager工具配置偵 聽器相當於修改該文件),該文件的默認路徑為$ORACLE_HOME/network/admin,其中 $ORACLE_HOME代表服務器軟件的安裝目錄。另外通過設置環境變量TNS_ADMIN可以改 變偵聽器配置文件的位置(.bash_profile文件中設置)。 偵聽器的設置主要包括偵聽位置和對應的數據庫服務,偵聽位置包括主機、端口和使用 的通訊協議,Oracle 默認的標准端口是1521,通常采用的協議是TCP/IP。1. 啟動Net Manager
2 [oracle@oracle ~]$ netmgr
2. 通過面板左側的“+”、“×”按鈕,可以增加和刪除偵聽器。
3. 點擊“+”增加一個監聽器,並且輸入監聽名稱。
- • 協議:用於指定監聽程序要使用的網絡協議,監聽程序可以使用多種網絡協議,但 最常用的是“TCP/IP”協議。
• 主機:用於指定Oracle 服務器所在機器的主機名或IP 地址。因為偵聽器和Oracle 服務器位於同一台機器,主機名在這里也可以輸入LOCALHOST。//和/etc/hosts保持一致。
• 端口號:用於指定監聽程序所要使用的TCP/IP 端口號,默認監聽端口號為1521。
如果要指定其他端口號,則必須是操作系統未占用的端口。
為了區別於LISTENER與LISTENER1,將LISTENER1 端口號改為1522。
注意:如果網絡客戶端想要通過LISTENER1 連接數據庫,其用於連接數據庫的網絡服 務名配置,也要使用同樣的端口號。
- • 全局數據庫名:一般設置為DB_NAME.DB_DOMAIN(DB_NAME 和DB_DOMAIN為 初始化參數),這里設為orcl。
• Oracle 主目錄:應該設置為Oracle 軟件的安裝路徑。
• SID:設置為數據庫的SID,這里設置為orcl。
三、配置客戶端網絡服務名
通過客戶端訪問服務器端的數據庫,就需要進行網絡配置。客戶端通過提供必要的參數, 包括要連接的服務器名稱、連接的端口號、使用的通訊協議等連接到服務器端。 對於普通用戶,這些參數不便於理解和記憶。通過網絡配置,可以把網絡服務名同配置 聯系起來,一旦配置完成,以后就可以使用該網絡服務名進行數據庫的連接,使連接過程得 到簡化。
當安裝Oracle 數據庫產品時,系統會自動在服務器端為數據庫配置相應的網絡服務名, 默認網絡服務名與實例標識(SID)相同。為了便於訪問同一台服務器上的多個Oracle 數據 庫,應該為新數據庫配置相應的網絡服務名。
網絡服務名對應的配置文件名稱為tnsnames.ora,該文件的默認路徑為 $ORACLE_HOME/network/admin。同樣,通過設置環境變量TNS_ADMIN可以改變該配置 文件的位置。
配置網絡服務名也可以使用工具Net Manager 來完成。1. 填寫客戶端網絡服務名
2. 選擇協議
3. 填寫主機名
4. 填寫數據庫的服務名(Service_names)
5. 點擊“Finish”完成配置
四、關於注冊
1. 什么是注冊
注冊就是將數據庫作為一個服務注冊到監聽程序。客戶端不需要知道數據庫名和實例 名,只需要知道該數據庫對外提供的服務名就可以申請連接到數據庫。這個服務名可能與實例名一樣,也有可能不一樣。
在數據庫服務器啟動過程中,數據庫服務器會向監聽程序注冊相應的服務(無論何時啟 動一個數據庫,默認地都有兩條信息注冊到監聽器中:數據庫服務器對應的實例和服務。) 相當於是這樣:在數據庫服務器和客戶端之間有一監聽程序(Listener),在監聽程序 中,會記錄相應數據庫對應的服務名(一個數據庫可能對應有多個服務名),當客戶端需要 連接數據庫時,只需要提供服務名,就可以建立客戶端和服務器之間的連接。2. 兩個參數
1) instance_name:
注冊到監聽器中的實例值從參數文件中的instance_name 參數取得。如果該參數沒有設 定值,那么它將取參數文件中的db_name 的值。如果在RAC 中配置,您必須將集群中每個 實例的instance_name 參數設置為一個唯一的值。2) service_names:
注冊到監聽器中的服務值從參數文件中的參數service_names 取得。如果該參數沒有設 定值,數據庫將拼接參數文件中的db_name 和db_domain 的值來注冊自己。
1 --//修改參數service_names 2 SQL> alter system set service_names='orcl,aaa'; 3 System altered. 4 SQL> show parameter service_names; 5 NAME TYPE VALUE 6 ---------------------------------- ------- ---------------- 7 service_names string orcl,aaa 8 --//查看監聽狀態 9 [oracle@oracle admin]$ lsnrctl stat 10 LSNRCTL for Linux: Version 10.2.0.1.0 - Production on 09-NOV-2013 17:23:10 11 Copyright (c) 1991, 2005, Oracle. All rights reserved. 12 Connecting to 13 (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=oracle.example.com) 14 (PORT=1521))) 15 STATUS of the LISTENER19 Alias LISTENER 20 Version TNSLSNR for Linux: Version 10.2.0.1.0 - Production 21 Start Date 09-NOV-2013 16:46:42 22 Uptime 0 days 0 hr. 36 min. 28 sec 23 Trace Level off 24 Security ON: Local OS Authentication 25 SNMP OFF 26 Listener Parameter File 27 /u01/app/oracle/product/10.2.0/db_1/network/admin/listener.ora 28 Listener Log File 29 /u01/app/oracle/product/10.2.0/db_1/network/log/listener.log 30 Listening Endpoints Summary... 31 (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=oracle.example.com)(PORT 32 =1521))) 33 Services Summary... 34 Service "aaa" has 1 instance(s). 35 Instance "orcl", status READY, has 1 handler(s) for this service... 36 Service "orcl" has 1 instance(s). 37 Instance "orcl", status READY, has 1 handler(s) for this service... 38 Service "orclXDB" has 1 instance(s). 39 Instance "orcl", status READY, has 1 handler(s) for this service... 40 Service "orcl_XPT" has 1 instance(s). 41 Instance "orcl", status READY, has 1 handler(s) for this service... 42 The command completed successfully 43 --//查看listener.ora 內容 44 [oracle@oracle admin]$ more listener.ora 45 # listener.ora Network Configuration File: /u01/app/oracle/product/10.2.0/db_1/network/admin/listener.ora 47 # Generated by Oracle configuration tools. 48 LISTENER = 49 (DESCRIPTION = 50 (ADDRESS = (PROTOCOL = TCP)(HOST = oracle.example.com)(PORT = 1521)) 52 ) 53 --//沒有寫list,動態注冊 54 55 56 --//查看tnsnames.ora 內容 57 [oracle@oracle admin]$ more tnsnames.ora 58 ORCL_S = 59 (DESCRIPTION = 60 (ADDRESS_LIST = 61 (ADDRESS = (PROTOCOL = TCP)(HOST = oracle.example.com)(PORT = 1521)) ) 64 (CONNECT_DATA = (SERVICE_NAME = orcl) ) 67 ) 68 AAA_S = 69 (DESCRIPTION = 70 (ADDRESS_LIST = 71 (ADDRESS = (PROTOCOL = TCP)(HOST = oracle.example.com)(PORT = 1521)) ) 74 (CONNECT_DATA = 75 (SERVICE_NAME = aaa) 76 ) 77 ) 78 --//驗證能否通過orcl_s 成功登錄 79 [oracle@oracle admin]$ sqlplus sys/oracle@orcl_s as sysdba 80 SQL*Plus: Release 10.2.0.1.0 - Production on Sat Nov 9 17:22:05 2013 81 Copyright (c) 1982, 2005, Oracle. All rights reserved. 82 Connected to: 83 Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production 84 With the Partitioning, OLAP and Data Mining options 85 --//驗證能否通過aaa_s 成功登錄 86 [oracle@oracle admin]$ sqlplus sys/oracle@aaa_s as sysdba 87 SQL*Plus: Release 10.2.0.1.0 - Production on Sat Nov 9 17:22:10 2013 88 Copyright (c) 1982, 2005, Oracle. All rights reserved. 89 Connected to: 90 Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production 91 With the Partitioning, OLAP and Data Mining options 92 93 --//驗證oracle 注冊service_names 的值 94 SQL> alter system set service_names='bbb'; 95 System altered. 96 --//aaa_s 無法正常連接 97 [oracle@oracle admin]$ sqlplus sys/oracle@aaa_s as sysdba 98 SQL*Plus: Release 10.2.0.1.0 - Production on Sat Nov 9 17:35:11 2013 99 Copyright (c) 1982, 2005, Oracle. All rights reserved. 100 ERROR: 101 ORA-12514: TNS:listener does not currently know of service requested in connect 102 descriptor 103 Enter user-name: 104 --//orcl_s 仍然可以正常連接(oracle 會自動注冊) 105 [oracle@oracle admin]$ sqlplus sys/oracle@orcl_s as sysdba 106 SQL*Plus: Release 10.2.0.1.0 - Production on Sat Nov 9 17:41:10 2013 107 Copyright (c) 1982, 2005, Oracle. All rights reserved. 108 Connected to: 109 Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production 110 With the Partitioning, OLAP and Data Mining options 111 --//測試service_names 參數的值 112 SQL> show parameter db_name 113 NAME TYPE VALUE 114 ------------------------------------ ----------- ----------- 115 db_name string orcl 116 --//當前db_domain 的值為空 117 SQL> show parameter domain 118 NAME TYPE VALUE 119 ------------------------------------ ----------- ------------ 120 db_domain string 121 --//當前servcie_names 的值為非默認值 122 SQL> show parameter service_names 123 NAME TYPE VALUE 124 ------------------------------------ ----------- ----------- 125 db_name string bbb 126 --//定義db_domain 的值 127 SQL> alter system set db_domain='oracle.com' scope=spfile; 128 System altered. 129 --//重置service_names 的值 130 SQL> alter system reset service_names scope=spfile sid='*'; 131 System altered. 132 133 134 --//重啟數據庫 135 SQL> shutdown immediate; 136 Database closed. 137 Database dismounted. 138 ORACLE instance shut down. 139 SQL> startup; 140 ORACLE instance started. 141 Total System Global Area 285212672 bytes 142 Fixed Size 1218992 bytes 143 Variable Size 92276304 bytes 144 Database Buffers 188743680 bytes 145 Redo Buffers 2973696 bytes 146 Database mounted. 147 Database opened. 148 SQL> show parameter domain 149 NAME TYPE VALUE 150 ------------------------------------ ----------- --------------- 151 db_domain string oracle.com 152 SQL> show parameter service 153 --//驗證service_names 的值為db_name+db_domain 154 NAME TYPE VALUE 155 ------------------------------------ ----------- -------------------- 156 service_names string orcl.oracle.com3. 動態注冊
動態注冊是在instance 啟動的時候PMON 進程根據參數文件中的instance_name, service_names 兩個參數將實例和服務動態注冊到listener 中。
首先要在參數文件中指定instance_name,service_names 兩個參數的值。在sqlplus 下 通過show parameter service_names 和show parameter instance_name 可以查看這兩個 參數的值。 可選擇的是,您可以在service_names 參數中指定多個服務值,值之間用逗號格開, 這對於共享服務器配置是很有用的。 動態注冊默認只注冊到默認的監聽器上(名稱是LISTENER、端口是1521、協議是 TCP),因為pmon 只會動態注冊port 等於1521的監聽,否則pmon 不能動態注冊listener, 如果需要向非默認監聽注冊,則需要配置local_listener 參數!1) Pmon 進程注冊(默認端口):
1 //為方便實驗,將db_domain 參數改回來 2 SQL> alter system set db_domain='' scope=spfile; 3 System altered. 4 //重啟數據庫使參數生效 5 SQL> shutdown immediate; 6 Database closed. 7 Database dismounted. 8 ORACLE instance shut down. 9 SQL> startup; 10 ORACLE instance started. 11 Total System Global Area 285212672 bytes 12 Fixed Size 1218992 bytes 13 Variable Size 92276304 bytes 14 Database Buffers 188743680 bytes 15 Redo Buffers 2973696 bytes 16 Database mounted. 17 Database opened. 18 //驗證參數 19 SQL> show parameter service_names 20 NAME TYPE VALUE 21 ------------------------------------ ----------- -------------- 22 service_names string orcl 23 //查看listener.ora 24 [oracle@oracle admin]$ more listener.ora 25 # listener.ora Network Configuration File: /u01/app/oracle/product/10.2.0/db_1/n 26 etwork/admin/listener.ora 27 # Generated by Oracle configuration tools. 28 LISTENER = 29 (DESCRIPTION = 30 (ADDRESS = (PROTOCOL = TCP)(HOST = oracle.example.com)(PORT = 31 1521)) 32 ) 33 //查看監聽器狀態 34 [oracle@oracle admin]$ lsnrctl stat 35 LSNRCTL for Linux: Version 10.2.0.1.0 - Production on 09-NOV-2013 18:20:37 36 Copyright (c) 1991, 2005, Oracle. All rights reserved. 37 Connecting to 38 (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=oracle.example.com)(PORT=15 39 21))) 40 42 STATUS of the LISTENER 43 ------------------------ 44 Alias LISTENER 45 Version TNSLSNR for Linux: Version 10.2.0.1.0 - 46 Production 47 Start Date 09-NOV-2013 16:46:42 48 Uptime 0 days 1 hr. 33 min. 54 sec 49 Trace Level off 50 Security ON: Local OS Authentication 51 SNMP OFF 52 Listener Parameter File 53 /u01/app/oracle/product/10.2.0/db_1/network/admin/listener.ora 54 Listener Log File 55 /u01/app/oracle/product/10.2.0/db_1/network/log/listener.log 56 Listening Endpoints Summary... 57 (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=oracle.example.com)(POR 58 T=1521))) 59 Services Summary... 60 Service "orcl" has 1 instance(s). 61 Instance "orcl", status READY, has 1 handler(s) for this service... 62 Service "orclXDB" has 1 instance(s). 63 Instance "orcl", status READY, has 1 handler(s) for this service... 64 Service "orcl_XPT" has 1 instance(s). 65 Instance "orcl", status READY, has 1 handler(s) for this service... 66 The command completed successfully 67 //查看tnsnames.ora 68 [oracle@oracle admin]$ more tnsnames.ora 69 ORCL_S = 70 (DESCRIPTION = 71 (ADDRESS_LIST = 72 (ADDRESS = (PROTOCOL = TCP)(HOST = oracle.example.com)(PORT 73 = 1521)) 74 ) 75 (CONNECT_DATA = 76 (SERVICE_NAME = orcl) 77 ) 78 ) 79 81 //嘗試連接 82 [oracle@oracle admin]$ sqlplus sys/oracle@orcl_s as sysdba 83 SQL*Plus: Release 10.2.0.1.0 - Production on Sat Nov 9 18:09:52 2013 84 Copyright (c) 1982, 2005, Oracle. All rights reserved. 85 Connected to: 86 Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production 87 With the Partitioning, OLAP and Data Mining options2) 修改參數注冊(非默認端口):
1 //查看listener.ora 2 [oracle@oracle admin]$ more listener.ora 3 # listener.ora Network Configuration File: /u01/app/oracle/product/10.2.0/db_1/n 4 etwork/admin/listener.ora 5 # Generated by Oracle configuration tools. 6 LISTENER = 7 (DESCRIPTION = 8 (ADDRESS = (PROTOCOL = TCP)(HOST = oracle.example.com)(PORT = 9 1521)) 10 ) 11 LISTENER1 = 12 (DESCRIPTION = 13 (ADDRESS = (PROTOCOL = TCP)(HOST = oracle.example.com)(PORT = 14 1522)) 15 ) 16 //查看tnsnames.ora 17 [oracle@oracle admin]$ more tnsnames.ora 18 # tnsnames.ora Network Configuration File: /u01/app/oracle/product/10.2.0/db_1/n 19 etwork/admin/tnsnames.ora 20 ORCL_S = 21 (DESCRIPTION = 22 (ADDRESS_LIST = 23 (ADDRESS = (PROTOCOL = TCP)(HOST = oracle.example.com)(PORT = 24 1521)) 25 ) 26 (CONNECT_DATA = 27 (SERVICE_NAME = orcl) 28 ) 29 ) 30 31 ORCL_S1 = 32 (DESCRIPTION = 33 (ADDRESS_LIST = 34 (ADDRESS = (PROTOCOL = TCP)(HOST = oracle.example.com)(PORT = 35 1522)) 36 ) 37 (CONNECT_DATA = 38 (SERVICE_NAME = orcl) 39 ) 40 ) 41 //修改參數 42 [oracle@oracle admin]$ sqlplus / as sysdba 43 SQL*Plus: Release 10.2.0.1.0 - Production on Sat Nov 9 18:05:21 2013 44 Copyright (c) 1982, 2005, Oracle. All rights reserved. 45 Connected to: 46 Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production 47 With the Partitioning, OLAP and Data Mining options 48 SQL> show parameter local 49 NAME TYPE VALUE 50 ------------------------------------ ----------- ------------------------------ 51 local_listener string 52 SQL> alter system set local_listener='orcl_s1'; 53 System altered. 54 SQL> alter system register; 55 System altered. 56 SQL> exit 57 Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 58 Production 59 With the Partitioning, OLAP and Data Mining options 60 //測試連接 61 [oracle@oracle admin]$ sqlplus sys/oracle@orcl_s1 as sysdba 62 SQL*Plus: Release 10.2.0.1.0 - Production on Sat Nov 9 18:09:52 2013 63 Copyright (c) 1982, 2005, Oracle. All rights reserved. 64 Connected to: 65 Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production 66 With the Partitioning, OLAP and Data Mining options 67 //或者 68 SQL> alter system set LOCAL_LISTENER='(ADDRESS = (PROTOCOL = 69 TCP)(HOST = oracle.example.com)(PORT = 1522))';4. 靜態注冊
靜態注冊就是實例啟動時讀取listener.ora 文件中實例和服務的配置,將實例和服務注 冊到監聽程序。1 --示例: 2 SID_LIST_LISTENER = 3 (SID_LIST = 4 (SID_DESC = 5 (GLOBAL_DBNAME = orcl) 6 (ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1) 7 (SID_NAME = orcl) 8 ) 9 (SID_DESC = 10 (GLOBAL_DBNAME = emrep) 11 (ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1) 12 (SID_NAME = emrep) 13 ) 14 ) 15 --List 列表中的即是需要靜態注冊的服務
五、查詢某服務是靜態還是動態注冊
可以使用命令lsnrctl status 來查看某服務是靜態注冊還是動態注冊。
• 實例狀態為UNKNOWN 值時表明此服務是靜態注冊的。這時監聽器用來表明它不 知道關於該實例的任何信息,只有當客戶發出連接請求時,才檢查該實例是否存在。
• 實例狀態為READY 或BLOCKED(數據庫nomount 時)表明是此服務是動態注 冊的。