如何使用 FreeTDS ODBC 连接到 SQL Server

ny_*_*ude 1 python sql-server unixodbc pyodbc freetds

我正在尝试通过我的 MacBook 连接到我公司的 SQL Server 数据库,并按照此处概述的步骤操作:https : //github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Mac-OSX但当我进入以下步骤时,不断收到以下错误:

通过运行检查一切是否正常isql TEST myuser mypassword。您应该看到以下内容:

+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
Run Code Online (Sandbox Code Playgroud)

我已经验证了以下工作:

使用 tsql 实用程序测试连接,例如tsql -S TEST -U myuser -P mypassword. 如果这有效,您应该看到以下内容:

locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
1>
Run Code Online (Sandbox Code Playgroud)

odbcinst.ini 和 odbc.ini 配置文件都在同一目录中。

MacBook-Pro: myname$ odbcinst -j
unixODBC 2.3.7
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /Users/myname/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
Run Code Online (Sandbox Code Playgroud)

odbcinst.ini 文件配置:

[FreeTDS]
Description=FreeTDS Driver for Linux & MSSQL
Driver=/usr/local/lib/libtdsodbc.so
Setup=/usr/local/lib/libtdsodbc.so
UsageCount=1
Run Code Online (Sandbox Code Playgroud)

odbc.ini 配置:

[TEST]
Description         = Test to SQLServer
Driver              = FreeTDS
Servername          = ServerName
Run Code Online (Sandbox Code Playgroud)

配置文件

#   $Id: freetds.conf,v 1.12 2007-12-25 06:02:36 jklowden Exp $
#
# This file is installed by FreeTDS if no file by the same 
# name is found in the installation directory.  
#
# For information about the layout of this file and its settings, 
# see the freetds.conf manpage "man freetds.conf".  

# Global settings are overridden by those in a database
# server specific section
[global]
        # TDS protocol version
    tds version = auto

    # Whether to write a TDSDUMP file for diagnostic purposes
    # (setting this to /tmp is insecure on a multi-user system)
;   dump file = /tmp/freetds.log
;   debug flags = 0xffff

    # Command and connection timeouts
;   timeout = 10
;   connect timeout = 10

    # If you get out-of-memory errors, it may mean that your client
    # is trying to allocate a huge buffer for a TEXT field.  
    # Try setting 'text size' to a more reasonable limit 
    text size = 64512

    # If you experience TLS handshake errors and are using openssl,
    # try adjusting the cipher list (don't surround in double or single quotes)
    # openssl ciphers = HIGH:!SSLv2:!aNULL:-DH

# A typical Sybase server
[egServer50]
    host = symachine.domain.com
    port = 5000
    tds version = 5.0

# A typical Microsoft server
[TEST]
    host = ServerName
    port = 1433
    tds version = 7.3
    client charset = UTF-8
Run Code Online (Sandbox Code Playgroud)

我的命令和输出: isql TEST myuser mypass -v [IM002][unixODBC][Driver Manager] 未找到数据源名称且未指定默认驱动程序 [ISQL] 错误:无法 SQLConnect

Alw*_*ing 5

首先确认您正在编辑正确的配置文件。

您可以通过以下方式确认 FreeTDS 的世界观:

$ tsql -C
Compile-time settings (established with the "configure" script)
                            Version: freetds v1.1.11
             freetds.conf directory: /usr/local/etc
     MS db-lib source compatibility: no
        Sybase binary compatibility: yes
                      Thread safety: yes
                      iconv library: yes
                        TDS version: 7.3
                              iODBC: no
                           unixodbc: yes
              SSPI "trusted" logins: no
                           Kerberos: yes
                            OpenSSL: yes
                             GnuTLS: no
                               MARS: yes
Run Code Online (Sandbox Code Playgroud)

这表明系统范围的freetds.conf文件将位于 path /usr/local/etc/freetds.conf,尽管您可以在~/.freetds.conf.

如果您尝试连接到网络上的 SQL Server fred.example.com,您可以在freetds.conf为其创建别名:

[fred]
    host = fred.example.com
    port = 1433
    tds version = auto
    client charset = UTF-8
Run Code Online (Sandbox Code Playgroud)

[fred]别名是不区分大小写。TSQL 可以通过以下任一方式连接到它:

[fred]
    host = fred.example.com
    port = 1433
    tds version = auto
    client charset = UTF-8
Run Code Online (Sandbox Code Playgroud)

... 等等。

一旦您确定 FreeTDS 正在运行,您就可以继续使用 ODBC。您可以通过以下方式检查 ODBC 的世界观:

$ tsql -S fred -U "FRED\YourSQLUserName" -P "YourSQLPassword"
$ tsql -S FRED -U "FRED\YourSQLUserName" -P "YourSQLPassword"
$ tsql -S FrEd -U "FRED\YourSQLUserName" -P "YourSQLPassword"
Run Code Online (Sandbox Code Playgroud)

首先编辑/usr/local/etc/odbcinst.ini文件(从空开始)并添加以下内容:

[FreeTDS]
Description=FreeTDS Driver for Linux & MSSQL
Driver=/usr/local/lib/libtdsodbc.so
Setup=/usr/local/lib/libtdsodbc.so
UsageCount=1
Run Code Online (Sandbox Code Playgroud)

接下来,您可以为 Fred in/usr/local/etc/odbc.ini或用户特定的 in添加系统范围的数据源~/.odbc.ini

[fred]
Description         = Test to SQLServer
Driver              = FreeTDS
Servername          = fred
Run Code Online (Sandbox Code Playgroud)

请注意,Servername = fred此处指向[fred]in freetds.conf。它也不区分大小写,但是您不应该将一个称为fred,另一个称为daphne

现在您应该能够使用 ODBC 连接:

$ odbcinst -j
unixODBC 2.3.7
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/YourUserName/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。