【问题标题】:pyodbc error: using django in docker containerpyodbc 错误:在 docker 容器中使用 django
【发布时间】:2020-04-09 00:31:03
【问题描述】:

我正在使用一个运行 django 应用程序的 docker 容器。

每当我尝试运行在 2 个独立数据库上操作数据的函数时,都会出现以下错误:

错误:[Microsoft][ODBC Driver 17 for SQL Server]SSL 提供程序:[错误:1425F102:SSL 例程:ssl_choose_client_version:unsupported protocol] (-1) (SQLDriverConnect)')

这是我运行的函数

def preStepBtn3(request):
    sourcePE = request.GET.get('sourcePE')
    targetPE = request.GET.get('targetPE')
    inputFTID = request.session['username']
    datetime_object = datetime.datetime.now()
    step_name = "preStepBtn3"
    table_name = "null"
    Action_taken = "change router status to MIG"
    MassivePortalSessionID = request.session['MassivePortalSessionID']

    try:
        with connections['DataAdmin'].cursor() as cursor:
            sql = """DECLARE @out nvarchar(max); exec DTA.mig_sop_ce_status2mig_django 0, %s, @param_out = @out OUTPUT; SELECT @out AS the_output; """
            params = [sourcePE]
            cursor.execute(sql, params)            
            rows = cursor.fetchall()
            result = []
            result.append(rows)
            logDB_sql(MassivePortalSessionID, inputFTID, sourcePE, targetPE,
                      table_name, step_name, datetime_object, Action_taken)
            print("data inserted in log DB")
            while rows:
                print(rows)
                if cursor.nextset():
                    result.append(cursor.fetchall())
                else:
                    print(result)
                    return JsonResponse(result,  safe=False)
    except Exception as ex:
        error = ex
        print(error)
        context = {'text': error}
        logDB_sql(MassivePortalSessionID, inputFTID, sourcePE, targetPE,
                  table_name, step_name, datetime_object, Action_taken)
        print("data inserted in log DB during exception")
        return render(request, 'posts/textArea.html', context)

每当我删除 logDB_sql 时,它都能完美运行。 这是 logDB_sql 的代码

def logDB_sql(MassivePortalSessionID, inputFTID, sourcePE, targetPE, table_name, step_name, datetime_object, Action_taken):
    params = [MassivePortalSessionID, inputFTID, sourcePE, targetPE,
              table_name, step_name, datetime_object, Action_taken]
    print(targetPE)
    print(sourcePE)
    print(MassivePortalSessionID)
    print(inputFTID)
    if sourcePE != None and MassivePortalSessionID != None and targetPE != None and inputFTID != None:
        sql = " insert into MASSIVE_MIGRATION_PORTAL_LOGS values (%s,%s,%s,%s,%s,%s,%s,%s )"
        print(sql)
        print(params)
        with connections['logDB'].cursor() as cursor:
            cursor.execute(sql, params)
            cursor.close()
            print("data inserted")

这是 settings.py 中的连接信息。请注意,它们是不同的主机

    'logDB': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'log_db',
        'HOST': 'xx.xx.xx.xx',
        'USER': 'user1',
        'PASSWORD': 'password1',

        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
            'timeout': 1000,
        }

    },

    'DataAdmin': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'another_db',
        'HOST': 'xx.xx.xx.xx',
        'USER': 'user1,
        'PASSWORD': 'password1',

        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
            'timeout': 1000,
        }
    },

}

这是错误信息

 File "/app/posts/views.py", line 387, in preStepBtn2
    table_name, step_name, datetime_object, Action_taken)
  File "/app/posts/views.py", line 90, in logDB_sql
    with connections['logDB'].cursor() as cursor:
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 255, in cursor
    return self._cursor()
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 232, in _cursor
    self.ensure_connection()
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.6/site-packages/sql_server/pyodbc/base.py", line 307, in get_new_connection
    timeout=timeout)
django.db.utils.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]SSL Provider: [error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol] (-1) (SQLDriverConnect)')

我已经在我的 Windows 机器上本地尝试过它,它没有任何问题,但不确定为什么当我将它移动到 docker 容器时它会失败。

有什么想法吗?

【问题讨论】:

  • 您的 SQL Server 版本是多少?旧版本的 SQL Server(例如:2008 R2)不支持现代客户端驱动程序所需的 TLS 1.2 协议,尤其是在它们使用 OpenSSL 时。您可以通过openssl s_client --host <YourServerIP> --port 1433 使用-tls1-tls1_1-tls1_2-tls1_3 开关手动确认服务器支持。
  • @AlwaysLearning 我已经尝试并得到:CONNECTED(00000003) 写入:errno=0 没有可用的对等证书 没有发送客户端证书 CA 名称 SSL 握手已读取 0 字节并写入 188 字节 验证:OK 新, (NONE), Cipher is (NONE) Secure Renegotiation is not supported 压缩:NONE 扩展:NONE 无 ALPN 协商 SSL-Session:协议:TLSv1.2 Cipher:0000 Session-ID:Session-ID-ctx:Master-Key:PSK身份:无 PSK 身份提示:无 SRP 用户名:无 开始时间:1586453378 超时:7200(秒)验证返回码:0(确定)扩展主密码:无

标签: sql-server django python-3.x docker pyodbc


【解决方案1】:

我已通过更新/etc/ssl/ 中的openssl.cnf 文件解决了这个问题

MinProtocol = TLSv1.2 更改为MinProtocol = TLSv1.0CipherString = DEFAULT@SECLEVEL=2 更改为CipherString = DEFAULT@SECLEVEL=1

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2022-06-13
    • 2022-01-20
    • 1970-01-01
    • 2015-10-22
    • 1970-01-01
    • 2021-02-18
    • 2019-11-19
    • 2021-11-12
    • 1970-01-01
    相关资源
    最近更新 更多