今天来学习一下监听的相关内容,昨晚被老大问了两个关于监听很简单的问题,但是却吞吞吐吐回答,而且有一个问题还答错了,刚刚查了下资料,才发现“驴头对了马嘴”,哭笑不得。
一、监听(listener)是什么
监听器是Oracle基于服务器端的一种网络服务,主要用于监听客户端向数据库服务器端提出的连接请求。既然是基于服务器端的服务,那么它也只存在于数据库服务器端,进行监听器的设置也是在数据库服务器端完成的。
oracle网络配置有三个配置文件 listener.ora,tnsnames.ora,sqlnet.ora,其目录均在$ORACLE_HOME/network/admin 。
Oracle客户端与服务器端的连接是通过客户端发出连接请求,由服务器端监听器对客户端连接请求进行合法检查,如果连接请求有效,则进行连接,否则拒绝该连接。
二、如何配置监听
1)listener.ora 主要负责服务器端的监听配置,配置的基本模板:
# listener.ora Network Configuration File: /home/oracle/app/product/11/db/network/admin/listener.ora# Generated by Oracle configuration tools.LISTENER_ORA = (注:LISTENER_ORA 为监听名字) (DESCRIPTION_LIST = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 12.1.4.5)(PORT = 1521)) ) )SID_LIST_LISTENER_ORA = (注:LISTENER_ORA 为监听名字) (SID_LIST = (SID_DESC = (GLOBAL_DBNAME = wsj) (ORACLE_HOME = /home/oracle/app/product/11/db) (SID_NAME = wsj) ) )
ADR_BASE_LISTENER = /oracle/app/oracle
其中:
PROTOCOL指的是监听接收链接的协议;
HOST是指的监听运行的主机或者IP地址;
PORT指的是监听运行的端口;
SID_NAME指的是监听服务的实例名;
GLOBAL_NAME指的是监听服务的服务名;
ORACLE_HOME指的是监听服务的$ORACLE_HOME。
如果[监听名]和SID_LIST_[监听名]配置是成对出现的,这代表是静态监听;如果listener.ora文件只有[监听名]的配置,监听就变成动态监听。
动态注册:客户端(sys用户)只能在远程数据库启动的情况下连接上,远程数据库关闭时客户端连接不上远程数据库。
静态注册:无论远程数据库处于开启还是关闭状态,客户端(sys用户)都可连接上。
静态注册可以在客户端控制服务器端数据库的启停,而动态注册则不可以,动态注册的监听在实例关闭时会被注销。
数据库关闭的状态下,只有sys用户可连入数据库,其他用户不可连入。
测试:
当数据库关闭状态时,sys用户可以连入数据库,提示接入空实例。其他用户不能连接至数据库:
[oracle@localhost ~]$ sqlplus / as sysdbaSQL*Plus: Release 11.2.0.4.0 Production on Sun Feb 11 21:14:56 2018Copyright (c) 1982, 2013, Oracle. All rights reserved.Connected to an idle instance. SQL> exit
[oracle@localhost ~]$ sqlplus hr/12
SQL*Plus: Release 11.2.0.4.0 Production on Sun Feb 11 21:16:47 2018
Copyright (c) 1982, 2013, Oracle. All rights reserved.
ERROR:
ORA-01034: ORACLE not availableORA-27101: shared memory realm does not existLinux-x86_64 Error: 2: No such file or directoryProcess ID: 0Session ID: 0 Serial number: 0a.当为动态配置时:
查看listener.ora:
[oracle@localhost ~]$ cd /oracle/app/oracle/product/11.2.0/dbhome_1/network/admin/[oracle@localhost admin]$ more listener.ora # listener.ora Network Configuration File: /oracle/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora# Generated by Oracle configuration tools.LISTENER = (DESCRIPTION_LIST = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521)) ) ) ADR_BASE_LISTENER = /oracle/app/oracle
在数据库关闭的情况下查看监听状态:
[oracle@localhost ~]$ lsnrctl statusLSNRCTL for Linux: Version 11.2.0.4.0 - Production on 11-FEB-2018 21:27:00Copyright (c) 1991, 2013, Oracle. All rights reserved.Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))STATUS of the LISTENER------------------------Alias LISTENERVersion TNSLSNR for Linux: Version 11.2.0.4.0 - ProductionStart Date 11-FEB-2018 13:07:16Uptime 0 days 8 hr. 19 min. 43 secTrace Level offSecurity ON: Local OS AuthenticationSNMP OFFListener Parameter File /oracle/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.oraListener Log File /oracle/app/oracle/diag/tnslsnr/localhost/listener/alert/log.xmlListening Endpoints Summary... (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))) (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))The listener supports no servicesThe command completed successfully
此时,远程连接数据库,发现有报错:
[oracle@localhost ~]$ sqlplus sys/oracle@rcl as sysdbaSQL*Plus: Release 11.2.0.4.0 Production on Sun Feb 11 21:28:18 2018Copyright (c) 1982, 2013, Oracle. All rights reserved.ERROR:ORA-12514: TNS:listener does not currently know of service requested in connectdescriptorEnter user-name:
当打开数据库再次查看监听状态:
[oracle@localhost ~]$ sqlplus / as sysdbaSQL*Plus: Release 11.2.0.4.0 Production on Sun Feb 11 21:29:15 2018Copyright (c) 1982, 2013, Oracle. All rights reserved.Connected to an idle instance.SQL> startupORACLE instance started.Total System Global Area 830930944 bytesFixed Size 2257800 bytesVariable Size 503319672 bytesDatabase Buffers 318767104 bytesRedo Buffers 6586368 bytesDatabase mounted.Database opened.SQL> ![oracle@localhost ~]$ lsnrctl statusLSNRCTL for Linux: Version 11.2.0.4.0 - Production on 11-FEB-2018 21:30:09Copyright (c) 1991, 2013, Oracle. All rights reserved.Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))STATUS of the LISTENER------------------------Alias LISTENERVersion TNSLSNR for Linux: Version 11.2.0.4.0 - ProductionStart Date 11-FEB-2018 13:07:16Uptime 0 days 8 hr. 22 min. 52 secTrace Level offSecurity ON: Local OS AuthenticationSNMP OFFListener Parameter File /oracle/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.oraListener Log File /oracle/app/oracle/diag/tnslsnr/localhost/listener/alert/log.xmlListening Endpoints Summary... (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))) (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))Services Summary...Service "ORCLXDB" has 1 instance(s). Instance "mydb", status READY, has 1 handler(s) for this service...Service "mydb" has 1 instance(s). Instance "mydb", status READY, has 1 handler(s) for this service...The command completed successfully
b.当为静态监听时:
查看listener.ora : <当配置listener.ora中GLOBAL_DBNAME的值 == tnsnames.ora中的SERVICE_NAME!!!!!!!!!>
[oracle@localhost admin]$ more listener.ora# listener.ora Network Configuration File: /oracle/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora# Generated by Oracle configuration tools.LISTENER = (DESCRIPTION_LIST = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521)) ) )ADR_BASE_LISTENER = /oracle/app/oracleSID_LIST_LISTENER = (SID_LIST = (SID_DESC = (GLOBAL_DBNAME = mydb) ##listener.ora中GLOBAL_DBNAME的值 == tnsnames.ora中的SERVICE_NAME (ORACLE_HOME = /oracle/app/oracle/product/11.2.0/dbhome_1) (SID_NAME = mydb) ##可通过env | grep ORACLE查看SID_NAME和$ORACLE_HOME的值 ) )
在数据库关闭的情况下查看监听状态:
[oracle@localhost admin]$ lsnrctl statusLSNRCTL for Linux: Version 11.2.0.4.0 - Production on 11-FEB-2018 22:16:02Copyright (c) 1991, 2013, Oracle. All rights reserved.Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))STATUS of the LISTENER------------------------Alias LISTENERVersion TNSLSNR for Linux: Version 11.2.0.4.0 - ProductionStart Date 11-FEB-2018 22:13:25Uptime 0 days 0 hr. 2 min. 36 secTrace Level offSecurity ON: Local OS AuthenticationSNMP OFFListener Parameter File /oracle/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.oraListener Log File /oracle/app/oracle/diag/tnslsnr/localhost/listener/alert/log.xmlListening Endpoints Summary... (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))) (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))Services Summary...Service "mydb" has 1 instance(s). Instance "mydb", status UNKNOWN, has 1 handler(s) for this service...The command completed successfully
此时,远程连接数据库,成功连接:
[oracle@localhost ~]$ sqlplus sys/oracle@rcl as sysdbaSQL*Plus: Release 11.2.0.4.0 Production on Sun Feb 11 22:16:46 2018Copyright (c) 1982, 2013, Oracle. All rights reserved.Connected to an idle instance.
开启数据库,再次查看监听状态(UNKNOWN即表示静态监听):
[oracle@localhost ~]$ lsnrctl statusLSNRCTL for Linux: Version 11.2.0.4.0 - Production on 11-FEB-2018 22:17:49Copyright (c) 1991, 2013, Oracle. All rights reserved.Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))STATUS of the LISTENER------------------------Alias LISTENERVersion TNSLSNR for Linux: Version 11.2.0.4.0 - ProductionStart Date 11-FEB-2018 22:13:25Uptime 0 days 0 hr. 4 min. 23 secTrace Level offSecurity ON: Local OS AuthenticationSNMP OFFListener Parameter File /oracle/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.oraListener Log File /oracle/app/oracle/diag/tnslsnr/localhost/listener/alert/log.xmlListening Endpoints Summary... (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))) (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))Services Summary...Service "ORCLXDB" has 1 instance(s). Instance "mydb", status READY, has 1 handler(s) for this service...Service "mydb" has 2 instance(s). Instance "mydb", status UNKNOWN, has 1 handler(s) for this service... Instance "mydb", status READY, has 1 handler(s) for this service...The command completed successfully
2)tnsnames.ora:主要负责客户端的监听配置,基本模板:
# tnsnames.ora Network Configuration File: /home/oracle/app/product/11/db/network/admin/tnsnames.ora# Generated by Oracle configuration tools. #RCL为实例名 RCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.30.2)(PORT = 1521)) #TNSNAME 对应的主机,端口,协议 (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = mydb) #可通过show parameter service_name;查询service_name 配置listener.ora中GLOBAL_DBNAME的值 == tnsnames.ora中的SERVICE_NAME ) )PROTOCOL :客户端与服务器端通讯的协议,一般为TCP ,该内容一般不用改。HOST :数据库侦听所在的机器的机器名或IP 地址,数据库侦听一般与数据库在同一个机器上,所以当我说数据库侦听所在的机器一般也是指数据库所在的机器。在UNIX 或WINDOWS 下,可以通过在数据库侦听所在的机器的命令提示符下使用hostname 命令得到机器名,或通过ipconfig(for WINDOWS) or ifconfig (for UNIX )命令得到IP 地址。需要注意的是,不管用机器名或IP 地址,在客户端一定要用ping 命令ping 通数据库侦听所在的机器的机器名,否则需要在 hosts 文件中加入数据库侦听所在的机器的机器名的解析。PORT :数据库侦听正在侦听的端口,可以察看服务器端的listener.ora 文件或在数据库侦听所在的机器的命令提示符下通过lnsrctl status [listener name] 命令察看。此处Port 的值一定要与数据库侦听正在侦听的端口一样。SERVICE_NAME :在服务器端,用system 用户登陆后,sqlplus> show parameter service_name命令察看。RCL: 对应的本机,SALES 对应的另外一个IP 地址,里边还定义了使用主用服务器还是共享服务器模式进行连接
3)sqlnet.ora
是驻留在客户端机器和数据库服务器上。使用该文件存储和实现概要文件。数据库服务器可以在sqlnet.ora文件中配置访问控制参数。这些参数指定基于协议的客户机是否允许或拒绝访问。
列出一些常见参数:
BEQUEATH_DETACH:控制unix系统中signal handling 的开关,默认是no,即signal handling 打开。DEFAULT_SDU_SIZE:指定session data unit (SDU) 的大小,单位是bytes,建议在client端和server端都设置这个参数,确保链接的时候使用相同的SDU size,如果client端和server端配置的值不匹配会使用较小的。LOG_DIRECTORY_CLIENT/LOG_DIRECTORY_SERVER:指定客户端/server端log日志文件的位置NAMES.DEFAULT_DOMAIN:设定客户端解析名字的域NAMES.DIRECTORY_PATH:指定client name解析方法的次序,默认是NAMES.DIRECTORY_PATH=(tnsnames, onames, hostname。取值可以是tnsnames,ldap(dictionary server),hostname/ezconnect,cds (分布式环境下),nis (Network Information Service (NIS)SQLNET_ALLOWED_LOGON_VERSIONS:指定运行链接的oracle的版本SQLNET_ALLOWED_LOGON_VERSIONS=(10,9,8)SQLNET.AUTHENTICATION_SERVICES:指定启动一个或多个认证服务SQLNET.INBOUND_CONNECT_TIMEOUT:指定客户端没有链接成功超时的时间。超时之后oracle会中断链接,同时报错。SSL_VERSION:指定ssl链接的版本TCP.EXCLUDED_NODES:指定不允许访问oracle的节点,可以使用主机名或者IP地址TCP.INVITED_NODES:指定允许访问db的客户端,他的优先级比TCP.EXCLUDED_NODES高。TCP.VALIDNODE_CHECKING:使用这个参数来启用上边的两个参数。TNSPING.TRACE_DIRECTORY:使用这个参数指定tnsping trace文件的目录,默认是$ORACLE_HOME/network/trace目录
三、一些常用命令
a. lsnrctl start/stop 开启监听/关闭监听
b.lsnrctl status 查看监听状态 (ready:动态,unknown:静态,)
c.lsnrctl reload 重新装入监听器,重新读取listener.ora文件,但不关闭监听器。如果该文件发生了变化,重新刷新监听器。
d.lsnrctl version 显示oracle net软件与协议适配器的版本。
e.lsnrctl services 列举监听器的服务信息,列出服务的一个汇总表及为每个协议服务处理程序所建立和拒绝的连接信息个数。
………………
先到这里啦,后续再加~~~