You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用python-oracledb连接Oracle数据库遇Bad file descriptor错误求助

问题:Python oracledb连接远程Oracle数据库报错DPY-4011及OSError: Errno 9

我尝试使用Python的oracledb驱动连接远程Oracle数据库,编写了如下测试脚本:

import oracledb


username = 'placeholder'
password = 'placeholder'
hostname = 'placeholder'
port = 1521
service_name = 'placeholder'
dsn = oracledb.makedsn(hostname, port, service_name=service_name)

connection =  oracledb.connect(user=username, password=password, dsn=dsn)

print("Successfully connected to Oracle Database")
connection.close()

运行脚本时出现如下错误:

Traceback (most recent call last):
  File "src/oracledb/impl/thin/connection.pyx", line 328, in oracledb.thin_impl.ThinConnImpl.connect
  File "src/oracledb/impl/thin/connection.pyx", line 215, in oracledb.thin_impl.ThinConnImpl._connect_with_params
  File "src/oracledb/impl/thin/connection.pyx", line 186, in oracledb.thin_impl.ThinConnImpl._connect_with_description
  File "src/oracledb/impl/thin/connection.pyx", line 127, in oracledb.thin_impl.ThinConnImpl._connect_with_address
  File "src/oracledb/impl/thin/protocol.pyx", line 251, in oracledb.thin_impl.Protocol._connect_phase_two
  File "src/oracledb/impl/thin/protocol.pyx", line 381, in oracledb.thin_impl.Protocol._process_message
  File "src/oracledb/impl/thin/protocol.pyx", line 359, in oracledb.thin_impl.Protocol._process_message
  File "src/oracledb/impl/thin/protocol.pyx", line 430, in oracledb.thin_impl.Protocol._receive_packet
  File "src/oracledb/impl/thin/packet.pyx", line 561, in oracledb.thin_impl.ReadBuffer.receive_packet
  File "src/oracledb/impl/thin/packet.pyx", line 360, in oracledb.thin_impl.ReadBuffer._receive_packet_helper
  File "src/oracledb/impl/thin/packet.pyx", line 206, in oracledb.thin_impl.ReadBuffer._get_data_from_socket
  File "/opt/homebrew/lib/python3.11/site-packages/oracledb/errors.py", line 127, in _raise_err
    raise exc_type(_Error(message)) from cause
oracledb.exceptions.DatabaseError: DPY-4011: the database or network closed the connection

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/mitchellgrant/jist-data-pipeline/jist_dbt/test.py", line 11, in <module>
    connection =  oracledb.connect(user=username, password=password, dsn=dsn)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/oracledb/connection.py", line 1020, in connect
    return conn_class(dsn=dsn, pool=pool, params=params, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/oracledb/connection.py", line 130, in __init__
    impl.connect(params_impl)
  File "src/oracledb/impl/thin/connection.pyx", line 337, in oracledb.thin_impl.ThinConnImpl.connect
  File "src/oracledb/impl/thin/connection.pyx", line 235, in oracledb.thin_impl.ThinConnImpl._force_close
  File "src/oracledb/impl/thin/protocol.pyx", line 353, in oracledb.thin_impl.Protocol._force_close
OSError: [Errno 9] Bad file descriptor

我已尝试几种解决方法但均无效,请问有什么解决建议吗?


解决建议

  • 检查网络连通性:执行telnet hostname 1521或nc -zv hostname 1521测试目标主机端口是否可达,排查防火墙、安全组或网络路由拦截问题
  • 验证连接参数准确性:确认username、password、hostname、service_name完全正确,注意service_name与SID的区别,可通过数据库端执行select value from v$parameter where name='service_names';获取正确的service_name
  • 切换连接模式:尝试从默认的thin模式切换到thick模式,需先安装Oracle Instant Client,代码中添加初始化语句:
    oracledb.init_oracle_client(lib_dir="/path/to/instant/client")
    
  • 确认数据库状态:检查远程Oracle数据库服务是否正常运行,监听程序是否启动,可在数据库端执行lsnrctl status查看监听状态
  • 排查SSL配置:若远程数据库要求SSL连接,需在connect方法中添加SSL相关参数,例如ssl_server_dn_match=True、ssl_cert_reqs=CERT_REQUIRED等
  • 升级驱动版本:执行pip install --upgrade oracledb将oracledb更新至最新版本,修复已知的连接类bug

内容的提问来源于stack exchange,提问作者Mitchell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 20:35:23