6

StackExchange 上的第一篇文章 - 请放轻松 :)

我在 Centos 6 中设置了 ODBC,以便从我的 Asterisk 安装中执行 ms-sql 查询。

我的配置文件是:

/etc/odbc.ini

[asterisk-connector]
Description     = MS SQL connection to 'asterisk' database
Driver          = /usr/lib64/libtdsodbc.so
Setup           = /usr/lib64/libtdsS.so
Servername      = SQL2
Port            = 1433
Username        = MyUsername
Password        = MyPassword
TDS_Version     = 7.0

/etc/odbcinst.ini

[odbc-test]
Description = TDS connection
Driver = /usr/lib64/libtdsodbc.so
Setup = /usr/lib64/libtdsS.so
UsageCount = 1
FileUsage = 1

/etc/asterisk/res_odbc.conf

[asterisk-connector]
enabled => yes
dsn => asterisk-connector
username => MyUsername
password => MyPassword
pooling => no
limit =>
pre-connect => yes

当我输入密码和用户名时,我可以通过 ISQL 进行连接:

[root@TestVM etc]# isql -v asterisk-connector MyUsername MyPassword
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL>

..但我应该能够在没有用户名/密码的情况下连接。所有返回的是:

[root@TestVM etc]# isql -v asterisk-connector
[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

就好像 ISQL 无法从配置文件中读取用户名和密码一样。

我需要能够从 Asterisk 拨号计划中执行 MS-SQL 查找,但要做到这一点,我必须能够仅使用数据源名称调用 ISQL,并且不能传入身份验证参数。

我在网上阅读的所有指南都表明我应该能够与

isql -v asterisk-connector

命令,但这对我来说并没有发生。

几天来,我一直在努力解决这个问题,因此非常感谢任何朝着正确方向的帮助或指示。

提前致谢。

编辑:

我已经打开了日志记录,并且可能有一个线索。用户名和密码肯定没有被传入。看:

[ODBC][27557][1455205133.129690][SQLConnect.c][3614]
                Entry:
                        Connection = 0xac3080
                        Server Name = [asterisk-connector][length = 18 (SQL_NTS)]
                        User Name = [NULL]
                        Authentication = [NULL]
                UNICODE Using encoding ASCII 'ISO8859-1' and UNICODE 'UCS-2LE'

                DIAG [01000] [FreeTDS][SQL Server]Adaptive Server connection failed

                DIAG [S1000] [FreeTDS][SQL Server]Unable to connect to data source

所以这里的用户名和身份验证是[NULL]。它显然没有在 odbc.ini 或 res_odbc.conf 中获取用户名/密码,但问题是为什么。我会继续调查的:)

编辑2:

OSQL 实用程序返回:

[root@TestVM etc]# osql -S SQL2 -U MyUsername -P MyPassword
checking shared odbc libraries linked to isql for default directories...
strings: '': No such file
        trying /tmp/sqlH ... no
        trying /tmp/sqlL ... no
        trying /etc ... OK
checking odbc.ini files
        reading /root/.odbc.ini
[SQL2] not found in /root/.odbc.ini
        reading /etc/odbc.ini
[SQL2] found in /etc/odbc.ini
found this section:
looking for driver for DSN [SQL2] in /etc/odbc.ini
  no driver mentioned for [SQL2] in odbc.ini
looking for driver for DSN [default] in /etc/odbc.ini
osql: error: no driver found for [SQL2] in odbc.ini
4

4 回答 4

0

我会在您的 odbc.ini 中将“用户名”替换为“ UID ”,将“密码”替换为“ PWD ”......来自FreeTDS 手册 - 第 4 章 - 准备 ODBC

这个难题的原始 ODBC 解决方案使用 odbc.ini 文件。odbc.ini 存储有关服务器的信息,通常称为数据源名称 (DSN)。ODBC 应用程序通过调用函数 SQLConnect(DSN, UID, PWD) 连接到服务器,其中 DSN 是 odbc.ini 中的数据源名称条目,UID 是用户名,PWD 是密码。有关 DSN 的所有信息都保存在 odbc.ini 中。世界上一切都很好。

