我正在尝试使用cx_oracle从python连接数据库服务器。我已将连接字符串构造为
dbconfig['schema']+"/"+dbconfig['password']+"@"+dbconfig["ip"]+"/"+dbconfig['sid']
其中dbconfig是具有数据库连接所需凭据的字典
我可以使用VPN连接连接到其中一台服务器。但是当我尝试连接到本地网络中的一个服务器时,它显示" TNS:监听器当前不知道连接描述符中请求的服务"错误。
我甚至用适当的sid修改了tnsnames.ora文件,即使在其他情况下不需要它。之后,我能够使用sqlplus登录。但是对python代码没有好运。
答案 0 :(得分:4)
您正在使用HOSTNAME连接类型或所谓的"轻松连接"连接到数据库。 - EZCONNECT
忘记场景中的tnsnames.ora。在执行HOSTNAME连接时未使用。
您需要让您的SERVICE_NAME正确。 SERVICE_NAME与SID不同。
SID是dbnode上服务器端进程的名称。固定到dbnode。
SERVICE_NAME是一个缩减版。假设我在两个dbnodes(RAC)(共享存储)上运行一个数据库,集群将公开一个服务,以便应用程序可以连接 - SERVICE_NAME。应用程序不必了解底层架构。
connection by service_name: user/pass@hostname:port/SERVICE_NAME
connection by sid user/pass@hostname:port:SID -- NOTE the colon!
如果您运行的是Oracle Database 12c,则必须指定SERVICE_NAME,而不是SID。永远不要使用SID!您的应用程序需要数据库服务!
SQL>-- identify the database services (you can have as many as you like)
SQL>-- finance,betting,app1,app2...appN,etc
SQL>-- YES you 'should' run ALL your applications in ONE database (schema separation or pluggable)
SQL>show parameter service_names
REM try EZCONNECT through sqlplus
REM Note! Add ..(EZCONNECT,TNSNAMES).. to your sqlnet.ora file if not already in.
C:\>sqlplus user/pass@hostname:PORT/SERVICE_NAME