oracledb.exceptions.DatabaseError: DPY-4011: 数据库或网络关闭了连接 - python oracledb库

0 投票
1 回答
189 浏览
提问于 2025-04-14 17:34

我正在尝试用 Python 连接 Oracle 11g 数据库

import oracledb
import os

user = 'system'
password = 'admin123'
port = 1521
service_name = 'xe'
oracle_server_addr = 'localhost'

conn_string = "{oracle_server_addr}:{port}/{service_name}".format(oracle_server_addr=oracle_server_addr, port=port, service_name=service_name)

print(conn_string)

with oracledb.connect(user=user, password=password, dsn=conn_string) as conn:
    with conn.cursor() as cursor:
        sql = """select sysdate from dual"""
        for r in cursor.execute(sql):
            print(r)

但是我遇到了这个错误:

conn_string: localhost:1521/xe

Traceback (most recent call last):
  File "src/oracledb/impl/thin/connection.pyx", line 353, in oracledb.thin_impl.ThinConnImpl._connect_with_address
  File "src/oracledb/impl/thin/protocol.pyx", line 207, in oracledb.thin_impl.Protocol._connect_phase_one
  File "src/oracledb/impl/thin/protocol.pyx", line 386, in oracledb.thin_impl.Protocol._process_message
  File "src/oracledb/impl/thin/protocol.pyx", line 365, in oracledb.thin_impl.Protocol._process_message
  File "src/oracledb/impl/thin/messages.pyx", line 1835, in oracledb.thin_impl.ConnectMessage.process
  File "src/oracledb/impl/thin/buffer.pyx", line 845, in oracledb.thin_impl.Buffer.read_uint32
  File "src/oracledb/impl/thin/packet.pyx", line 235, in oracledb.thin_impl.ReadBuffer._get_raw
  File "src/oracledb/impl/thin/packet.pyx", line 588, in oracledb.thin_impl.ReadBuffer.wait_for_packets_sync
  File "src/oracledb/impl/thin/transport.pyx", line 306, in oracledb.thin_impl.Transport.read_packet
  File "/home/acme/.local/lib/python3.10/site-packages/oracledb/errors.py", line 162, in _raise_err
    raise exc_type(_Error(message)) from cause
oracledb.exceptions.DatabaseError: DPY-4011: the database or network closed the connection
Help: https://python-oracledb.readthedocs.io/en/latest/user_guide/troubleshooting.html#dpy-4011

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/tmp/workspace/sql_ping/sql_ping.py", line 16, in <module>
    with oracledb.connect(user=user, password=password, dsn=conn_string) as conn:
  File "/home/acme/.local/lib/python3.10/site-packages/oracledb/connection.py", line 1134, in connect
    return conn_class(dsn=dsn, pool=pool, params=params, **kwargs)
  File "/home/acme/.local/lib/python3.10/site-packages/oracledb/connection.py", line 523, in __init__
    impl.connect(params_impl)
  File "src/oracledb/impl/thin/connection.pyx", line 449, in oracledb.thin_impl.ThinConnImpl.connect
  File "src/oracledb/impl/thin/connection.pyx", line 445, in oracledb.thin_impl.ThinConnImpl.connect
  File "src/oracledb/impl/thin/connection.pyx", line 411, in oracledb.thin_impl.ThinConnImpl._connect_with_params
  File "src/oracledb/impl/thin/connection.pyx", line 392, in oracledb.thin_impl.ThinConnImpl._connect_with_description
  File "src/oracledb/impl/thin/connection.pyx", line 358, in oracledb.thin_impl.ThinConnImpl._connect_with_address
  File "/home/acme/.local/lib/python3.10/site-packages/oracledb/errors.py", line 162, in _raise_err
    raise exc_type(_Error(message)) from cause
oracledb.exceptions.OperationalError: DPY-6005: cannot connect to database (CONNECTION_ID=emkjRug2341/ymv7uTC0Bg==).
DPY-4011: the database or network closed the connection
Help: https://python-oracledb.readthedocs.io/en/latest/user_guide/troubleshooting.html#dpy-4011

环境背景

数据库和客户端应用程序都在同一台主机上

  • 数据库

    • Oracle 数据库 11g Express Edition 版本 11.2.0.2.0 - 64位 (localhost)
    • 操作系统:ubuntu
    • 用户名 = 'system'
    • 密码 = 'admin123'
    • 端口 = 1521
    • 服务名称 = 'xe'
    • 主机 = 'localhost'(我也试过用本地 IP 和 127.0.0.1)
  • 应用程序

    • 操作系统:ubuntu
    • Python 版本 3.10
    • oracledb 版本 2.0.1 和 2.0.0
    • 使用 thin 模式
  • 没有防火墙、代理、杀毒软件等,所有都在我的 ubuntu 本地主机上

尝试过的解决办法

# Java 可以正常连接

在同一台主机上,使用相同的数据库和 thin 模式,Java 可以毫无问题地连接。代码可以在 这里找到

# 使用 dbeaver 数据库 IDE 也能连接

这里是图片描述

# 降级 oracledb pip 库

我尝试了从 1.3.1 开始的所有版本,错误几乎都是一样的

https://pypi.org/project/oracledb/#history

1.2.1, 1.3.2, 1.4.2

oracledb.exceptions.NotSupportedError: DPY-3010: connections to this database server version are not supported by python-oracledb in thin mode

# disable_oob= True

我尝试了 这个 方法,但错误依然存在

# 类似的未解决问题

1 个回答

3

你不能使用瘦模式,因为Oracle数据库不支持Oracle 11。

来自Python OracleDB PyPi页面的信息:

依赖关系和兼容性

  • 支持Python版本3.7到3.12。

    这些Python版本在Windows、macOS和Linux上都有现成的安装包。

    源代码也可以获取。

  • Oracle客户端库是可选的。

    瘦模式:默认情况下,python-oracledb以“瘦模式”运行,这种模式直接连接到Oracle数据库。

    厚模式:一些高级的Oracle数据库功能目前只有在加载了可选的Oracle客户端库时才能使用。这些库可以在免费的Oracle即时客户端包中找到。python-oracledb可以使用11.2到21c版本的Oracle客户端库。

  • Oracle数据库

    瘦模式:需要Oracle数据库12.1(或更高版本)。

    厚模式:根据Oracle客户端库的版本,要求Oracle数据库11.2(或更高版本)。Oracle数据库的标准客户端-服务器版本兼容性允许连接到旧版和新版数据库。例如,当python-oracledb使用Oracle客户端19c库时,它可以连接到Oracle数据库11.2或更高版本。

要么使用厚模式,要么将你的数据库升级到至少Oracle 12.1。

撰写回答