cx_Oracle & 远程连接到 Oracle 数据库

发布于 2024-07-07 07:21:55 字数 469 浏览 5 评论 0原文

如何通过 IP 地址连接到远程服务器,就像 TOAD、SqlDeveloper 能够仅使用 IP 地址、用户名、SID 和密码连接到数据库一样?

每当我尝试指定 IP 地址时,它似乎都是在本地获取的。

换句话说,cx_Oracle.connect() 的字符串应该如何格式化为非本地数据库?

之前有一篇文章列出了通过 cx_Oracle 模块连接到 Oracle 的答案,代码如下:

#!/usr/bin/python

import cx_Oracle
connstr='scott/tiger'
conn = cx_Oracle.connect(connstr)
curs = conn.cursor()

curs.execute('select * from emp')
print curs.description
for row in curs:
    print row
conn.close()

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 password?

Whenever I try to specify and IP address, it seems to be taking it locally.

In other words, how should the string for cx_Oracle.connect() be formatted to a non local database?

There was a previous post which listed as an answer connecting to Oracle via cx_Oracle module with the following code:

#!/usr/bin/python

import cx_Oracle
connstr='scott/tiger'
conn = cx_Oracle.connect(connstr)
curs = conn.cursor()

curs.execute('select * from emp')
print curs.description
for row in curs:
    print row
conn.close()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

野生奥特曼 2024-07-14 07:21:55

我喜欢这样做:

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)

我喜欢这种方法的主要原因之一是我通常在某个地方有一个 TNSNAMES.ORA 文件,我可以检查 dsn_tns 对象是否会正确执行通过这样做:

print dsn_tns

并将输出与我的 TNSNAMES.ORA 进行比较

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

Smile简单爱 2024-07-14 07:21:55

您可以在连接字符串中指定服务器,例如:

import cx_Oracle
connstr = 'scott/tiger@server:1521/orcl'
conn = cx_Oracle.connect(connstr)
  • “server”是服务器,如果需要,也可以是 IP 地址。
  • “1521”是数据库正在侦听的端口。
  • “orcl”是实例(或数据库服务)的名称。

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).
避讳 2024-07-14 07:21:55
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)
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)
任谁 2024-07-14 07:21:55

您可以创建一个 dsn 并通过 service_name 连接,而不是指定 SID 就像:

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)

使用服务名称而不是特定实例标识符 (SID) 的好处是它也可以在 RAC 环境中工作(使用 SID 则不行)。 此参数自 cx_Oracle 版本 5.1.1(2011 年 8 月 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)

还给你自由 2024-07-14 07:21:55
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()
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()
隔岸观火 2024-07-14 07:21:55
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()
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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文