尝试使用 django-pyodbc-azure 连接到 MSSQL 导致找不到文件错误
Attempting to connect to MSSQL with django-pyodbc-azure results in file not found error
我认为我的问题或多或少是 Trying to query SQL Server from django running on Linux - Can't open lib '/path/to/libtdsodbc.so' 的重复,但那里的答案完全没有用。
我正在使用以下东西:
- Ubuntu15.04
Python3
Django 1.9
(通过 pip3 安装)
freetds-dev 0.91-6build1
通过 apt-get 安装
django-pyodbc-azure
/django-pyodbc
通过 pip3 安装
- MSSQL 2012
尝试连接时,执行 python3 manage.py inspectdb
我得到以下堆栈跟踪:
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
File "/usr/local/lib/python3.4/dist-packages/sql_server/pyodbc/base.py", line 302, in get_new_connection
timeout=timeout)
pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'FreeTDS' : file not found (0) (SQLDriverConnect)")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 342, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/commands/inspectdb.py", line 25, in handle
for line in self.handle_inspection(options):
File "/usr/local/lib/python3.4/dist-packages/django/core/management/commands/inspectdb.py", line 38, in handle_inspection
with connection.cursor() as cursor:
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 231, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 204, in _cursor
self.ensure_connection()
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python3.4/dist-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
File "/usr/local/lib/python3.4/dist-packages/sql_server/pyodbc/base.py", line 302, in get_new_connection
timeout=timeout)
django.db.utils.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'FreeTDS' : file not found (0) (SQLDriverConnect)")
我编辑了 base.py 以打印出它正在使用的连接字符串,即:
DRIVER=FreeTDS;DATABASE=test;PWD=test;UID=sa;PORT=1433;SERVER=10.13.36.223
我的 settings.py 数据库看起来像:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'test',
'USER': 'sa',
'PASSWORD': 'test',
'HOST': '10.13.36.223',
'PORT': '1433',
'AUTOCOMMIT': True,
'OPTIONS' : {
'host_is_server': True
}
}
}
我的假设是连接 str 的 DRIVER
部分应该是 odbc 驱动程序的可执行文件的完全限定路径,而现在它设置为 "FreeTDS",这不是t 作为文件存在。那么我的问题是:
- 如果我的假设是正确的,我该如何更改驱动程序的值?
- 如果我的假设不正确,究竟是什么错误,我该如何解决?
我需要执行以下操作:
sudo apt-get install tdsodbc
- 编辑
/etc/odbcinst.ini
以包括以下内容:
.
[FreeTDS]
Description = TDS Driver for MSSQL
driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
它奏效了。
上面的[FreeTDS]配置很有帮助。
在 settings.py 数据库会话中,我还添加了驱动程序版本以及 unicode 结果设置。这是我的配置:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'myDataBaseNameInMSSQLServer',
'USER': 'mssql-user-with-SQL-authentication',
'PASSWORD': 'password',
'HOST': 'server-host-name-or-IP\SQLEXPRESS',
'PORT': '1433',
'OPTIONS' : {
'AUTOCOMMIT': True,
'host_is_server': True,
'unicode_results': True,
'driver': 'FreeTDS',
'extra_params' : 'TDS_VERSION=8.0',
}
}
}
对于 SQL Express,您还需要Google搜索如何使该实例可用于远程连接。
我认为我的问题或多或少是 Trying to query SQL Server from django running on Linux - Can't open lib '/path/to/libtdsodbc.so' 的重复,但那里的答案完全没有用。
我正在使用以下东西:
- Ubuntu15.04
Python3
Django 1.9
(通过 pip3 安装)freetds-dev 0.91-6build1
通过 apt-get 安装django-pyodbc-azure
/django-pyodbc
通过 pip3 安装- MSSQL 2012
尝试连接时,执行 python3 manage.py inspectdb
我得到以下堆栈跟踪:
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
File "/usr/local/lib/python3.4/dist-packages/sql_server/pyodbc/base.py", line 302, in get_new_connection
timeout=timeout)
pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'FreeTDS' : file not found (0) (SQLDriverConnect)")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 342, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/commands/inspectdb.py", line 25, in handle
for line in self.handle_inspection(options):
File "/usr/local/lib/python3.4/dist-packages/django/core/management/commands/inspectdb.py", line 38, in handle_inspection
with connection.cursor() as cursor:
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 231, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 204, in _cursor
self.ensure_connection()
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python3.4/dist-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
File "/usr/local/lib/python3.4/dist-packages/sql_server/pyodbc/base.py", line 302, in get_new_connection
timeout=timeout)
django.db.utils.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'FreeTDS' : file not found (0) (SQLDriverConnect)")
我编辑了 base.py 以打印出它正在使用的连接字符串,即:
DRIVER=FreeTDS;DATABASE=test;PWD=test;UID=sa;PORT=1433;SERVER=10.13.36.223
我的 settings.py 数据库看起来像:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'test',
'USER': 'sa',
'PASSWORD': 'test',
'HOST': '10.13.36.223',
'PORT': '1433',
'AUTOCOMMIT': True,
'OPTIONS' : {
'host_is_server': True
}
}
}
我的假设是连接 str 的 DRIVER
部分应该是 odbc 驱动程序的可执行文件的完全限定路径,而现在它设置为 "FreeTDS",这不是t 作为文件存在。那么我的问题是:
- 如果我的假设是正确的,我该如何更改驱动程序的值?
- 如果我的假设不正确,究竟是什么错误,我该如何解决?
我需要执行以下操作:
sudo apt-get install tdsodbc
- 编辑
/etc/odbcinst.ini
以包括以下内容:
.
[FreeTDS]
Description = TDS Driver for MSSQL
driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
它奏效了。
上面的[FreeTDS]配置很有帮助。
在 settings.py 数据库会话中,我还添加了驱动程序版本以及 unicode 结果设置。这是我的配置:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'myDataBaseNameInMSSQLServer',
'USER': 'mssql-user-with-SQL-authentication',
'PASSWORD': 'password',
'HOST': 'server-host-name-or-IP\SQLEXPRESS',
'PORT': '1433',
'OPTIONS' : {
'AUTOCOMMIT': True,
'host_is_server': True,
'unicode_results': True,
'driver': 'FreeTDS',
'extra_params' : 'TDS_VERSION=8.0',
}
}
}
对于 SQL Express,您还需要Google搜索如何使该实例可用于远程连接。