我使用Homebrew安装了freetds。我的配置如下:
odbcinst -j:
unixODBC 2.3.1
DRIVERS............: /usr/local/Cellar/unixodbc/2.3.1/etc/odbcinst.ini
SYSTEM DATA SOURCES: /usr/local/Cellar/unixodbc/2.3.1/etc/odbc.ini
FILE DATA SOURCES..: /usr/local/Cellar/unixodbc/2.3.1/etc/ODBCDataSources
USER DATA SOURCES..: /Users/mark.richman/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
/usr/local/Cellar/unixodbc/2.3.1/etc/odbcinst.ini:
[ODBC]
Trace = Yes
TraceFile = /tmp/odbc.log
/usr/local/Cellar/freetds/0.91/etc/freetds.conf:
[db.mydomain.local]
host = db.mydomain.local
port = 1433
tds version = 8.0
client charset = UTF-8
~/.odbc.ini:
[MomImporterDSN]
Description = Mom Importer
Driver = /usr/local/Cellar/freetds/0.91/lib/libtdsodbc.so
Servername = db.mydomain.local
Database = MomImporter
UserName = mark.richman
Password = mypassword
当我尝试isql -v MomImporterDSN
时,我收到以下错误:
[S1000][unixODBC][FreeTDS][SQL Server]Unable to connect to data source
[01000][unixODBC][FreeTDS][SQL Server]Adaptive Server connection failed
[ISQL]ERROR: Could not SQLConnect
我的/tmp/odbc.log
似乎并不特别有用:
[ODBC][14514][1373290079.248688][__handles.c][460]
Exit:[SQL_SUCCESS]
Environment = 0x7fcbe1004c00
[ODBC][14514][1373290079.249314][SQLAllocHandle.c][375]
Entry:
Handle Type = 2
Input Handle = 0x7fcbe1004c00
[ODBC][14514][1373290079.249470][SQLAllocHandle.c][493]
Exit:[SQL_SUCCESS]
Output Handle = 0x7fcbe1024800
[ODBC][14514][1373290079.249712][SQLConnect.c][3700]
Entry:
Connection = 0x7fcbe1024800
Server Name = [MOMImporterDSN][length = 14 (SQL_NTS)]
User Name = [NULL]
Authentication = [NULL]
UNICODE Using encoding ASCII 'char' and UNICODE 'UCS-2-INTERNAL'
DIAG [01000] [FreeTDS][SQL Server]Adaptive Server connection failed
DIAG [S1000] [FreeTDS][SQL Server]Unable to connect to data source
[ODBC][14514][1373290079.703504][SQLConnect.c][4067]
Exit:[SQL_ERROR]
[ODBC][14514][1373290079.704257][SQLError.c][434]
Entry:
Connection = 0x7fcbe1024800
SQLState = 0x7fff5a823526
Native = 0x7fff5a823520
Message Text = 0x7fff5a823530
Buffer Length = 500
Text Len Ptr = 0x7fff5a82351e
[ODBC][14514][1373290079.704559][SQLError.c][471]
Exit:[SQL_SUCCESS]
SQLState = S1000
Native = 0x7fff5a823520 -> 0
Message Text = [[unixODBC][FreeTDS][SQL Server]Unable to connect to data source]
[ODBC][14514][1373290079.704777][SQLError.c][434]
Entry:
Connection = 0x7fcbe1024800
SQLState = 0x7fff5a823526
Native = 0x7fff5a823520
Message Text = 0x7fff5a823530
Buffer Length = 500
Text Len Ptr = 0x7fff5a82351e
[ODBC][14514][1373290079.704996][SQLError.c][471]
Exit:[SQL_SUCCESS]
SQLState = 01000
Native = 0x7fff5a823520 -> 20002
Message Text = [[unixODBC][FreeTDS][SQL Server]Adaptive Server connection failed]
[ODBC][14514][1373290079.705172][SQLError.c][434]
Entry:
Connection = 0x7fcbe1024800
SQLState = 0x7fff5a823526
Native = 0x7fff5a823520
Message Text = 0x7fff5a823530
Buffer Length = 500
Text Len Ptr = 0x7fff5a82351e
[ODBC][14514][1373290079.705357][SQLError.c][471]
Exit:[SQL_NO_DATA]
[ODBC][14514][1373290079.705583][SQLError.c][514]
Entry:
Environment = 0x7fcbe1004c00
SQLState = 0x7fff5a823526
Native = 0x7fff5a823520
Message Text = 0x7fff5a823530
Buffer Length = 500
Text Len Ptr = 0x7fff5a82351e
[ODBC][14514][1373290079.705763][SQLError.c][551]
Exit:[SQL_NO_DATA]
[ODBC][14514][1373290079.706061][SQLFreeHandle.c][284]
Entry:
Handle Type = 2
Input Handle = 0x7fcbe1024800
[ODBC][14514][1373290079.706195][SQLFreeHandle.c][333]
Exit:[SQL_SUCCESS]
[ODBC][14514][1373290079.706415][SQLFreeHandle.c][219]
Entry:
Handle Type = 1
Input Handle = 0x7fcbe1004c00
我正在使用Mountain Lion 10.8.4,unixodbc 2.3.1,freetds 0.91和SQL Server 2008.
谢谢!
答案 0 :(得分:2)
按照https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Mac-OSX
中的所有说明操作但是,如果isql / osql不起作用(假设tsql正常工作),则几乎没有其他步骤和黑客攻击。他们可能听起来很奇怪,但解决了问题(isql / osql没有连接虽然tsql工作正常)对我来说。
下面列出的每个步骤都是调试osql错误消息的结果。
在一个或多个步骤中,您可能需要其他root权限。必要时授予它们并在实现pyodbc连接后撤销。
文件odbc.ini
和odbcinst.ini
也应该出现在/Users/<username>/etc
中,除了官方文档指定的位置。
odbcinst.ini
中也应该有/(root directory)
的副本。
文件freetds.conf
应出现在/usr/local/etc
目录中。
如果您使用的是SQL Server 2014及更高版本,则应将tds版本设置为7.4。不要将tds版本设置为8,因为它只是7.1的别名。
检查文件/usr/local/lib/libtdsodbc.so
是否具有执行权限。如果没有,请通过chmod授予766/744/777(在最坏的情况下)特权。这一步对于运行osql / isql以及从python进行pyodbc连接至关重要。
完成这些步骤后,请重新检查odbc.ini
,odbcinst.ini
和freetds.conf
的内容,并确认它们符合文档(https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Mac-OSX)
运行isql / osql。
如果现在一切正常,那么你们都被设置为从python连接到SQLServer。以下代码段显示了基本连接过程。
import pyodbc
connection = pyodbc.connect('DSN=MYMSSQL;UID=<user_id for db_login>;PWD=<password_for db_login>)
cursor = connection.cursor()
result = cursor.execute("select @@VERSION").fetchall()
print(result)
cursor.close()
connection.close()
答案 1 :(得分:0)
您可以通过在ODBC设置中指定TDS版本来修复此错误
[MomImporterDSN]
Description = Mom Importer
Driver = /usr/local/Cellar/freetds/0.91/lib/libtdsodbc.so
TDS_Version = 8.0
Servername = db.mydomain.local
Database = MomImporter
UserName = mark.richman
Password = mypassword