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

Ubuntu下用ODBC Driver 17连接SQL Server 2008 R2遇SSL错误求助

解决ODBC Driver 17连接SQL Server 2008 R2的SSL错误

问题背景

尝试使用Microsoft ODBC Driver 17通过ODBC连接SQL Server 2008 R2数据库,执行Python代码时出现SSL内部错误,无法建立连接。

配置文件与代码

odbc.ini配置:

[ODBC Data Sources]
MyDB=ODBC Driver 17 for SQL Server

[MyDB]
Description=MyDB test database
Driver=ODBC Driver 17 for SQL Server
Server=***.***.***.***
Port=1433
Database=DbName
UID=********
PWD=********
Encrypt = no
TrustServerCertificate = no
Security = TLSv1

odbcinst.ini配置:

[Default]
Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.10.so.2.1

[ODBC Driver 18 for SQL Server]
Description=Microsoft ODBC Driver 18 for SQL Server
Driver=/opt/microsoft/msodbcsql18/lib64/libmsodbcsql-18.2.so.1.1
UsageCount=1

[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.10.so.2.1
UsageCount=1

Python连接代码:

import pyodbc
def connect():
    connStr = 'DSN=MyDB;'
    print(f'Connection: {connStr}')
    cnxn = pyodbc.connect(connStr)
    cur1 = cnxn.execute('SELECT top 2 * from MASTER')
    res1 = cur1.fetchall()
    print(res1)

if __name__ == "__main__":
     connect()

错误日志

Connection: DSN=MyDB
[ODBC][2133819][1678232108.869580][__handles.c][460]
        Exit:[SQL_SUCCESS]
            Environment = 0x557e51cc9e80
[ODBC][2133819][1678232108.869650][SQLSetEnvAttr.c][189]
        Entry:
            Environment = 0x557e51cc9e80
            Attribute = SQL_ATTR_ODBC_VERSION
            Value = 0x3
            StrLen = 4
[ODBC][2133819][1678232108.869720][SQLSetEnvAttr.c][381]
        Exit:[SQL_SUCCESS]
[ODBC][2133819][1678232108.869746][SQLAllocHandle.c][377]
        Entry:
            Handle Type = 2
            Input Handle = 0x557e51cc9e80
        UNICODE Using encoding ASCII 'UTF-8' and UNICODE 'UCS-2LE'

[ODBC][2133819][1678232108.869836][SQLAllocHandle.c][513]
        Exit:[SQL_SUCCESS]
            Output Handle = 0x557e51ce8a80
[ODBC][2133819][1678232108.870183][SQLDriverConnectW.c][290]
        Entry:
            Connection = 0x557e51ce8a80
            Window Hdl = (nil)
            Str In = [DSN=MyDB][length = 8 (SQL_NTS)]
            Str Out = (nil)
            Str Out Max = 0
            Str Out Ptr = (nil)
            Completion = 0
[ODBC][2133819][1678232108.879971][__handles.c][460]
        Exit:[SQL_SUCCESS]
            Environment = 0x557e51d30ce0
[ODBC][2133819][1678232108.880053][SQLGetEnvAttr.c][157]
        Entry:
            Environment = 0x557e51d30ce0
            Attribute = 65002
            Value = 0x7fffaef27420
            Buffer Len = 128
            StrLen = 0x7fffaef273bc
[ODBC][2133819][1678232108.880185][SQLGetEnvAttr.c][273]
        Exit:[SQL_SUCCESS]
[ODBC][2133819][1678232108.880229][SQLFreeHandle.c][220]
        Entry:
            Handle Type = 1
            Input Handle = 0x557e51d30ce0
[ODBC][2133819][1678232108.880360][SQLDriverConnectW.c][699]
        Exit:[SQL_ERROR]
[ODBC][2133819][1678232108.880402][SQLDriverConnect.c][748]
        Entry:
            Connection = 0x557e51ce8a80
            Window Hdl = (nil)
            Str In = [DSN=MyDB][length = 8 (SQL_NTS)]
            Str Out = 0x7fffaef2b190
            Str Out Max = 2048
            Str Out Ptr = (nil)
            Completion = 0
        DIAG [08001] [Microsoft][ODBC Driver 17 for SQL Server]SSL Provider: [error:0A0C0103:SSL routines::internal error]

        DIAG [08001] [Microsoft][ODBC Driver 17 for SQL Server]Client unable to establish connection

[ODBC][2133819][1678232108.884482][SQLDriverConnect.c][1637]
        Exit:[SQL_ERROR]
[ODBC][2133819][1678232108.884519][SQLGetDiagRecW.c][535]
        Entry:
            Connection = 0x557e51ce8a80
            Rec Number = 1
            SQLState = 0x7fffaef2c994
            Native = 0x7fffaef2c96c
            Message Text = 0x557e51d98250
            Buffer Length = 1023
            Text Len Ptr = 0x7fffaef2c96a
[ODBC][2133819][1678232108.884612][SQLGetDiagRecW.c][596]
        Exit:[SQL_SUCCESS]
            SQLState = [08001]
            Native = 0x7fffaef2c96c -> -1 (32 bits)
            Message Text = [[Microsoft][ODBC Driver 17 for SQL Server]SSL Provider: [error:0A0C0103:SSL routines::internal error]]
[ODBC][2133819][1678232108.884703][SQLFreeHandle.c][290]
        Entry:
            Handle Type = 2
            Input Handle = 0x557e51ce8a80
[ODBC][2133819][1678232108.884753][SQLFreeHandle.c][339]
        Exit:[SQL_SUCCESS]
Traceback (most recent call last):
  File "/home/data2.py", line 13, in <module>
    connect()
  File "/home/data2.py", line 7, in connect
    cnxn = pyodbc.connect(connStr)
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]SSL Provider: [error:0A0C0103:SSL routines::internal error] (-1) (SQLDriverConnect)')

