cx_Oracle doesn't connect when using SID instead of service name on connection string

后端 未结 7 805
你的背包
你的背包 2020-12-13 09:14

I have a connection string that looks like this

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

Where ora1 is the

相关标签:
7条回答
  • 2020-12-13 09:50

    I also met this issue. The solution is:

    1: get the service name at tnsnames.ora
    2: put the service name in
    con_str = "myuser/mypass@oracle.sub.example.com:1521/ora1"
    
    0 讨论(0)
  • 2020-12-13 09:54

    SID's may not be easily accessible or you might not have it created for your database.

    In my case, I'm working from the client side requesting access to a cloud database so creating an SID didn't really make sense.

    Instead, you might have a string that looks similar to this:

    "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = something.cloud.company)
    (PORT = 12345)) (ADDRESS = (PROTOCOL = TCP)(HOST = something.cloud.company)
    (PORT = 12345)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = 
    something.company)))"
    

    You can use it in replacement of the SID.

    connection = cx_Oracle.connect("username", "pw", "(DESCRIPTION = (ADDRESS = 
                    (PROTOCOL = TCP)(HOST = something.cloud.company)(PORT = 12345)) (ADDRESS = 
                    (PROTOCOL = TCP)(HOST = something.cloud.company)(PORT = 12345)) 
                    (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = something.company)))")
    
    0 讨论(0)
  • 2020-12-13 09:55

    I thought during a while that I would not be able to use Magic SQL (%sql, %%sql) because of service name issue in connection that would force to use the alternative way described above with cx_Oracle.connect(), cx_Oracle.makedsn()... I finally found a solution working for me: declare and set a variable for the service name first and then use it in the command (since not working if literal string for service name put in the command !)

    import cx_Oracle
    
    user='youruser'
    pwd='youruserpwd'
    dbhost='xx.xx.xx.xx'
    service='yourservice'
    
    %load_ext sql
    %sql oracle+cx_oracle://$user:$pwd@$dbhost:1521/?service_name=$service
    

    output (what you get in successful connection):

    u'Connected: youruser@'
    
    0 讨论(0)
  • 2020-12-13 09:56

    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")
    

    This returns something like

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

    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()
    
    0 讨论(0)
  • 2020-12-13 09:58

    It still may not work. You need to take the output of dsnStr and modify the string by replacing SID with SERVICE_NAME and use that variable in the con string. This procedure worked for me.

    0 讨论(0)
  • 2020-12-13 10:07

    For those looking for how to specify service_name instead of SID.

    From changelog for SQLAlchemy 1.0.0b1 (released on March 13, 2015):

    [oracle] [feature] Added support for cx_oracle connections to a specific service name, as opposed to a tns name, by passing ?service_name=<name> to the URL. Pull request courtesy Sławomir Ehlert.

    The change introduces new, Oracle dialect specific option service_name which can be used to build connect string like this:

    from sqlalchemy import create_engine
    from sqlalchemy.engine import url
    
    connect_url = url.URL(
        'oracle+cx_oracle',
        username='some_username',
        password='some_password',
        host='some_host',
        port='some_port',
        query=dict(service_name='some_oracle_service_name'))
    
    engine = create_engine(connect_url)
    
    0 讨论(0)
提交回复
热议问题