在 RHEL 6 上使用 sqlcmd 和 odbc 驱动程序连接到 MSSQL 服务器失败

在 RHEL 6 上使用 sqlcmd 和 odbc 驱动程序连接到 MSSQL 服务器失败

我花了整整一个小时尝试通过 odbc 驱动程序使用 sqlcmd 连接到 mssql 服务器。我和数据库管理员交谈,但他似乎不知道发生了什么。也许你可以帮我找出一些我可以问那个人的问题。

我正在使用 Microsoft 的官方 odbc 驱动程序。显然我们正在使用某种动态端口。

中间没有防火墙。

# odbcinst -q -d -n
[PostgreSQL]
[MySQL]
[ODBC Driver 11 for SQL Server]

我尝试了不同的方法

错误代码 0x2AF9:

sqlcmd -S hostname\DEV04 -U username -P password
Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..
Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : TCP Provider: Error code 0x2AF9.

定位指定的服务器/实例时出错 [xFFFFFFFF]。

sqlcmd -S hostname\\DEV04 -U username -P password
Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..
Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF]. 

使用 perl

DBI connect('DASHBOARD','username',...) failed: [unixODBC][Microsoft][ODBC Driver 11 for SQL Server]Login timeout expired (SQL-HYT00) [state was HYT00 now 08001]
[unixODBC][Microsoft][ODBC Driver 11 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (SQL-08001)
[unixODBC][Microsoft][ODBC Driver 11 for SQL Server]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].  (SQL-08001) at ./test.pl line 4
Can't connect to : [unixODBC][Microsoft][ODBC Driver 11 for SQL Server]Login timeout expired (SQL-HYT00) [state was HYT00 now 08001]
[unixODBC][Microsoft][ODBC Driver 11 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (SQL-08001)
[unixODBC][Microsoft][ODBC Driver 11 for SQL Server]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].  (SQL-08001) at ./test.pl line 4.

答案1

阅读完此帖子后: https://github.com/Microsoft/msphpsql/issues/190

我必须通过执行以下命令来找出 SQL Server 正在监听的另一个端口号

USE MASTER 
GO 
xp_readerrorlog 0, 1, N'Server is listening on' 
GO

结果

2018-04-12 03:19:57.830 Server       Server is listening on [ 'any' <ipv6> 49155].                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
2018-04-12 03:19:57.830 Server       Server is listening on [ 'any' <ipv4> 49155]. 

为 SQL Server 主机上的 Windows 防火墙添加规则: - 启用特定端口 49155(我认为这可能会有所不同)

然后这个命令在我的 Ubuntu 机器上成功执行:

sqlcmd -Smyhost,49155 -U sa -P sa_pwd -d mydb -Q "select @@servername"

答案2

我遇到了类似的问题。事实证明,\DEV04的部分hostname\DEV04用于指示客户端应通过与默认端口不同的端口连接到服务器。在 Microsoft 世界中,使用DEV04自动确保客户端连接到正确的端口……但这似乎不适用于 Linux sqlcmd。

询问 SQL Server 管理员\DEV04实例正在监听哪个端口并使用hostname,PORT

答案3

根据此处的简介:https://social.msdn.microsoft.com/Forums/sqlserver/en-US/36b8956e-0218-44aa-b717-725fe1d601f9/error-in-sqlserver-odbc-dirver-for-linux?forum=sqldataaccess

使用 Microsoft 提供的 ODBC 工具和 sqlcmd 时,UDP 协议在 Linux 中无法工作。UDP 部分最初用于探测服务器以查找请求的数据库正在监听的 TCP 端口。

我工作中使用的解决方案是切换到 pymssql,它将与命名实例和 UDP 端口请求一起工作。

相关内容