经过特地引起问题,聚焦问题,解决问题,并按部就班html
最后总结jdbc链接oracle12c中cdb和pdb的条件.java
软件环境:Redhat7.1+orcacle12cpython
全部动做前,先贴出listener.ora和tnsnames.ora配置:sql
# 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)) ) )
# 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)数据库
SQL> create user scott identified by scott; create user scott identified by scott * ERROR at line 1: ORA-65096: invalid common user or role name SQL> !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##开头)c#
SQL> grant dba to c##scott; Grant succeeded.
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
远程机子ping服务机网络
C:\Users\baby>tnsping 192.168.75.131:1521 TNS Ping Utility for 32-bit Windows: Version 11.2.0.1.0 - Production on 04-6月 - 2015 12:30:50 Copyright (c) 1997, 2010, Oracle. All rights reserved.
已使用的参数文件: 已使用 HOSTNAME 适配器来解析别名 尝试链接 (DESCRIPTION=(CONNECT_DATA=(SERVICE_NAME=))(ADDRESS=(PROTOCOL=TCP)(HOST =192.168.75.131)(PORT=1521))) OK (30 毫秒) 成功启动监听,并如上远程tnsping通监听,但未启动数据库服务时, Jdbc链接时就会报以下错误 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
LSNRCTL> exit [oracle@localhost Desktop]$ sqlplussys/sys_oracle as sysdba SQL*Plus: Release 12.1.0.2.0 Production onThu Jun 4 09:51:07 2015 Copyright (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.
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 status LSNRCTL for Linux: Version 12.1.0.2.0 - Production on 04-JUN-2015 10:06:15 Copyright (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
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("链接对象:"+conn);
链接对象:oracle.jdbc.driver.T4CConnection@3d3ee5c4oracle
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("链接对象:"+conn);
(失败,缘由:未启动名为pdborcl的pdb数据库)app
java.sql.SQLRecoverableException:ORA-01033: ORACLE initialization or shutdown in progressdom
[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 2015 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> show con_name; CON_NAME ------------------------------ PDBORCL SQL> startup Pluggable Database opened.
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("链接对象:"+conn);
链接对象:oracle.jdbc.driver.T4CConnection@76c20307
java.sql.SQLRecoverableException:ORA-01033: ORACLE initialization or shutdown in progress
[root@localhost ~]# systemctl status firewalld firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled) Active: active (running) since Thu 2015-06-04 12:32:06 CST; 8s ago Main 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...
致使结果:
C:\Users\baby>tnsping 192.168.75.131:1521 TNS Ping Utility for 32-bit Windows: Version 11.2.0.1.0 - Production on 04-6月 - 2015 12:33:13 Copyright (c) 1997, 2010, Oracle. All rights reserved.
已使用的参数文件: 已使用 HOSTNAME 适配器来解析别名 尝试链接 (DESCRIPTION=(CONNECT_DATA=(SERVICE_NAME=))(ADDRESS=(PROTOCOL=TCP)(HOST =192.168.75.131)(PORT=1521))) TNS-12535: TNS: 操做超时
(ps:防火墙命令由iptables迭代为firewalld;防火墙开启,且没取消阻挡端口,此时tnsping会等待好久,最后提示" TNS-12535:TNS: 操做超时")
至此,已经完成jdbc链接oracle12c的cdb和pdb数据库!
l jdbc链接cdb数据库时,url兼容2种模式:
ü "jdbc:oracle:thin:@192.168.75.131:1521:oracle12c"
ü "jdbc:oracle:thin:@192.168.75.131:1521/oracle12c"
l jdbc链接pdb数据库时url必须使用:" jdbc:oracle:thin:@192.168.75.131:1521/oracle12c"格式,若使用传统格式" jdbc:oracle:thin:@192.168.75.131:1521:oracle12c"则会报一下错误:
java.sql.SQLException: Listenerrefused the connection with the following error:
ORA-12505, TNS:listener does notcurrently know of SID given in connect descriptor
l 监听配置正确;
l ping [ip] 能ping通,客户端与服务端网络互通;
l 关闭防火墙(systemctlstop firewalld);
l 监听成功启用,且tnsping经过(OK (30 毫秒);
l tnsnames.ora配置正确(链接pdb数据库,注意添加pdb服务项,具体如何配置查看[准备阶段]);
l 监听连带的相应数据库成功开启;
l Jdbc链接字符串迭代更新为”jdbc:oracle:thin:@192.168.75.131:1521/pdborcl”而非" jdbc:oracle:thin:@192.168.75.131:1521:pdborcl"
Oracle 12C引入了CDB与PDB的新特性,在ORACLE12C数据库引入的多租用户环境(Multitenant Environment)中,容许一个数据库容器(CDB)承载多个可插拔数据库(PDB)。CDB全称为ContainerDatabase,中文翻译为数据库容器,PDB全称为Pluggable Database,便可插拔数据库。在ORACLE 12C以前,实例与数据库是一对一或多对一关系(RAC):即一个实例只能与一个数据库相关联,数据库能够被多个实例所加载。而实例与数据库不多是一对多的关系。当进入ORACLE 12C后,实例与数据库能够是一对多的关系。下面是官方文档关于CDB与PDB的关系图。