ODBC 3.0 规范引入了一个新功能:SQLDriverConnect。连接属性作为单个参数提供,即一串串联的名称-值对。SQLDriverConnect 包含了 SQLConnect 的功能,因为名称-值对字符串允许调用者传递——除了原始 DSN、UID 和 PWD——驱动程序可以接受的任何其他参数。此外,应用程序可以指定使用哪个驱动程序。实际上,可以将整个 DSN 属性集指定为 SQLDriverConnect 的参数,从而无需 odbc.ini。这导致使用所谓的无 DSN 配置,即没有 odbc.ini 的设置。

于 2016-02-12T07:49:26.917 回答
0

好的,所以我解决了它(几乎)。我的 odbc 文件中的密码和用户名被忽略了。因为我是从 Asterisk 调用数据库查询,所以我也使用了一个名为 res_odbc.ini 的文件。这也包含我的用户名和密码,当我从 Asterisk 运行查询时,它会连接并返回正确的结果。

如果有帮助,这是我的最终工作配置。

odbc.ini

[asterisk-connector]
Description = MS SQL connection to asterisk database
driver = /usr/lib64/libtdsodbc.so
servername = SQL2
Port = 1433
User = MyUsername
Password = MyPassword

odbcinst.ini

[FreeTDS]
Description = TDS connection
Driver = /usr/lib64/libtdsodbc.so
UsageCount = 1

[ODBC]
trace           = Yes
TraceFile       = /tmp/sql.log
ForceTrace      = Yes

freetds.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 = 4.2

        # 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

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

# A typical Microsoft server
[SQL2]
        host = 192.168.1.59
        port = 1433
        tds version = 8.0

res_odbc.conf

[asterisk-connector]
enabled = yes
dsn = asterisk-connector
username = MyUsername
password = MyPassword
pooling = no
limit = 1
pre-connect = yes

请记住,如果您使用 Centos 64 位将驱动程序路径修改为 lib64。大多数在线指南都有错误的(64 位)路径。

祝你好运 - 这很头疼:)

于 2016-02-12T16:20:23.923 回答
0

我就这个问题联系了 unixODBC 的开发者 Nick Gorham,他确认isql 没有从配置文件中读取用户名/密码

嗨尼克,

我认为 unixODBC 是一个很棒的项目,但我惊讶地发现它不安全(或者至少我不知道如何正确使用它)。

当我使用 isql 连接到数据库时,我必须输入密码。在共享服务器上,这是不安全的,因为

$ ps –辅助

命令以明文形式显示密码。

有解决办法吗?我可以将密码放在只有我的用户可读的文件中吗?

感谢您的帮助。

答案:

你好,

这取决于驱动程序。有些人可以从 odbc.ini 或 ~/.odbc.ini 文件中读取用户和密码,因此您可以将密码存储在那里。

isql 仅设计为一个简单的测试应用程序,没有什么可以阻止您修改 ilsq 以从您选择的文件中提取用户和密码,并在需要时对其进行解密。

于 2016-09-23T15:07:50.913 回答
0

我遇到了一个稍微不同的问题,但我的谷歌搜索把我带到了这里。尝试通过 isql 连接时,Login failed for user ''即使我在 odbc.ini 文件中指定了一个用户,我也得到了

[SQLSERVER_SAMPLE]
Driver=ODBC Driver 17 for SQL Server
Server=SERVER
Database=DATABASE
Trusted_Connection=no
UID=USER
PWD=PASSWORD

我都尝试了UIDand User,但都给出了同样的错误。阅读@Andrei Sura的解决方案后,我发现用户名和密码被忽略了。

我的解决方案是运行,isql -v SQLSERVER_SAMPLE USER PASSWORD即使在 odbc.ini 文件中指定了用户名和密码 - 并且它已连接。

于 2021-06-18T14:11:21.663 回答