使用SID代替连接字符串上的服务名称时,cx_Oracle无法连接 [英] cx_Oracle doesn't connect when using SID instead of service name on connection string

查看:667
本文介绍了使用SID代替连接字符串上的服务名称时,cx_Oracle无法连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的连接字符串

I have a connection string that looks like this

con_str = "myuser/mypass@oracle.sub.example.com:1521/ora1"

其中ora1是我的数据库的SID.在SQL Developer中使用此信息可以正常工作,这意味着我可以毫无问题地进行连接和查询.

Where ora1 is the SID of my database. Using this information in SQL Developer works fine, meaning that I can connect and query without problems.

但是,如果我尝试使用此字符串连接到Oracle,它将失败.

However, if I attempt to connect to Oracle using this string, it fails.

cx_Oracle.connect(con_str)

DatabaseError:  ORA-12514:  TNS:listener  does  not  currently  know  of  service  requested  in  connect  descriptor

但是,如果ora1是服务名称,则此连接字符串格式有效.

This connection string format works if the ora1 is a service name, though.

我看到其他问题似乎与我的问题相反(它适用于SID,但不适用于服务名称)

I have seen other questions that seem to have the reverse of my problem (it works with SID, but not Service name)

  • Using Oracle Service Names with SQLAlachemy
  • Oracle SID and Service name; connection problems
  • cx_Oracle & Connecting to Oracle DB Remotely

使用cx_Oracle,使用SID而非服务名称连接到Oracle的正确方法是什么?无需调整TNSNAMES.ORA文件怎么办?我的应用程序内部分发给许多用户,并且在与Windows计算机上没有管理员特权的用户打交道时,对TNSNAMES文件进行更改并不理想.另外,当我使用服务名称时,我完全不需要触摸此文件,而是希望它保持这种状态.

What is the proper way to connect to Oracle, using cx_Oracle, using an SID and not a service name? How do I do this without the need to adjust the TNSNAMES.ORA file? My application is distributed to many users internally and making changes to the TNSNAMES file is less than ideal when dealing with users without administrator privileges on their Windows machines. Additionally, when I use service name, I don't need to touch this file at all and would like it keep it that way.

推荐答案

在类似的情况下,我能够使用

I a similar scenario, I was able to connect to the database by using cx_Oracle.makedsn() to create a dsn string with a given SID (instead of the service name):

dsnStr = cx_Oracle.makedsn("oracle.sub.example.com", "1521", "ora1")

这将返回类似

(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=oracle.sub.example.com)(PORT=1521)))(CONNECT_DATA=(SID=ora1)))

然后可以与 cx_Oracle.connect() 一起使用以连接到数据库:

which can then be used with cx_Oracle.connect() to connect to the database:

con = cx_Oracle.connect(user="myuser", password="mypass", dsn=dsnStr)
print con.version
con.close()

这篇关于使用SID代替连接字符串上的服务名称时,cx_Oracle无法连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