我在尝试使用 sqlcmd 从 Ubuntu 计算机连接到 SQL Server 实例时发现一些问题。
所以我安装了sqlcmd,如下所述:https://learn.microsoft.com/it-it/sql/linux/sql-server-linux-setup-tools?view=sql-server-ver15#ubuntu
SQL 服务器似乎可以访问,因为我可以通过 Telnet 成功连接到其 IP 的 1433 端口。
然后我尝试通过以下方式连接到该服务器上的一个数据库:
./sqlcmd -S MY_SERVER_IP\ESB_WSO2_USER_DB
其中 MY_SERVER_IP 是该服务器的 IP,ESB_WSO2_USER_DB 是该服务器上数据库的名称。
问题是我收到以下消息错误作为输出:
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2AFA.
Sqlcmd: Error: Microsoft ODBC Driver 17 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 安装有关(可能是一些配置错误)或与我的连接字符串有关?或者与SQL Server数据库相关的东西?
该消息表示 SQL Server 可能不允许外部连接,但是...如果是这样...为什么 Telnet 连接工作正常?
最佳答案
-S MY_SERVER_IP\ESB_WSO2_USER_DB
表示服务器的 IP,以及实例的名称。 Windows 主机可以运行 SQL Server 的多个实例,当运行时,后面的实例需要指定与默认实例名称 (MSSQLSERVER
) 不同的名称,并在不同的端口上运行。因此,您可以稍后指定名称,而不是端口(如果您正在运行命名服务器)。例如,SRVSQLHost\DevInstance
将连接到服务器 SRVSQLHost
上的实例 DevInstance
; 不是主机 SRVSQLHost
上实例 MSSQLSERVER
上的数据库 DevInstance
。
您可以使用-d
开关传递数据库的名称。如果您仅运行 sqlcmd
(在 Bash 中,而不是 Powershell,您需要使用 sqlcmd -?
),您将看到输入开关:
:~$ sqlcmd
Microsoft (R) SQL Server Command Line Tool
Version 17.4.0001.1 Linux
Copyright (c) 2012 Microsoft. All rights reserved.
usage: sqlcmd [-U login id] [-P password]
[-S server or Dsn if -D is provided]
[-H hostname] [-E trusted connection]
[-N Encrypt Connection][-C Trust Server Certificate]
[-d use database name] [-l login timeout] [-t query timeout]
[-h headers] [-s colseparator] [-w screen width]
[-a packetsize] [-e echo input] [-I Enable Quoted Identifiers]
[-c cmdend]
[-q "cmdline query"] [-Q "cmdline query" and exit]
[-m errorlevel] [-V severitylevel] [-W remove trailing spaces]
[-u unicode output] [-r[0|1] msgs to stderr]
[-i inputfile] [-o outputfile]
[-k[1|2] remove[replace] control characters]
[-y variable length type display width]
[-Y fixed length type display width]
[-p[1] print statistics[colon format]]
[-R use client regional setting]
[-K application intent]
[-M multisubnet failover]
[-b On error batch abort]
[-D Dsn flag, indicate -S is Dsn]
[-X[1] disable commands, startup script, environment variables [and exit]]
[-x disable variable substitution]
[-? show syntax summary]
请注意,Windows Powershell 上可用的某些开关在 Linux 的 Bash 和 Powershell 中不可用;例如参数。
所以,对于你想要的东西:
sqlcmd -S MY_SERVER_IP -d ESB_WSO2_USER_DB -U YOUR_LOGIN_NAME
(我假设您需要传递 -U
开关,除非您在 Ubuntu 实例上配置了 kerboros,但您没有提及,所以我认为不是。请注意,-U
的 LOGIN
必须是 SQL 身份验证登录,而不是 AD 登录。)
关于sql-server - 尝试使用 sqlcmd(从 Ubuntu 命令行)连接到 SQL Server 时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59175505/