如何使用 FreeTDS ODBC 连接到 SQL Server
我正在尝试通过我的 MacBook 连接到我公司的 SQL Server 数据库,并已按照此处列出的步骤操作:https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Mac-OSX 但继续获取当我进入以下步骤时出现以下错误:
I am trying to connect to my company's SQL Server Databases via my MacBook and have followed the steps outlined here: https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Mac-OSX but keep getting the following error when I get to the following step:
通过运行 isql TEST myuser mypassword
检查一切是否正常.您应该看到以下内容:
Check that all is OK by running isql TEST myuser mypassword
. You should see the following:
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
我已经验证以下方法有效:
I already verified that the following works:
使用 tsql 实用程序测试连接,例如tsql -S TEST -U myuser -P mypassword
.如果这样做有效,您应该会看到以下内容:
Test the connection using the tsql utility, e.g. tsql -S TEST -U myuser -P mypassword
. If this works, you should see the following:
locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
1>
odbcinst.ini 和 odbc.ini 配置文件都在同一个目录下.
The odbcinst.ini and odbc.ini configuration files are both in the same directory.
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
odbcinst.ini 文件配置:
odbcinst.ini file config:
[FreeTDS]
Description=FreeTDS Driver for Linux & MSSQL
Driver=/usr/local/lib/libtdsodbc.so
Setup=/usr/local/lib/libtdsodbc.so
UsageCount=1
odbc.ini 配置:
odbc.ini config:
[TEST]
Description = Test to SQLServer
Driver = FreeTDS
Servername = ServerName
freetdf.conf
freetdf.conf
# $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
我的命令和输出:isql TEST myuser mypass -v
[IM002][unixODBC][Driver Manager]未找到数据源名称且未指定默认驱动程序[ISQL]错误:无法 SQLConnect
My command and the output:
isql TEST myuser mypass -v
[IM002][unixODBC][Driver Manager]Data source name not found and no default driver specified
[ISQL]ERROR: Could not SQLConnect
首先确认您正在编辑正确的配置文件.
Start by confirming that you're editing the correct configuration files.
您可以通过以下方式确认 FreeTDS 的世界观:
You can confirm FreeTDS's view of the world with:
$ 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
这表明系统范围的 freetds.conf
文件将位于路径 /usr/local/etc/freetds.conf
,尽管您可以拥有自己的~/.freetds.conf
中的用户特定版本.
This shows that the System-wide freetds.conf
file will be at the path /usr/local/etc/freetds.conf
, although you can have your own user-specific version at ~/.freetds.conf
.
如果您尝试连接到网络上的 SQL Server,fred.example.com
,您可以在 freetds.conf:
If you're trying to connect to an SQL Server on your network, fred.example.com
, you can create an alias for it in freetds.conf:
[fred]
host = fred.example.com
port = 1433
tds version = auto
client charset = UTF-8
[fred]
别名不区分大小写.TSQL 可以通过以下任一方式连接到它:
The [fred]
alias is not case-sensitive. TSQL can connect to it with any of the following:
$ tsql -S fred -U "FRED\YourSQLUserName" -P "YourSQLPassword"
$ tsql -S FRED -U "FRED\YourSQLUserName" -P "YourSQLPassword"
$ tsql -S FrEd -U "FRED\YourSQLUserName" -P "YourSQLPassword"
...等等.
一旦您确定 FreeTDS 正在运行,您就可以继续使用 ODBC.您可以通过以下方式查看 ODBC 的世界观:
Once you've established that FreeTDS is working you can move on to ODBC. You can check ODBC's view of the world with:
$ 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
首先编辑 /usr/local/etc/odbcinst.ini
文件(开始时为空)并添加以下内容:
Start by editing the /usr/local/etc/odbcinst.ini
file (which start out empty) and adding the following:
[FreeTDS]
Description=FreeTDS Driver for Linux & MSSQL
Driver=/usr/local/lib/libtdsodbc.so
Setup=/usr/local/lib/libtdsodbc.so
UsageCount=1
接下来,您可以在 /usr/local/etc/odbc.ini
中为 Fred 添加一个系统范围的数据源,或者在 ~/.odbc.ini 中添加一个用户特定的数据源代码>:
Next, you can add a System-wide data source for Fred in /usr/local/etc/odbc.ini
or a user-specific one in ~/.odbc.ini
:
[fred]
Description = Test to SQLServer
Driver = FreeTDS
Servername = fred
注意这里的Servername = fred
指向freetds.conf
中的[fred]
.它也不区分大小写,但您不应将一个称为 fred
的另一个称为 daphne
.
Note that the Servername = fred
here points to the [fred]
in freetds.conf
. It's not case-sensitive, either, but you shouldn't have one called fred
and the other called daphne
.
现在您应该可以使用 ODBC 连接:
Now you should be able to connect using ODBC with:
$ isql fred "FRED\YourSQLUserName" "YourSQLPassword"
希望这会有所帮助.