jdbc-connect-oracle12c-pdb/cdb(jdbc连接oracle12c的pdb和cdb)
?
1?????? 本文簡(jiǎn)介:
通過特意引發(fā)問題,聚焦問題,解決問題,并循序漸進(jìn)
最后總結(jié)jdbc連接oracle12c中cdb和pdb的條件.
軟件環(huán)境:Redhat7.1+orcacle12c
?
?
2?????? 準(zhǔn)備階段:
2.1??listener.ora和tnsnames.ora配置:
?
所有動(dòng)作前,先貼出listener.ora和tnsnames.ora配置:
2.1.1? listener.ora>>
# listener.ora Network Configuration File: /u01/app/oracle/product/12.1.0/dbhome_1/network/admin/listener.ora # Generated by Oracle configuration tools. LISTENER =(DESCRIPTION_LIST =(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))))?
?
2.1.2 ?tnsnames.ora>>
?
# tnsnames.ora Network Configuration File: /u01/app/oracle/product/12.1.0/dbhome_1/network/admin/tnsnames.ora # Generated by Oracle configuration tools. PDBORCL =(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = pdborcl)))LISTENER_ORACLE12C =(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = oracle12c)))ORACLE12C =(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = oracle12c)))?
?
?
?
(注意添加PDBORCL)
2.1??用戶創(chuàng)建
2.1.1? 用戶創(chuàng)建:
?
SQL> create user scott identified by scott; create user scott identified by scott* ERROR at line 1: ORA-65096: invalid common user or role nameSQL> !oerr ora 65096 65096, 00000, "invalid common user or role name" // *Cause: An attempt was made to create a common user or role with a name // that wass not valid for common users or roles. In addition to // the usual rules for user and role names, common user and role // names must start with C## or c## and consist only of ASCII // characters. // *Action: Specify a valid common user or role name. //SQL> create user c##scott identified by scott;User created.?
?
?
?
(公共用戶名注意以c##開頭)
2.1.2 ?用戶授權(quán):
SQL> grant dba to c##scott; Grant succeeded.?
3 ? ? ??開始階段:
3.1??啟動(dòng)監(jiān)聽:
LSNRCTL> start Starting /u01/app/oracle/product/12.1.0/dbhome_1/bin/tnslsnr: please wait...TNSLSNR for Linux: Version 12.1.0.2.0 - Production System parameter file is /u01/app/oracle/product/12.1.0/dbhome_1/network/admin/listener.ora Log messages written to /u01/app/oracle/diag/tnslsnr/localhost/listener/alert/log.xml Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521)))Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))) STATUS of the LISTENER ------------------------ Alias LISTENER Version TNSLSNR for Linux: Version 12.1.0.2.0 - Production Start Date 04-JUN-2015 09:17:52 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/12.1.0/dbhome_1/network/admin/listener.ora Listener Log File /u01/app/oracle/diag/tnslsnr/localhost/listener/alert/log.xml Listening Endpoints Summary...(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))) The listener supports no services The command completed successfully?
遠(yuǎn)程機(jī)子ping服務(wù)機(jī)
C:\Users\baby>tnsping 192.168.75.131:1521TNS Ping Utility for 32-bit Windows: Version 11.2.0.1.0 - Production on 04-6月 - 2015 12:30:50Copyright (c) 1997, 2010, Oracle. All rights reserved.?
已使用的參數(shù)文件:已使用 HOSTNAME 適配器來解析別名 嘗試連接 (DESCRIPTION=(CONNECT_DATA=(SERVICE_NAME=))(ADDRESS=(PROTOCOL=TCP)(HOST =192.168.75.131)(PORT=1521))) OK (30 毫秒) 成功啟動(dòng)監(jiān)聽,并如上遠(yuǎn)程tnsping通監(jiān)聽,但未啟動(dòng)數(shù)據(jù)庫服務(wù)時(shí), Jdbc連接時(shí)就會(huì)報(bào)如下錯(cuò)誤java.sql.SQLRecoverableException: Listener refused the connection with the following error: ORA-12514, TNS:listener does not currently know of service requested in connect descriptor?
3.2 ?啟動(dòng)數(shù)據(jù)庫服務(wù):
?
LSNRCTL> exit [oracle@localhost Desktop]$ sqlplussys/sys_oracle as sysdbaSQL*Plus: Release 12.1.0.2.0 Production onThu Jun 4 09:51:07 2015Copyright (c) 1982, 2014, Oracle. All rights reserved.Connected to an idle instance.SQL> starttup SP2-0042: unknown command"starttup" - rest of line ignored. SQL> startup ORACLE instance started.Total System Global Area 411041792 bytes Fixed Size 2925024 bytes Variable Size 276827680 bytes Database Buffers 125829120 bytes Redo Buffers 5459968 bytes Database mounted. Database opened.?
3.3?查看監(jiān)聽狀態(tài)
SQL> exit Disconnected from Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options [oracle@localhost Desktop]$ lsnrctl statusLSNRCTL for Linux: Version 12.1.0.2.0 - Production on 04-JUN-2015 10:06:15Copyright (c) 1991, 2014, Oracle. All rights reserved.Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))) STATUS of the LISTENER ------------------------ Alias LISTENER Version TNSLSNR for Linux: Version 12.1.0.2.0 - Production Start Date 04-JUN-2015 09:17:52 Uptime 0 days 0 hr. 48 min. 22 sec Trace Level off Security ON: Local OS Authentication SNMP OFF Listener Parameter File /u01/app/oracle/product/12.1.0/dbhome_1/network/admin/listener.ora Listener Log File /u01/app/oracle/diag/tnslsnr/localhost/listener/alert/log.xml Listening Endpoints Summary...(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521)))(DESCRIPTION=(ADDRESS=(PROTOCOL=tcps)(HOST=localhost)(PORT=5500))(Security=(my_wallet_directory=/u01/app/oracle/admin/oracle12c/xdb_wallet))(Presentation=HTTP)(Session=RAW)) Services Summary... Service "oracle12c" has 1 instance(s).Instance "oracle12c", status READY, has 1 handler(s) for this service... Service "oracle12cXDB" has 1 instance(s).Instance "oracle12c", status READY, has 1 handler(s) for this service... Service "pdborcl" has 1 instance(s).Instance "oracle12c", status READY, has 1 handler(s) for this service... The command completed successfully?
3.4 ?Jdbc連接cdb數(shù)據(jù)庫實(shí)例oracle12c過程:
Connection conn = null; Statement stmt = null; Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.75.131:1521/oracle12c", "c##scott", "scott"); stmt = conn.createStatement(); System.out.println("連接對(duì)象:"+conn);?
3.5 ?Jdbc連接cdb數(shù)據(jù)庫實(shí)例oracle12c結(jié)果:
連接對(duì)象:oracle.jdbc.driver.T4CConnection@3d3ee5c4
3.6 ?Jdbc連接pdb數(shù)據(jù)庫實(shí)例oracle12c過程:
Connection conn = null; Statement stmt = null; Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.75.131:1521/pdborcl", " c##scott ", "scott"); stmt = conn.createStatement(); System.out.println("連接對(duì)象:"+conn);?
3.7 ?Jdbc連接pdb數(shù)據(jù)庫實(shí)例oracle12c結(jié)果:
(失敗,原因:未啟動(dòng)名為pdborcl的pdb數(shù)據(jù)庫)
?
java.sql.SQLRecoverableException:ORA-01033: ORACLE initialization or shutdown in progress
?
?
3.8 ?啟動(dòng)名為pdborcl的pdb數(shù)據(jù)庫:
[oracle@localhost Desktop]$ sqlplus sys/sys_oracle@pdborcl as sysdba;SQL*Plus: Release 12.1.0.2.0 Production on Thu Jun 4 10:09:18 2015Copyright (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 optionsSQL> show con_name;CON_NAME ------------------------------ PDBORCL SQL> startup Pluggable Database opened.?
3.9 ?再次嘗試jdbc連接pdb數(shù)據(jù)庫
Connection conn = null; Statement stmt = null; Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.75.131:1521/pdborcl", "c##scott", "scott"); stmt = conn.createStatement(); System.out.println("連接對(duì)象:"+conn);?
3.10 ?再次嘗試jdbc連接pdb數(shù)據(jù)庫結(jié)果:
?
連接對(duì)象:oracle.jdbc.driver.T4CConnection@76c20307
?
4 ? ? ??引發(fā)問題集中營:
4.1??pdb數(shù)據(jù)庫未開啟引發(fā)問題:
?
java.sql.SQLRecoverableException:ORA-01033: ORACLE initialization or shutdown in progress
?
4.2??防火墻阻擋引發(fā)問題:
[root@localhost ~]# systemctl status firewalld firewalld.service - firewalld - dynamic firewall daemonLoaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled)Active: active (running) since Thu 2015-06-04 12:32:06 CST; 8s agoMain PID: 6060 (firewalld)CGroup: /system.slice/firewalld.service└─6060 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid Jun 04 12:32:06 localhost.localdomain systemd[1]: Started firewalld - dynamic...?
導(dǎo)致結(jié)果:
C:\Users\baby>tnsping 192.168.75.131:1521TNS Ping Utility for 32-bit Windows: Version 11.2.0.1.0 - Production on 04-6月 - 2015 12:33:13Copyright (c) 1997, 2010, Oracle. All rights reserved.?
已使用的參數(shù)文件:已使用 HOSTNAME 適配器來解析別名 嘗試連接 (DESCRIPTION=(CONNECT_DATA=(SERVICE_NAME=))(ADDRESS=(PROTOCOL=TCP)(HOST =192.168.75.131)(PORT=1521))) TNS-12535: TNS: 操作超時(shí)(ps:防火墻命令由iptables迭代為firewalld;防火墻開啟,且沒取消阻擋端口,此時(shí)tnsping會(huì)等待很久,最后提示" TNS-12535:TNS: 操作超時(shí)")
?
至此,已經(jīng)完成jdbc連接oracle12c的cdb和pdb數(shù)據(jù)庫!
?
4.3 ?jdbc連接字符串引發(fā)問題:
l? jdbc連接cdb數(shù)據(jù)庫時(shí),url兼容2種模式:
ü? "jdbc:oracle:thin:@192.168.75.131:1521:oracle12c"
ü? "jdbc:oracle:thin:@192.168.75.131:1521/oracle12c"
l? jdbc連接pdb數(shù)據(jù)庫時(shí)url必須使用:" jdbc:oracle:thin:@192.168.75.131:1521/oracle12c"格式,若使用傳統(tǒng)格式" jdbc:oracle:thin:@192.168.75.131:1521:oracle12c"則會(huì)報(bào)一下錯(cuò)誤:
?
java.sql.SQLException: Listenerrefused the connection with the following error:
ORA-12505, TNS:listener does notcurrently know of SID given in connect descriptor
?
5 ? ? ??總結(jié)概要:
5.1??tnsping能ping通的條件:
l? 監(jiān)聽配置正確;
l? ping [ip] 能ping通,客戶端與服務(wù)端網(wǎng)絡(luò)互通;
l? 關(guān)閉防火墻(systemctlstop firewalld);
5.2??jdbc連接oracle12的數(shù)據(jù)庫需要具備的條件:
l? 監(jiān)聽成功啟用,且tnsping通過(OK (30 毫秒);
l? tnsnames.ora配置正確(連接pdb數(shù)據(jù)庫,注意添加pdb服務(wù)項(xiàng),具體如何配置查看[準(zhǔn)備階段]);
l? 監(jiān)聽連帶的相應(yīng)數(shù)據(jù)庫成功開啟;
l? Jdbc連接字符串迭代更新為”jdbc:oracle:thin:@192.168.75.131:1521/pdborcl”而非" jdbc:oracle:thin:@192.168.75.131:1521:pdborcl"
?
?
6 ? ? ??知識(shí)補(bǔ)充:
6.1??Oracle12c新特性之cdb&pdb:
Oracle 12C引入了CDB與PDB的新特性,在ORACLE12C數(shù)據(jù)庫引入的多租用戶環(huán)境(Multitenant Environment)中,允許一個(gè)數(shù)據(jù)庫容器(CDB)承載多個(gè)可插拔數(shù)據(jù)庫(PDB)。CDB全稱為ContainerDatabase,中文翻譯為數(shù)據(jù)庫容器,PDB全稱為Pluggable Database,即可插拔數(shù)據(jù)庫。在ORACLE 12C之前,實(shí)例與數(shù)據(jù)庫是一對(duì)一或多對(duì)一關(guān)系(RAC):即一個(gè)實(shí)例只能與一個(gè)數(shù)據(jù)庫相關(guān)聯(lián),數(shù)據(jù)庫可以被多個(gè)實(shí)例所加載。而實(shí)例與數(shù)據(jù)庫不可能是一對(duì)多的關(guān)系。當(dāng)進(jìn)入ORACLE 12C后,實(shí)例與數(shù)據(jù)庫可以是一對(duì)多的關(guān)系。下面是官方文檔關(guān)于CDB與PDB的關(guān)系圖。
?
?
6.2??dba更多支持:
http://www.cnblogs.com/dingyingsi/p/3604789.html
http://www.cnblogs.com/kerrycode/p/3386917.html
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/pcbaby-ch/p/jdbc-connect-oracle12c-pdb-cdb.html
總結(jié)
以上是生活随笔為你收集整理的jdbc-connect-oracle12c-pdb/cdb(jdbc连接oracle12c的pdb和cdb)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java实验报告三
- 下一篇: 读写属性/if判断那点事/数组