oracle监听器配置

时间:2021-02-03 08:38:20

刚学了动态监听、静态监听、非标准监听、共享服务器模式、专用服务器模式,赶紧写下来,免得忘了。顺便把自己怎么配置plsql developer的说说。
先讲一小故事。有一天你要去贾府找贾宝玉请教怎么去追林妹妹。要达到这个目的,要分几步实施呢?首先要找到贾府大门,告诉门卫你要找贾宝玉,门卫肯定知道贾府里住这个不学无术、好吃懒做的少爷叫贾宝玉。这时候他会做什么事情呢?假设贾府克隆人技术非常发达,可以随时克隆出一个贾宝玉。第一种情况,贾府待人非常热情,知道你要找贾宝玉,就立马克隆出一个贾宝玉招待你,全程为你服务,你走了就把这个克隆贾宝玉杀掉。第二种情况,克隆人也要花费时间金钱啊,贾府一共就克隆出了3个贾宝玉,不可能一直陪着哪一个客人。为了解决这一问题,贾府就找来了几个小厮,代替贾宝玉接待你,你要有什么问题,就由小厮转达给里面的三贾宝玉。然后三个贾宝玉那个有空那个回答,然后再由小厮把结果传达给你。

这里的贾府大门就相当于oracle的监听器。
贾宝玉就一服务进程,专门来满足你要求的。
几个小厮就是dispatcher,用来转达你要求的,可能同时接待多个客人。
如果你一进贾府就由一个贾宝玉来为你服务,那就是专有服务器模式。
如果你的命令还需要小厮转达,那就是共享服务器模式。


如果贾府没有大门的话,会出现什么情况呢?肯定任何人都找不到贾宝玉了。同理,要让你的oracle为其他人服务,监听是必不可少的。这里还有种情况,如果你和贾宝玉聊的正欢呢,贾府突然把大门关上了,素不接待其他客人,那你还能不能继续和贾宝玉聊天呢?答案是肯定的,你已经找到贾宝玉了,还管他大门开不开干什么。下面具体讲解静态监听,动态监听,非标准端口监听配置。
1.静态监听:
oracle监听文件默认存放位置是$ORACLE_HOME/network/admin下面。名字当然就是listener.ora了。

静态监听包括俩东西,第一块定义这个监听用的什么协议,主机是谁,监控那个端口。第二快定义这个监听的服务对象,服务对象是固定死的,所以叫做静态监听。
jiabaoyu=
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
      (ADDRESS = (PROTOCOL = TCP)(HOST = mypc)(PORT = 1521))
    )
  )

SID_LIST_jiabaoyu =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = PLSExtProc)
      (ORACLE_HOME = /u01/app/oracle/product/10.2.0/db_1)
      (PROGRAM = extproc)
    )
    (SID_DESC =
      (SID_NAME = orcl)
      (ORACLE_HOME = /u01/app/oracle/product/10.2.0/db_1)
    )
  )
jiabaoyu是我的监听名字,随便起,只是一个标识。默认是LISTENER,但是这个名字要和SID_LIST_后面的东西一致,这就表示他俩是一对。贾宝玉这个监听,占用一个mypc的1521端口,你这就是贾府大门了。所谓标准监听就是端口号为1521的监听。

那这个监听定义好了,怎么启动呢?
oracle给我们提供了一个命令lsnrctl。
在命令行输入lsnrctl进入
lsnrctl>
键入help可以查看它支持的所有命令,最常用的就三个,start [监听名字],stop [监听名字],stat
监听名字可以省略,代表start LISTENER 或者stop LISTENER.
要启动我们自己定义的jiabaoyu监听,可以用一下命令
LSNRCTL> start jiabaoyu
Starting /u01/app/oracle/product/10.2.0/db_1/bin/tnslsnr: please wait...

TNSLSNR for Linux: Version 10.2.0.1.0 - Production
System parameter file is /u01/app/oracle/product/10.2.0/db_1/network/admin/listener.ora
Log messages written to /u01/app/oracle/product/10.2.0/db_1/network/log/jiabaoyu.log
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1)))
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=mypc)(PORT=1521)))

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1)))
STATUS of the LISTENER
------------------------
Alias                     jiabaoyu
Version                   TNSLSNR for Linux: Version 10.2.0.1.0 - Production
Start Date                14-FEB-2015 10:35:35
Uptime                    0 days 0 hr. 0 min. 0 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u01/app/oracle/product/10.2.0/db_1/network/admin/listener.ora
Listener Log File         /u01/app/oracle/product/10.2.0/db_1/network/log/jiabaoyu.log
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=mypc)(PORT=1521)))
Services Summary...
Service "PLSExtProc" has 1 instance(s).
  Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...
Service "orcl" has 1 instance(s).
  Instance "orcl", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully


一大堆东西,其中如果你看到UNKNOWN,就代表这一监听是静态监听,这是判断静态监听还是动态监听的一种办法,还能真么判断呢?查看listener.ora文件,如果[监听名 ]和
SID_LIST_[监听名]是成对出现的,这代表是静态监听。

