Oracle连接配置解读

安装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关联才能连接!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