如何测试从 Linux 到 Microsoft SQL Server 的连接?

如何测试从 Linux 到 Microsoft SQL Server 的连接?

检查从 Linux 服务器(例如 Ubuntu 18.04)到远程 Microsoft SQL Server 2017 实例的连接的最简单方法是什么?

答案1

我认为最简单的方法是尝试从命令行 telnet 到 MS SQL 服务器:

$ telnet <server-name-or-ip> 1433

如果连接成功,屏幕将变为空白,否则你会看到类似“连接到服务器名称或 ip...无法在端口 1433 上打开与主机的连接:连接失败。”

答案2

另一个简单的方法是从命令行使用nc( ):netcat

nc -zv YOUR_SERVER_NAME_OR_IP 1433

如果可以建立连接,您应该会看到如下输出:

Connection to YOUR_SQL_SERVER_NAME_OR_IP 1433 port [tcp/ms-sql-s] succeeded!

失败时,您会根据原因收到不同的错误消息:

nc: getaddrinfo for host "NON_EXISTING_SQL_SERVER_NAME_OR_IP" port 1433: Name or service not known

或者可能...

nc: connect to YOUR_SQL_SERVER_NAME_OR_IP port 143 (tcp) failed: Connection timed out

nc手册页中,z选项v包括:

     -z      Specifies that nc should just scan for listening daemons, without sending any
             data to them.  It is an error to use this option in conjunction with the -l
             option.
     -v      Have nc give more verbose output.

答案3

在远程 Microsoft SQL Server 实例上:确保防火墙中的端口 TCP 1433 已打开。

在 Ubuntu Server 实例上安装 mssql-tools,如下所示:https://www.microsoft.com/en-us/sql-server/developer-get-started/php/ubuntu

sudo su
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
exit
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install msodbcsql17 mssql-tools
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
sudo apt-get install unixodbc-dev

然后您可以运行测试查询 - 系统将提示您输入密码:

[user@lnx01 ~ $] sqlcmd -S <remoteip> -U <user> -p -Q "SELECT @@VERSION"
Password: 

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Microsoft SQL Server 2017 (RTM) - 14.0.1000.169 (X64) 
    Aug 22 2017 17:04:49 
    Copyright (C) 2017 Microsoft Corporation
    Express Edition (64-bit) on Windows Server 2016 Standard 10.0 <X64> (Build 14393: )


(1 rows affected)

相关内容