tnsnames.ora中的配置如下,一个是12c,一个是11g:
#oracle12c
ORA12C =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.2.39)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = ora12c.localdomain)
)
)
#oracle11g
ORCDATA =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.2.186)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcdata)
)
)
问题现象:
用SQLplus连接都正常,可以访问数据库。
C:\SQLPLUS system/manager@orcdata
SQL*Plus: Release 11.2.0.1.0 Production on Fri Jul 2 12:48:30 2021
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL>
C:\SQLPLUS system/manager@ORA12C
SQL*Plus: Release 11.2.0.1.0 Production on Fri Jul 2 12:49:05 2021
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
SQL>
用cx_Oracle访问,访问11g的数据库正常,但是访问12c的数据库报错:
cx_Oracle.DatabaseError: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
测试数据库连接的程序如下,都用tnsname 连接:
def test_myoracle():
#11g的数据库,用tns name 连接
#db = cx_Oracle.connect('system', 'manager', '192.168.2.186:1521/orcdata')
#12c的数据库,用tns name 连接
#db = cx_Oracle.connect('system', 'manager', '192.168.2.39:1521/ORA12C')
cursor = db.cursor()
cursor.execute("select count(*) from tab")
result = cursor.fetchall()
print(result)
cursor.close()
db.close()
解决方法:
在网上也没有找到办法,灵机一动,想用服务名测试一下,居然成功了,但是不知道为什么??
用服务名连接:ora12c.localdomain
访问正常,不报cx_Oracle.DatabaseError: ORA-12514错误。
#12C的数据库,用service name 连接
db = cx_Oracle.connect('system', 'manager', '192.168.2.39:1521/ora12c.localdomain')
cursor = db.cursor()
cursor.execute("select count(*) from tab")
result = cursor.fetchall()
print(result)
cursor.close()
db.close()
推测:
oracle client是11g的,推测cx_Oracle对11g和12c连接数据库还是有区别。或者是和oracle client版本相关。
在cx_oracle官网上,也没有找到对应数据库版本区别的解释。
花了一天的时间,连接12c数据库,记录之。
https://cx-oracle.readthedocs.io/en/latest/user_guide/connection_handling.html
如有高手指点一下,不胜感激。