如何配置Oracle连接
本篇文章为大家展示了如何配置Oracle连接,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
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/baseLISTENER1 = (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 successfullylsnrctl 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 serviceNAME TYPE VALUE------------------------------------ ----------- ------------------------------service_names string orcl1400
也就是为什么使用orcl1400能连上数据库。这里我们使用静态连接试一下,也是可以连接数据库的(注意端口号和动态的不同)
sqlplus sys/password@iZbp1d4tisi44j6vxze02fZ:1522/gdn1400 as sysdbaSQL*Plus: Release 12.1.0.2.0 Production on Thu May 30 20:51:00 2019Copyright (c) 1982, 2014, Oracle. All rights reserved.Connected to:Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit ProductionWith the Partitioning, OLAP, Advanced Analytics and Real Application Testing optionsSQL>
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 -----> listener2NSN1521, iZbp1d4tisi44j6vxze02fZ, 1521, orcl1400 -----> listener1
两个别名指向了两个不同的listener,连接测试:
# sqlplus sys/password@nsn1521 as sysdbaSQL*Plus: Release 12.1.0.2.0 Production on Thu May 30 20:58:51 2019Copyright (c) 1982, 2014, Oracle. All rights reserved.Connected to:Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit ProductionWith the Partitioning, OLAP, Advanced Analytics and Real Application Testing optionsSQL># sqlplus sys/password@nsn1522 as sysdbaSQL*Plus: Release 12.1.0.2.0 Production on Thu May 30 20:58:55 2019Copyright (c) 1982, 2014, Oracle. All rights reserved.Connected to:Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit ProductionWith the Partitioning, OLAP, Advanced Analytics and Real Application Testing optionsSQL>
注意:tns依赖lsnrctl使用,可以理解为hostname这样的概念,注意tns的SERVICE_NAME等信息必须和listener关联才能连接!
上述内容就是如何配置Oracle连接,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。