正确连接 Oracle 的姿势是什么?

2022-05-23 21:27:46 +08:00
 LeeReamond

需求:编程化连接 Oracle 数据库并进行 helloworld 级别的创建表、增删改查。

目前做了的工作:

疑问:

尝试使用 python-cx_Oracle 库连接数据库,文档给出范例代码

import cx_Oracle

userpwd = ". . ." # Obtain password string from a user prompt or environment variable

connection = cx_Oracle.connect(user="hr", password=userpwd,
                               dsn="dbhost.example.com/orclpdb1",
                               encoding="UTF-8")

疑问在于,安装过程中没有印象配置了监听 IP/监听端口的选项,并且数据库名的表达和 mysql 系列似乎不太相同,且印象中创建了三个管理员用户,让我比较混乱,上述代码中到底应该使用哪个用户,什么 IP 和端口,如何指定要连接的数据库,这个 dsn 的构造规则也不太清楚。另外依稀印象里多年前使用 oracle 的时候,似乎 oracle 安装完成后是需要再单独设置监听服务的种种细节的,这次安装没有进行类似操作。

尝试使用下述代码连接

import cx_Oracle

dsn = cx_Oracle.makedsn(
    host = 'localhost',
    port = '1521',
    sid = 'xe'
)
connection = cx_Oracle.connect(
    user="root",
    password="123456",
    dsn=dsn
)

得到错误提示

Traceback (most recent call last):
  File "/home/tmp/test.py", line 8, in <module>
    connection = cx_Oracle.connect(
cx_Oracle.DatabaseError: ORA-12505: TNS:listener does not currently know of SID given in connect descriptor
1313 次点击
所在节点    Oracle
4 条回复
gefranks
2022-05-24 07:15:14 +08:00
先设个 listener 然后把服务都起起来 sqlplus 先连下看看
LeeReamond
2022-05-24 09:51:06 +08:00
@gefranks 具体怎么操作呢,网上 oracle 资料不多。sqlplus 客户端也开不起来,运行命令提示
[oracle@localhost ~]$ sqlplus
Error 6 initializing SQL*Plus
SP2-0667: Message file sp1<lang>.msb not found
SP2-0750: You may need to set ORACLE_HOME to your Oracle software directory
LeeReamond
2022-05-24 09:51:44 +08:00
@LeeReamond 提示找不到 oraclehome ,但是程序都装上了,oraclehome 肯定是有的,就很怪
gefranks
2022-05-24 11:12:35 +08:00
export ORACLE_HOME=/oracledb/oracle/19.0.1/db_home1 这样的,要设环境变量的

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/854807

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX