使用 freetds 和 unixodbc 连接 MS SQL:isql - 未指定默认驱动程序

Dej*_*jan 28 server database sql

我正在尝试使用freetdsunixodbc连接到 MS SQL 数据库。我已经阅读了各种指南如何做到这一点,但没有一个适合我。当我尝试使用isql工具连接到数据库时,出现以下错误:

$ isql -v TS username password
[IM002][unixODBC][Driver Manager]Data source name not found, and no default driver specified
[ISQL]ERROR: Could not SQLConnect
Run Code Online (Sandbox Code Playgroud)

是否有人已经在 Ubuntu 12.04 上使用freetdsunixodbc成功建立了与 MS SQL 数据库的连接?我真的很感激一些帮助。

下面是我用来配置freetdsunixodbc 的过程。提前感谢您的帮助!

程序

首先,我安装了以下软件包:

sudo apt-get install unixodbc unixodbc-dev freetds-dev tdsodbc
Run Code Online (Sandbox Code Playgroud)

并配置freetds如下:

--- /etc/freetds/freetds.conf ---
[TS]
host = SERVER
port = 1433
tds version = 7.0
client charset = UTF-8
Run Code Online (Sandbox Code Playgroud)

使用tsql工具我可以通过执行成功连接到数据库

tsql -S TS -U username -P password
Run Code Online (Sandbox Code Playgroud)

因为我需要一个odbc连接,所以我配置了odbcinst.ini如下:

--- /etc/odbcinst.ini ---
[FreeTDS]
Description = FreeTDS
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
FileUsage = 1
CPTimeout =
CPResuse  =
client charset = utf-8
Run Code Online (Sandbox Code Playgroud)

odbc.ini如下:

--- /etc/odbc.ini ---
[TS]
Description = "test"
Driver = FreeTDS
Servername = SERVER
Server = SERVER
Port = 1433
Database = DBNAME
Trace = No
Run Code Online (Sandbox Code Playgroud)

尝试使用具有此类配置的isql工具连接到数据库会导致以下错误:

$ isql -v TS username password
[IM002][unixODBC][Driver Manager]Data source name not found, and no default driver specified
[ISQL]ERROR: Could not SQLConnect
Run Code Online (Sandbox Code Playgroud)

小智 17

谢谢,你的帖子对我很有用。我能够通过从我的 odbcinst.ini 文件中删除以下几行来让它工作

Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
FileUsage = 1
CPTimeout =
CPResuse  =
client charset = utf-8
Run Code Online (Sandbox Code Playgroud)

所以现在我的 odbcinst.ini 文件看起来像这样:

--- /etc/odbcinst.ini ---
[FreeTDS]
Description = FreeTDS
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Run Code Online (Sandbox Code Playgroud)

我的 odbc.ini 文件现在看起来像这样:

--- /etc/odbc.ini ---
[TS]
Description = "test"
Driver = FreeTDS
Server = SERVER
Port = 1433
Database = DBNAME
Run Code Online (Sandbox Code Playgroud)

一旦我简化了一切,它就很好用。我仍然无法让它与 RODBC 一起工作,但它与 isql 一起工作。

我不知道这是否会有所帮助,但您的帖子对我有所帮助。谢谢。


use*_*735 14

这是一个最小的,但完整的例子如何连接到Azure的SQL数据库isql从Ubuntu的LTS 14.04.1。该示例摘自如何从 Ubuntu 连接 Azure SQL 数据库(免责声明:这是我的个人 wiki)。

安装必要的包

$ sudo apt-get -y install freetds-bin tdsodbc unixodbc
Run Code Online (Sandbox Code Playgroud)

配置 FreeTDS

文件 /etc/freetds/freetds.conf

[global]
tds version = 7.1

[<SERVERNAME>]
host = <HOST>.database.windows.net
port = 1433
Run Code Online (Sandbox Code Playgroud)

测试连接

此时连接tsql应该可以工作:

$ tsql -S <SERVERNAME> -U <USERNAME>@<HOST> -P <PASSWORD>
Run Code Online (Sandbox Code Playgroud)

请注意,这@<HOST>是必需的。否则连接以错误结束:

Msg 40531 (severity 11, state 1) from [<SERVERNAME>] Line 1:
    "Server name cannot be determined.  It must appear as the first segment of the server's dns name (servername.database.windows.net).  Some libraries do not send the server name, in which case the server name must be included as part of the user name (username@servername).  In addition, if both formats are used, the server names must match."
Error 20002 (severity 9):
    Adaptive Server connection failed
There was a problem connecting to the server
Run Code Online (Sandbox Code Playgroud)

配置 ODBC 驱动程序

文件 /etc/odbcinst.ini

[FreeTDS]
Description = FreeTDS Driver
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Run Code Online (Sandbox Code Playgroud)

配置 ODBC 数据源

文件 /etc/odbc.ini

[<DATA_SOURCE_NAME>]
Driver = FreeTDS
Servername = <SERVERNAME>
Port = 1433
Database = <DBNAME>
Run Code Online (Sandbox Code Playgroud)

<SERVERNAME>与中相同freetds.conf

与 isql 连接

$ isql -v <DATA_SOURCE_NAME> <USER>@<HOST> <PASSWORD>
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL> select current_timestamp
+------------------------+
|                        |
+------------------------+
| 2015-01-02 09:05:55.593|
+------------------------+
SQLRowCount returns 1
1 rows fetched
SQL>
Run Code Online (Sandbox Code Playgroud)

请注意,这@<HOST>是必需的。否则连接以错误结束:

[S1000][unixODBC][FreeTDS][SQL Server]Unable to connect to data source
[37000][unixODBC][FreeTDS][SQL Server]Server name cannot be determined.  It must appear as the first segment of the server's dns name (servername.database.windows.net).  Some libraries do not send the server name, in which case the server name must be included as part of the user name (username@servername).  In addition, if both formats are used, the server names must match.
[ISQL]ERROR: Could not SQLConnect
Run Code Online (Sandbox Code Playgroud)


Sud*_*shu 8

在我的情况下,问题是由于我的配置文件中的简单缩进而引起的。所以在 中/etc/odbc.ini,我删除了所有缩进,瞧!

odbcinst.ini表现得像一个正常的孩子,似乎没有发脾气。)