2.动态监听
很简单,只保留[监听名 ],把SID_LIST_[监听名]去掉,监听就变成动态坚挺了。他需要由oracle数据库的pmon主动想监听注册,然后这个监听才知道有这么一个服务。一般一分钟pmon就会主动向监听发起注册,当然也可以强制立即注册。命令是:
sql>alter system register

退出后用lsnrctl stat查看状态出现下列信息:

LSNRCTL for Linux: Version 10.2.0.1.0 - Production on 14-FEB-2015 10:42:43

Copyright (c) 1991, 2005, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1)))
STATUS of the LISTENER
------------------------
Alias                     jiabaoyu
Version                   TNSLSNR for Linux: Version 10.2.0.1.0 - Production
Start Date                14-FEB-2015 10:42:12
Uptime                    0 days 0 hr. 0 min. 30 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u01/app/oracle/product/10.2.0/db_1/network/admin/listener.ora
Listener Log File         /u01/app/oracle/product/10.2.0/db_1/network/log/jiabaoyu.log
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=mypc)(PORT=1521)))
Services Summary...
Service "orcl" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
Service "orclXDB" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
Service "orcl_XPT" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
The command completed successfully

其中READY就代表动态监听,当然也可以去查看listener.ora文件

3.非标准监听
这里只配置一动态非标准监听。在listener.ora加入下列内容:
jiabaoyu2=
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
      (ADDRESS = (PROTOCOL = TCP)(HOST = mypc)(PORT = 1522))
    )
  )
配置贾宝玉二号监听器,它的端口是1522。oracle的 pmon只认识1521端口的监听,怎么才能让他认识1522的监听呢?
我们得告诉oracle数据库,监听所在的位置,大家都知道,一个ip一个端口号就能唯一确认一个应用程序地址了。
我知道的两种告诉oracle 数据库非标准监听位置的办法:(其实原理一样,要是还有其他办法,估计原理也差不多)
方法一:
sql> alter system set local_listener='mypc:1522';--mypc是我的主机名,1522 使我们配置的端口号
sql>alter system register;
lsnrctl>stat

Copyright (c) 1991, 2005, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1)))
STATUS of the LISTENER
------------------------
Alias                     jiabaoyu2
Version                   TNSLSNR for Linux: Version 10.2.0.1.0 - Production
Start Date                14-FEB-2015 10:48:41
Uptime                    0 days 0 hr. 0 min. 23 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u01/app/oracle/product/10.2.0/db_1/network/admin/listener.ora
Listener Log File         /u01/app/oracle/product/10.2.0/db_1/network/log/jiabaoyu2.log
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=mypc)(PORT=1522)))
Services Summary...
Service "orcl" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
Service "orclXDB" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
Service "orcl_XPT" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
The command completed successfully


我们看到监听已经注册上了。

方法二:
大家都知道域名服务器,是把一个字符串解释成ip地址。oracle同样提供了一个名字转换的服务,叫做啥 tnsnames.ora,也存放在$ORACLE_HOME/network/admin下面。它也是用来吧一个名字解释成一些有用的信息,oracle 遇到不认识的地址,比如www.baidu.com,他就会去$ORACLE_HOME/network/admin/tnsnames.ora查找有没有他的定义,在我的知识范围呢,找不到,oracle服务器就只能报错了,当然好像还有神呢ldap,还有啥啥的,等学到再说。嘎嘎

最简单的办法就是把我们定义的jiabaoyu2直接拷过去,当然了我们可以起个其他名字
我们把
jiabaoyu2=
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
      (ADDRESS = (PROTOCOL = TCP)(HOST = mypc)(PORT = 1522))
    )
  )
改成
www.baidu.com=
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
      (ADDRESS = (PROTOCOL = TCP)(HOST = mypc)(PORT = 1522))
    )
  )
然后添加到tnsnames.ora中,然后运行下列命令
lsnrctl>start jiabaoyu2
sql> alter system set local_listener='www.baidu.com';-----运行时,oracle 服务器会去tnsnames.ora查找,看有没有这玩意,所以要先吧他加入tnsnames.ora
sql>alter system register;

[oracle@mypc admin]$ lsnrctl stat

LSNRCTL for Linux: Version 10.2.0.1.0 - Production on 14-FEB-2015 11:03:07

Copyright (c) 1991, 2005, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1)))
STATUS of the LISTENER
------------------------
Alias                     jiabaoyu2
Version                   TNSLSNR for Linux: Version 10.2.0.1.0 - Production
Start Date                14-FEB-2015 11:02:10
Uptime                    0 days 0 hr. 0 min. 57 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u01/app/oracle/product/10.2.0/db_1/network/admin/listener.ora
Listener Log File         /u01/app/oracle/product/10.2.0/db_1/network/log/jiabaoyu2.log
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=mypc)(PORT=1522)))
Services Summary...
Service "orcl" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
Service "orclXDB" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
Service "orcl_XPT" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
The command completed successfully


注册成功。哈哈 ,到此静态监听、动态监听、非标准监听配置完毕。

共享服务器,专用服务器下篇日志再说。