客户端无法建立连接,macOS,django,odbc 17,pyodbc

sga*_*ich 5 python django odbc openssl path

一切正常,直到我重新启动笔记本电脑。

我有这个设置:

  • 蟒蛇3.8.9
  • Django 3.1.1
  • pyodbc 4.0.30
  • pyodbc.drivers()显示这个:['ODBC Driver 17 for SQL Server']
  • SQL服务器
  • openssl version显示这个:OpenSSL 1.1.1l 24 Aug 2021
  • isql -v -k "DRIVER=ODBC Driver 17 for SQL Server;SERVER=<my server>,<my port>;UID=<my username>;PWD=<my password>"连接到数据库没有问题
  • 服务器名、端口、用户和密码似乎也是正确的,因为 jdbc 驱动程序可以正常工作,没有任何问题。
  • cat /etc/odbcinst.inicat /etc/odbc.ini- 都返回这个:
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/usr/local/lib/libmsodbcsql.17.dylib
UsageCount=10
Run Code Online (Sandbox Code Playgroud)
  • odbcinst -j返回这个:
unixODBC 2.3.9
DRIVERS............: /usr/local/etc/odbcinst.ini
SYSTEM DATA SOURCES: /usr/local/etc/odbc.ini
FILE DATA SOURCES..: /usr/local/etc/ODBCDataSources
USER DATA SOURCES..: /Users/sgalich/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
Run Code Online (Sandbox Code Playgroud)
  • 在 Django 设置中:
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/usr/local/lib/libmsodbcsql.17.dylib
UsageCount=10
Run Code Online (Sandbox Code Playgroud)
  • cat ~/.bash_profile显示这个:
export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"
export CPATH="/usr/local/opt/openssl@1.1/include"
export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"
export LIBRARY_PATH="/usr/local/opt/openssl@1.1/lib"
export DYLD_LIBRARY_PATH="/usr/local/opt/openssl@1.1/lib"
export DYLD_FALLBACK_LIBRARY_PATH="/usr/local/opt/openssl@1.1/lib"
Run Code Online (Sandbox Code Playgroud)

