V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
LeeReamond
V2EX  ›  Oracle

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

  •  
  •   LeeReamond · 2022-05-23 21:27:46 +08:00 · 1304 次点击
    这是一个创建于 696 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    目前做了的工作:

    • 使用 oralce linux 8 系统
    • 安装 oracle 的版本是 19c
    • 基本上按照谷歌搜索排名靠前的这个链接的说明安装
    • 安装过程简单来说是 dnf install oracle-database-preinstall-19c ,然后在图形界面中执行安装程序,安装过程中选择单实例-服务器模式

    疑问:

    尝试使用 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
    
    4 条回复    2022-05-24 11:12:35 +08:00
    gefranks
        1
    gefranks  
       2022-05-24 07:15:14 +08:00 via iPhone
    先设个 listener 然后把服务都起起来 sqlplus 先连下看看
    LeeReamond
        2
    LeeReamond  
    OP
       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
        3
    LeeReamond  
    OP
       2022-05-24 09:51:44 +08:00
    @LeeReamond 提示找不到 oraclehome ,但是程序都装上了,oraclehome 肯定是有的,就很怪
    gefranks
        4
    gefranks  
       2022-05-24 11:12:35 +08:00   ❤️ 1
    export ORACLE_HOME=/oracledb/oracle/19.0.1/db_home1 这样的,要设环境变量的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4386 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 04:05 · PVG 12:05 · LAX 21:05 · JFK 00:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.