我正在尝试使用 SQLAlchemy 和 OracleDB 在 Python 中连接到 Oracle 数据库。但我总是收到这个错误:
sqlalchemy.exc.OperationalError: (oracledb.exceptions.OperationalError) DPY-6005: cannot connect to database (CONNECTION_ID=1f27PvoJ7yfyC1G5UcfdNQ==).
DPY-3008: unsupported in-band notification with error number 12572
我可以使用与 DBeaver 和 Pentaho 相同的身份验证连接到数据库,所以我不认为这里的问题是防火墙或类似的东西。
代码很简单:
import pandas as pd
import oracledb
from sqlalchemy import create_engine
from os.path import realpath, dirname
# Script path
path = realpath(dirname(__file__))
oracle_user = 'secret'
oracle_password = 'secret'
oracle_host = 'secret'
oracle_port = 'secret'
oracle_service_name = 'secret'
dsn_oracle = '''(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=SECRET)(PORT=1521))
(CONNECT_DATA=(SERVER=dedicated)(SERVICE_NAME=SECRET)))'''
oracle_engine = create_engine(f'oracle+oracledb://{oracle_user}:{oracle_password}@{oracle_host}:{oracle_port}/?service_name={oracle_service_name}')
df = pd.read_sql('SELECT * FROM SILOMS.T_TIPO_DISPONIBILIDADE', oracle_engine)
我尝试了以下更改:
oracle_engine = create_engine(f'oracle+oracledb://{oracle_user}:{oracle_password}@{oracle_host}:{oracle_port}/{oracle_service_name}')
上面的代码出现了不同的错误:
DPY-6005: cannot connect to database (CONNECTION_ID=Ahugep/3dI6/h82DqaM/BA==).
DPY-6003: SID "SECRET_SERVICE_NAME" is not registered with the listener at host "SECRET_HOST_NAME" port 1521. (Similar to ORA-12505)
oracle_engine = create_engine(f'oracle+oracledb://{oracle_user}:{oracle_password}@{dsn_oracle}')
上面的代码从标题中得到了相同的 12572 错误(DPY-3008)
此问题已在昨天发布(2024年8月22日)的2.4.1版本中修复。