cx_Oracle & Connecting to Oracle DB Remotely

后端 未结 6 1916
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 04:59

How do you connect to a remote server via IP address in the manner that TOAD, SqlDeveloper, are able to connect to databases with just the ip address, username, SID and pass

相关标签:
6条回答
  • 2020-11-28 05:12
    import cx_Oracle
    
    CONN_INFO = {
        'host': 'xxx.xx.xxx.x',
        'port': 12345,
        'user': 'user_name',
        'psw': 'your_password',
        'service': 'abc.xyz.com',
    }
    
    CONN_STR = '{user}/{psw}@{host}:{port}/{service}'.format(**CONN_INFO)
    
    connection = cx_Oracle.connect(CONN_STR)
    
    0 讨论(0)
  • 2020-11-28 05:14

    I like to do it this way:

    ip = '192.168.0.1'
    port = 1521
    SID = 'YOURSIDHERE'
    dsn_tns = cx_Oracle.makedsn(ip, port, SID)
    
    db = cx_Oracle.connect('username', 'password', dsn_tns)
    

    One of the main reasons I like this method is that I usually have a TNSNAMES.ORA file lying around someplace, and I can check that the dsn_tns object will do the right thing by doing:

    print dsn_tns
    

    and comparing the output to my TNSNAMES.ORA

    0 讨论(0)
  • 2020-11-28 05:27

    You can specify the server in the connection string, e.g.:

    import cx_Oracle
    connstr = 'scott/tiger@server:1521/orcl'
    conn = cx_Oracle.connect(connstr)
    
    • "server" is the server, or the IP address if you want.
    • "1521" is the port that the database is listening on.
    • "orcl" is the name of the instance (or database service).
    0 讨论(0)
  • 2020-11-28 05:28

    Instead of specifying the SID, you can create a dsn and connect via service_name like:

    import cx_Oracle
    ip = '192.168.0.1'
    port = 1521
    service_name = 'my_service'
    dsn = cx_Oracle.makedsn(ip, port, service_name=service_name)
    
    db = cx_Oracle.connect('user', 'password', dsn)
    

    The benefit of using the service name instead of the specific instance identifier (SID), is that it will work in a RAC environment as well (using a SID won't). This parameter is available as of cx_Oracle version 5.1.1 (Aug 28, 2011)

    0 讨论(0)
  • 2020-11-28 05:28
    import cx_Oracle
    dsn = cx_Oracle.makedsn(host='127.0.0.1', port=1521, sid='your_sid')
    conn = cx_Oracle.connect(user='your_username', password='your_password', dsn=dsn)
    conn.close()
    
    0 讨论(0)
  • 2020-11-28 05:35
    import cx_Oracle
    ip = '172.30.1.234'
    port = 1524
    SID = 'dev3'
    dsn_tns = cx_Oracle.makedsn(ip, port, SID)
    
    conn = cx_Oracle.connect('dbmylike', 'pass', dsn_tns)
    print conn.version
    conn.close()
    
    0 讨论(0)
提交回复
热议问题