我按照Microsoft 官方说明重新安装了 ODBC 驱动程序。但这没有帮助。我仍然无法启动 django 服务器并面临以下错误:

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/Users/sgalich/Library/Python/3.8/lib/python/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/Users/sgalich/Library/Python/3.8/lib/python/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/Users/sgalich/Library/Python/3.8/lib/python/site-packages/django/db/backends/base/base.py", line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/Users/sgalich/Library/Python/3.8/lib/python/site-packages/mssql/base.py", line 326, in get_new_connection
    conn = Database.connect(connstr,
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Client unable to establish connection (0) (SQLDriverConnect)')

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

Traceback (most recent call last):
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/sgalich/Library/Python/3.8/lib/python/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/Users/sgalich/Library/Python/3.8/lib/python/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
    self.check_migrations()
  File "/Users/sgalich/Library/Python/3.8/lib/python/site-packages/django/core/management/base.py", line 486, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/Users/sgalich/Library/Python/3.8/lib/python/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/Users/sgalich/Library/Python/3.8/lib/python/site-packages/django/db/migrations/loader.py", line 53, in __init__
    self.build_graph()
  File "/Users/sgalich/Library/Python/3.8/lib/python/site-packages/django/db/migrations/loader.py", line 220, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/Users/sgalich/Library/Python/3.8/lib/python/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
    if self.has_table():
  File "/Users/sgalich/Library/Python/3.8/lib/python/site-packages/django/db/migrations/recorder.py", line 55, in has_table
    with self.connection.cursor() as cursor:
  File "/Users/sgalich/Library/Python/3.8/lib/python/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/Users/sgalich/Library/Python/3.8/lib/python/site-packages/django/db/backends/base/base.py", line 259, in cursor
    return self._cursor()
  File "/Users/sgalich/Library/Python/3.8/lib/python/site-packages/mssql/base.py", line 230, in _cursor
    conn = super()._cursor()
  File "/Users/sgalich/Library/Python/3.8/lib/python/site-packages/django/db/backends/base/base.py", line 235, in _cursor
    self.ensure_connection()
  File "/Users/sgalich/Library/Python/3.8/lib/python/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/Users/sgalich/Library/Python/3.8/lib/python/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/Users/sgalich/Library/Python/3.8/lib/python/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/Users/sgalich/Library/Python/3.8/lib/python/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/Users/sgalich/Library/Python/3.8/lib/python/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/Users/sgalich/Library/Python/3.8/lib/python/site-packages/django/db/backends/base/base.py", line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/Users/sgalich/Library/Python/3.8/lib/python/site-packages/mssql/base.py", line 326, in get_new_connection
    conn = Database.connect(connstr,
django.db.utils.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]Client unable to establish connection (0) (SQLDriverConnect)')
Run Code Online (Sandbox Code Playgroud)
  • odbc 日志显示:
[ODBC][33439][1634323619.168260][__handles.c][460]
        Exit:[SQL_SUCCESS]
            Environment = 0x7fb18c951200
[ODBC][33439][1634323619.168500][SQLSetEnvAttr.c][189]
        Entry:
            Environment = 0x7fb18c951200
            Attribute = SQL_ATTR_ODBC_VERSION
            Value = 0x3
            StrLen = 4
[ODBC][33439][1634323619.168927][SQLSetEnvAttr.c][381]
        Exit:[SQL_SUCCESS]
[ODBC][33439][1634323619.169074][SQLAllocHandle.c][377]
        Entry:
            Handle Type = 2
            Input Handle = 0x7fb18c951200
        UNICODE Using encoding ASCII 'UTF-8' and UNICODE 'UCS-2-INTERNAL'

[ODBC][33439][1634323619.169343][SQLAllocHandle.c][513]
        Exit:[SQL_SUCCESS]
            Output Handle = 0x7fb19d4b2000
[ODBC][33439][1634323619.170140][SQLDriverConnectW.c][290]
        Entry:
            Connection = 0x7fb19d4b2000
            Window Hdl = 0x0
            Str In = [DRIVER=ODBC Driver 17 for SQL Server;SERVER=<my server>,<my password>;UID=<my username>;PWD=********************;DATABASE=growth;unico...][length = 144 (SQL_NTS)]
            Str Out = 0x0
            Str Out Max = 0
            Str Out Ptr = 0x0
            Completion = 0
[ODBC][33439][1634323619.291639][__handles.c][460]
        Exit:[SQL_SUCCESS]
            Environment = 0x7fb19d4c0a00
[ODBC][33439][1634323619.291819][SQLGetEnvAttr.c][157]
        Entry:
            Environment = 0x7fb19d4c0a00
            Attribute = 65002
            Value = 0x70000ee057a0
            Buffer Len = 128
            StrLen = 0x70000ee05784
[ODBC][33439][1634323619.292027][SQLGetEnvAttr.c][273]
        Exit:[SQL_SUCCESS]
[ODBC][33439][1634323619.292192][SQLFreeHandle.c][220]
        Entry:
            Handle Type = 1
            Input Handle = 0x7fb19d4c0a00
[ODBC][33439][1634323619.292399][SQLDriverConnectW.c][699]
        Exit:[SQL_ERROR]
[ODBC][33439][1634323619.292558][SQLDriverConnect.c][748]
        Entry:
            Connection = 0x7fb19d4b2000
            Window Hdl = 0x0
            Str In = [DRIVER=ODBC Driver 17 for SQL Server;SERVER=<my server>,<my password>;UID=<my username>;PWD=********************;DATABASE=growth;unico...][length = 144 (SQL_NTS)]
            Str Out = 0x70000ee06160
            Str Out Max = 2048
            Str Out Ptr = 0x0
            Completion = 0
        DIAG [08001] [Microsoft][ODBC Driver 17 for SQL Server]Client unable to establish connection

        DIAG [01S00] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute

[ODBC][33439][1634323619.346187][SQLDriverConnect.c][1637]
        Exit:[SQL_ERROR]
[ODBC][33439][1634323619.346372][SQLGetDiagRecW.c][535]
        Entry:
            Connection = 0x7fb19d4b2000
            Rec Number = 1
            SQLState = 0x70000ee08504
            Native = 0x70000ee084ec
            Message Text = 0x7fb19dad5e00
            Buffer Length = 1023
            Text Len Ptr = 0x70000ee08502
[ODBC][33439][1634323619.346517][SQLGetDiagRecW.c][596]
        Exit:[SQL_SUCCESS]
            SQLState = [08001]
            Native = 0x70000ee084ec -> 0 (32 bits)
            Message Text = [[Microsoft][ODBC Driver 17 for SQL Server]Client unable to establish connection]
[ODBC][33439][1634323619.346654][SQLFreeHandle.c][290]
        Entry:
            Handle Type = 2
            Input Handle = 0x7fb19d4b2000
[ODBC][33439][1634323619.346751][SQLFreeHandle.c][339]
        Exit:[SQL_SUCCESS]

Run Code Online (Sandbox Code Playgroud)

我设法找到的所有答案都没有帮助,包括这个答案:升级 OpenSSL 后 ODBC 无法找到正确的 OpenSSL 版本

这很奇怪,一切都工作了很长一段时间,直到我重新启动我的笔记本电脑(没有操作系统更新,只是重新启动)。这个问题可能与导出一些带路径的变量有关吗?请帮我解决这个问题。

小智 10

我不确定你是否能找到这个。但以下解决方案对我有用。我的 odbc 无法找到 ssl 版本 1。我这样做了:在我的 /usr/local/opt/ 中

rm openssl
Run Code Online (Sandbox Code Playgroud)

或者

rm openssl@3
Run Code Online (Sandbox Code Playgroud)

这个想法是删除它指向的任何 openssl 的链接。您也可以与您的发现者核实。

然后新建一个指向openssl1.1的链接

ln -s /usr/local/Cellar/openssl@1.1/1.1.1l_1 /usr/local/opt/openssl
Run Code Online (Sandbox Code Playgroud)

  • 拉贾特谢谢你的回答。在我在这里发布这个问题之前我已经这样做了,但这对我没有帮助。但对我有帮助的是:我将文件夹“openssl@1.1”内的所有文件复制到“openssl”文件夹中。 (2认同)