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


28

我正在尝试使用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

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

以下是我用来配置freetdsunixodbc的过程。谢谢您的帮助!

程序

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

sudo apt-get install unixodbc unixodbc-dev freetds-dev tdsodbc

并配置freetds如下:

--- /etc/freetds/freetds.conf ---
[TS]
host = SERVER
port = 1433
tds version = 7.0
client charset = UTF-8

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

tsql -S TS -U username -P password

由于需要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

odbc.ini如下:

--- /etc/odbc.ini ---
[TS]
Description = "test"
Driver = FreeTDS
Servername = SERVER
Server = SERVER
Port = 1433
Database = DBNAME
Trace = No

尝试使用具有这样配置的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

使用tsqlsudo apt-get install freetds-bin
Stevie G

Answers:


17

谢谢,您的帖子对我非常有用。通过从odbcinst.ini文件中消除以下几行,我使其能够工作

Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
FileUsage = 1
CPTimeout =
CPResuse  =
client charset = utf-8

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

--- /etc/odbcinst.ini ---
[FreeTDS]
Description = FreeTDS
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so

现在我的odbc.ini文件如下所示:

--- /etc/odbc.ini ---
[TS]
Description = "test"
Driver = FreeTDS
Server = SERVER
Port = 1433
Database = DBNAME

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

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


谢谢,这绝对与缺少驱动程序有关/etc/odbcinst.ini
Dejan 2013年

1
嘿,好答案,但不幸的是,即使复制文件,我也无法使它正常工作。为什么有任何想法?除了在SERVER部分我使用IP而不是名称之外,其他内容几乎相同。您认为可能吗?非常感谢
Pedro Braz 2015年

14

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

安装必要的软件包

$ sudo apt-get -y install freetds-bin tdsodbc unixodbc

配置FreeTDS

文件 /etc/freetds/freetds.conf

[global]
tds version = 7.1

[<SERVERNAME>]
host = <HOST>.database.windows.net
port = 1433

测试连接

此时与的连接tsql应该起作用:

$ tsql -S <SERVERNAME> -U <USERNAME>@<HOST> -P <PASSWORD>

请注意这@<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

配置ODBC驱动程序

文件 /etc/odbcinst.ini

[FreeTDS]
Description = FreeTDS Driver
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so

配置ODBC数据源

文件 /etc/odbc.ini

[<DATA_SOURCE_NAME>]
Driver = FreeTDS
Servername = <SERVERNAME>
Port = 1433
Database = <DBNAME>

<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>

请注意这@<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

@<HOST>被要求不再出现。
阿德里安·基斯特

7

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

odbcinst.ini行为就像一个普通的孩子,似乎没有发脾气。)


谢谢!我一直在注视着这个配置达2个小时,试图弄清楚空格是问题所在。
Alex Barker

3

12.04之前的Ubuntu在/etc/odbcinst.ini文件中具有不同的odbc路径。

旧的驱动程序路径为:

Driver = /usr/lib/odbc/libtdsodbc.so

我将其更改为:

Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so

这是完整的配置:

--- /etc/odbcinst.ini ---
[FreeTDS]
Description = tdsodbc
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
FileUsage = 1
CPTimeout = 5
CPReuse = 5

现在就像魅力一样!谢谢!


现在不赞成安装线吗?
阿德里安·基斯特
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.