【发布时间】:2023-04-07 04:26:01
【问题描述】:
在我重新启动笔记本电脑之前一切正常。
我有这个设置:
- python 3.8.9
- django 3.1.1
- pyodbc 4.0.30
-
pyodbc.drivers()显示:['ODBC Driver 17 for SQL Server'] - SQLServer
-
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.ini和cat /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
-
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
- 在 django 设置中:
DATABASES = {
'default': {
'ENGINE': 'mssql',
'NAME': os.environ.get('DB_NAME'),
'USER': os.environ.get('DB_USER'),
'PASSWORD': os.environ.get('DB_PASSWORD'),
'HOST': os.environ.get('DB_HOST'),
'PORT': os.environ.get('DB_PORT'),
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
},
},
}
-
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"
我用这个official Microsoft instruction 重新安装了 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)')
- 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]
我设法找到的所有答案都没有帮助,包括这个答案:ODBC can't find correct OpenSSL version after upgrading OpenSSL
这很奇怪,直到我重新启动笔记本电脑(没有操作系统更新,只是重新启动)之前,一切都正常工作了很长时间。这个问题可能与使用路径导出一些变量有关吗?请帮我解决这个问题。
【问题讨论】:
-
我也遇到了同样的错误,你找到解决办法了吗?谢谢!
-
@DPs in
/usr/local/bin我将文件夹openssl@1.1中的所有文件复制到openssl文件夹中,它开始工作。不确定这是否是一个可接受的解决方案。 -
感谢您的评论,我试过了,但我的 /usr/local/bin 文件夹中没有 openssl@1.1 文件夹,Rajat 的解决方案对我有用。谢谢
标签: python django path openssl odbc