安裝ORACLE數據庫軟件,dbca安裝數據庫后,需要配置listener連接數據庫。這里有一些概念比較難理解,記錄一些分析實戰結論。
從連接端講起。
1 連接數據庫的方式
oracle的連接串有幾部分構成,這里就按sqlplus為例,一個完成的連接串遵循下面格式
1
|
sqlplus 用戶名/密碼@主機:端口號/SID 可選 as sysdba |
下面先講只有listener沒有tns的情況
2 listener
使用listener連接需要配置完整連接信息,這里分為兩種連接方式,我們看一個listener的例子:
(帶sid的listener使用netmgr增加listener的datavase services即可出現sid的配置)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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。在查看狀態時存在區別:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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
我們在看一個連接串:
1
|
sqlplus sys/ password @iZbp1d4tisi44j6vxze02fZ:1521/orcl1400 as sysdba |
這個連接串中最需要關注的就是服務名了,這里是orcl1400。
注意!:這個服務名必須由listener中的某一個提供,這里listener2的服務名提供的是gdn1400,而listener1沒有提供服務名。那么如何連接數據庫呢?答案就是走listener1的連接會去數據庫中動態的查詢服務名(所以叫做動態連接)
1
2
3
4
5
|
SQL> show parameter service NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ service_names string orcl1400 |
也就是為什么使用orcl1400能連上數據庫。這里我們使用靜態連接試一下,也是可以連接數據庫的(注意端口號和動態的不同)
1
2
3
4
5
6
7
8
9
10
11
12
|
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是什么呢?我們看下這個連接串。
1
|
sqlplus sys/ password @iZbp1d4tisi44j6vxze02fZ:1521/orcl1400 as sysdba |
@后面的信息很多,能否省略呢?比如
1
|
sqlplus sys/ password @tns1400 as sysdba |
這樣看起來簡潔很多也便于管理,TNS即實現了這個功能,我們看一個tnsnames.ora的配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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對應上!
1
2
|
NSN1522, iZbp1d4tisi44j6vxze02fZ, 1522, gdn1400 -----> listener2 NSN1521, iZbp1d4tisi44j6vxze02fZ, 1521, orcl1400 -----> listener1 |
兩個別名指向了兩個不同的listener,連接測試:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# 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關聯才能連接!
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://segmentfault.com/a/1190000020395535