jdbc-connect-oracle12c-pdb/cdb(jdbc连接oracle12c的pdb和cdb)
1 本文简介:
通过特意引发问题,聚焦问题,解决问题,并循序渐进
最后总结jdbc连接oracle12c中cdb和pdb的条件.
软件环境:Redhat7.1+orcacle12c
2 准备阶段:
2.1 listener.ora和tnsnames.ora配置:
所有动作前,先贴出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 用户创建
2.1.1 用户创建:
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##开头)
2.1.2 用户授权:
SQL> grant dba to c##scott; Grant succeeded.
3 开始阶段:
3.1 启动监听:
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
3.2 启动数据库服务:
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.
3.3 查看监听状态
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
3.4 Jdbc连接cdb数据库实例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("连接对象:"+conn);
3.5 Jdbc连接cdb数据库实例oracle12c结果:
连接对象:oracle.jdbc.driver.T4CConnection@3d3ee5c4
3.6 Jdbc连接pdb数据库实例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("连接对象:"+conn);
3.7 Jdbc连接pdb数据库实例oracle12c结果:
(失败,原因:未启动名为pdborcl的pdb数据库)
java.sql.SQLRecoverableException:ORA-01033: ORACLE initialization or shutdown in progress
3.8 启动名为pdborcl的pdb数据库:
[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.
3.9 再次尝试jdbc连接pdb数据库
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);
3.10 再次尝试jdbc连接pdb数据库结果:
连接对象:oracle.jdbc.driver.T4CConnection@76c20307
4 引发问题集中营:
4.1 pdb数据库未开启引发问题:
java.sql.SQLRecoverableException:ORA-01033: ORACLE initialization or shutdown in progress
4.2 防火墙阻挡引发问题:
[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[]: 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数据库!
4.3 jdbc连接字符串引发问题:
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
5 总结概要:
5.1 tnsping能ping通的条件:
l 监听配置正确;
l ping [ip] 能ping通,客户端与服务端网络互通;
l 关闭防火墙(systemctlstop firewalld);
5.2 jdbc连接oracle12的数据库需要具备的条件:
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"
6 知识补充:
6.1 Oracle12c新特性之cdb&pdb:
Oracle 12C引入了CDB与PDB的新特性,在ORACLE12C数据库引入的多租用户环境(Multitenant Environment)中,允许一个数据库容器(CDB)承载多个可插拔数据库(PDB)。CDB全称为ContainerDatabase,中文翻译为数据库容器,PDB全称为Pluggable Database,即可插拔数据库。在ORACLE 12C之前,实例与数据库是一对一或多对一关系(RAC):即一个实例只能与一个数据库相关联,数据库可以被多个实例所加载。而实例与数据库不可能是一对多的关系。当进入ORACLE 12C后,实例与数据库可以是一对多的关系。下面是官方文档关于CDB与PDB的关系图。
6.2 dba更多支持:
jdbc-connect-oracle12c-pdb/cdb(jdbc连接oracle12c的pdb和cdb)的更多相关文章
- (转)PLSQL Developer 12.0.7连接Oracle12c数据库
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/sl1992/article/details/80489413 1.下载安装PL/SQL Develo ...
- 【JDBC】java程序通过jdbc连接oracle数据库方法
版权声明:本文为博主原创文章(原文:blog.csdn.net/clark_xu 徐长亮的专栏).未经博主同意不得转载. https://blog.csdn.net/u011538954/articl ...
- JDBC课程1-实现Driver接口连接mysql数据库、通用的数据库连接方法(使用文件jdbc.properties)
package day_18; import jdk.internal.util.xml.impl.Input; import org.junit.Test; import java.io.Input ...
- PLSQL连接oracle12c
一.本人以前都是使用oracle10g客户端,PLSQL连接oracle12c时报错 确认配置完全没问题,纠结了不少时间.后来查的是oracle客户端太老了,版本11.2.0.2.0以上即可 二.下载 ...
- 数据库程序接口——JDBC——API解读第一篇——建立连接的核心对象
结构图 核心对象 Driver Java通过Driver接口表示驱动,每种类型的数据库通过实现Driver接口提供自己的Driver实现类. Driver由属性,操作,事件三部分组成. 属性 公共属性 ...
- Universe Design Tool Using JDBC connect Sybase/Oracle Get Error
一.针对Sybase 1 使用SAP Universe 设计工具连接Sybase数据库报错,报错如下: “CS: Java Class not found in classpath : com.syb ...
- com.microsoft.sqlserver.jdbc.SQLServerException: Socket closed 或者 该连接已关闭
com.microsoft.sqlserver.jdbc.SQLServerException: Socket closed 或者 该连接已关闭 解决方案: DBUtil公共方法如下: package ...
- jdbc 数据库连接 长时间空闲 断开连接 ApplicationContext.xml
数据库连接 长时间空闲 断开连接solution: <property name="validationQuery" value="select 1"/& ...
- 解决Mybatis连接Sql server 出现 Cannot load JDBC driver class 'com.mysql.jdbc.Driver '的问题
tomcat启动的时候没有错误,但是进行数据库操作就会有错误. 在网上找了很久 好不容易找到解决方法 转自 http://blog.csdn.net/ro_bot/article/details/5 ...
随机推荐
- SR4000(二)
返回相位(用于测距离,一个全相位代表5m) D=3*10^8/2f(60M)=5m full-phase(0xffff) 返回LED反射光的振幅和背景光均值 无效数据: B太大 幅度(也是16bit ...
- Flex 箭头(军标)库封装完成
封装的一个月,在这个月期间还完成的一些其它的工作:公司有规定不能公布代码,我可以讲一下大致的流程,真的很抱歉! 1.用B样条曲线画箭头的两侧的曲线,这个要注意了,不要贝塞尔曲线,因为贝塞尔曲线的算法会 ...
- Linux中的权限管理
touch 11.txt(创建了一个文件) chown zhangsan:zhangsan 11.txt ll(可看到11.txt的属主和属组都改为了zhangsan) useradd lisi(添加 ...
- 关于sharepoint事件接收器中properties.AfterProperties[""].Tostring()取值的问题。
这个这个属性是不能获取到中文的意思,他是获取AfterProperties的集合的值. string name=properties.AfterProperties["登录人"]. ...
- C# Dynamic特性
C# 4.0 dynamic:声明动态对象 dynamic关键字用于声明一个动态对象,然后通过该动态对象去调用方法或读写属性.这是C#4.0 添加的特性.官方解释:dynamic类型是帮助我们绕过编译 ...
- lru cache java
http://www.acmerblog.com/leetcode-lru-cache-lru-5745.html acm之家的讲解是在是好,丰富 import java.util.LinkedHas ...
- 杨氏矩阵 leecode 提
提交网址https://oj.leetcode.com/problems/search-a-2d-matrix/ 有个矩阵中的数,从左向右递增,从上而下递增,快速查找是一个数是是否存在,剑指offer ...
- 使用Windows Azure创建Windows系统虚拟机-上
创建虚拟机来运行Windows 本教程介绍了如何轻松创建运行Windows 的 Azure虚拟机(VM),用作来自Azure管理门户中映像图库的Windows 服务器映像.映像图库提供了多种图像,包括 ...
- CSS 中常用的选择器(选择符)
一.标签选择器:直接将HTML标签(Tag)作为选择器,可以是p.h1.dl.strong等HTML标签.如: p { font:12px;}em { color:blue;}dl { float:l ...
- php-fpm正在生成页面时,浏览器刷新后,php-fpm会退出吗?
好久没写博客了,因为没有啥可写. 之所以有此疑问,是因为看了一篇大牛的文章:PHP升级导致系统负载过高问题分析.看完后,其中有些文字触发了我这个想法,也想验证一下. 方案,用tcpdump抓包,用st ...