安装ORACLE数据库软件,dbca安装数据库后,需要配置listener连接数据库。这里有一些概念比较难理解,记录一些分析实战结论。
从连接端讲起。
1 连接数据库的方式
oracle的连接串有几部分构成,这里就按sqlplus为例,一个完成的连接串遵循下面格式
sqlplus 用户名/密码@主机:端口号/SID 可选as sysdba
下面先讲只有listener没有tns的情况
2 listener
使用listener连接需要配置完整连接信息,这里分为两种连接方式,我们看一个listener的例子:
(带sid的listener使用netmgr增加listener的datavase services即可出现sid的配置)
LISTENER2 = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = iZbp1d4tisi44j6vxze02fZ)(PORT = 1522)) ) SID_LIST_LISTENER2 = (SID_LIST = (SID_DESC = (GLOBAL_DBNAME = gdn1400) (ORACLE_HOME = /fdisk1/oracle1400/base/dbhome_1) (SID_NAME = orcl1400) ) ) ADR_BASE_LISTENER2 = /fdisk1/oracle1400/base LISTENER1 = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = iZbp1d4tisi44j6vxze02fZ)(PORT = 1521)) ) ADR_BASE_LISTENER1 = /fdisk1/oracle1400/base
这里有两个listener,有sid的叫做静态listener,没有sid的叫做动态listener。在查看状态时存在区别:
lsnrctl status listener1 ... Services Summary... Service "orcl1400" has 1 instance(s). Instance "orcl1400", status READY, has 1 handler(s) for this service... Service "orcl1400XDB" has 1 instance(s). Instance "orcl1400", status READY, has 1 handler(s) for this service... The command completed successfully lsnrctl status listener2 ... Services Summary... Service "gdn1400" has 1 instance(s). Instance "orcl1400", status UNKNOWN, has 1 handler(s) for this service... The command completed successfully
注意对于lsnrctl来说,service的名字是global database name
我们在看一个连接串:
sqlplus sys/password@iZbp1d4tisi44j6vxze02fZ:1521/orcl1400 as sysdba
这个连接串中最需要关注的就是服务名了,这里是orcl1400。
注意!:这个服务名必须由listener中的某一个提供,这里listener2的服务名提供的是gdn1400,而listener1没有提供服务名。那么如何连接数据库呢?答案就是走listener1的连接会去数据库中动态的查询服务名(所以叫做动态连接)
SQL> show parameter service NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ service_names string orcl1400
也就是为什么使用orcl1400能连上数据库。这里我们使用静态连接试一下,也是可以连接数据库的(注意端口号和动态的不同)
sqlplus sys/password@iZbp1d4tisi44j6vxze02fZ:1522/gdn1400 as sysdba SQL*Plus: Release 12.1.0.2.0 Production on Thu May 30 20:51:00 2019 Copyright (c) 1982, 2014, Oracle. All rights reserved. Connected to: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options SQL>
3 TNS
那么tns是什么呢?我们看下这个连接串。
sqlplus sys/password@iZbp1d4tisi44j6vxze02fZ:1521/orcl1400 as sysdba
@后面的信息很多,能否省略呢?比如
sqlplus sys/password@tns1400 as sysdba
这样看起来简洁很多也便于管理,TNS即实现了这个功能,我们看一个tnsnames.ora的配置:
NSN1522 = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = iZbp1d4tisi44j6vxze02fZ)(PORT = 1522)) ) (CONNECT_DATA = (SERVICE_NAME = gdn1400) ) ) NSN1521 = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = iZbp1d4tisi44j6vxze02fZ)(PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = orcl1400) ) )
这里可以看到,最左面的NSN1522和NSN1521就是我们可以@的服务名字。内部的映射信息是指向listener的,这里的servicename要和上面的global database name对应上!
NSN1522, iZbp1d4tisi44j6vxze02fZ, 1522, gdn1400 -----> listener2 NSN1521, iZbp1d4tisi44j6vxze02fZ, 1521, orcl1400 -----> listener1
两个别名指向了两个不同的listener,连接测试:
# sqlplus sys/password@nsn1521 as sysdba SQL*Plus: Release 12.1.0.2.0 Production on Thu May 30 20:58:51 2019 Copyright (c) 1982, 2014, Oracle. All rights reserved. Connected to: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options SQL> # sqlplus sys/password@nsn1522 as sysdba SQL*Plus: Release 12.1.0.2.0 Production on Thu May 30 20:58:55 2019 Copyright (c) 1982, 2014, Oracle. All rights reserved. Connected to: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options SQL>
注意:tns依赖lsnrctl使用,可以理解为hostname这样的概念,注意tns的SERVICE_NAME等信息必须和listener关联才能连接!
本文Oracle连接配置解读到此结束。不要像玻璃那样脆弱。有的人眼睛总盯着自我,所以长不高看不远;总是喜欢怨天尤人,也使别人无比厌烦。没有苦中苦,哪来甜中甜?不要像玻璃那样脆弱,而应像水晶一样透明,太阳一样辉煌,腊梅一样坚强。小编再次感谢大家对我们的支持!