解决方案

1. 调整ODBC连接参数

SQL Server 2008 R2仅支持TLS 1.0,ODBC Driver 17的默认配置与该版本存在兼容性问题,修改odbc.ini中的MyDB段:

[MyDB]
Description=MyDB test database
Driver=ODBC Driver 17 for SQL Server
Server=***.***.***.***,1433
Database=DbName
UID=********
PWD=********
Encrypt=yes
TrustServerCertificate=yes
  • 移除Security = TLSv1:该参数并非ODBC Driver 17的标准参数,驱动会自动适配支持的TLS版本
  • 设置Encrypt=yes:强制启用加密连接,匹配SQL Server的配置
  • 设置TrustServerCertificate=yes:跳过证书验证(适用于测试环境或自签名证书场景)

2. 启用系统层面的TLS 1.0支持

较新的Linux发行版(如Ubuntu 22.04+)默认禁用了TLS 1.0,需要修改OpenSSL配置以启用:

  1. 编辑/etc/ssl/openssl.cnf,添加或修改以下内容:
openssl_conf = default_conf

[default_conf]
ssl_conf = ssl_sect

[ssl_sect]
system_default = system_default_sect

[system_default_sect]
MinProtocol = TLSv1
CipherString = DEFAULT@SECLEVEL=1
  1. 保存配置后,重启应用或服务器使配置生效

3. 确认SQL Server 2008 R2的补丁状态

SQL Server 2008 R2需要安装**Service Pack 3 (SP3)**及后续的TLS 1.0支持补丁,否则无法正常建立加密连接。如果未安装补丁,即使客户端配置正确也会连接失败。

4. 验证ODBC配置正确性

执行以下命令确认ODBC配置文件路径和驱动状态:

odbcinst -j

确保输出中的ODBCINI和ODBCSYSINI指向你正在使用的配置文件目录,同时验证Driver 17的路径是否正确。

5. 直接使用连接字符串(可选)

如果DSN配置仍有问题,可以在Python代码中直接使用完整连接字符串,避免依赖DSN:

import pyodbc
def connect():
    connStr = (
        'DRIVER={ODBC Driver 17 for SQL Server};'
        'SERVER=***.***.***.***,1433;'
        'DATABASE=DbName;'
        'UID=********;'
        'PWD=********;'
        'Encrypt=yes;'
        'TrustServerCertificate=yes'
    )
    print(f'Connection: {connStr}')
    cnxn = pyodbc.connect(connStr)
    cur1 = cnxn.execute('SELECT top 2 * from MASTER')
    res1 = cur1.fetchall()
    print(res1)

if __name__ == "__main__":
     connect()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 10:17:00