Error trying to connect to a SQL Server using sqlcmd (from Ubuntu command line)?

醉酒当歌 提交于 2020-01-15 05:02:28

问题


I am finding some problem trying to connect to a SQL Server instance from an Ubuntu machine using sqlcmd.

So I installed sqlcmd as explained here: https://docs.microsoft.com/it-it/sql/linux/sql-server-linux-setup-tools?view=sql-server-ver15#ubuntu

The SQL server seems to be reachable because I can successfully connect to the 1433 port of its IP via Telnet.

Then I tried to connect to one of my database on this server, in this way:

./sqlcmd -S MY_SERVER_IP\ESB_WSO2_USER_DB

Where MY_SERVER_IP is the IP of this server and ESB_WSO2_USER_DB is the name of a database on this server.

The problem is that I am obtaining the following message error as output:

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.

So what could be the problem? Is it something that could be related to my sqlcmd installation (maybe some missconfiguration) or something related to my connection string? or something related to the SQL Server database?

The message says that maybe the SQL Server maybe is not allowing external connection but...if so...why Telnet connection works fine?


回答1:


-S MY_SERVER_IP\ESB_WSO2_USER_DB means the IP of the server, and the name of the instance. Windows Hosts can run multiple instances of SQL Server, and when they do, latter instances need to be given a different name to the default instance name (MSSQLSERVER) and run on a different port. As a result you can specify the name afterwards, rather than the port (if you have named servers running). For example SRVSQLHost\DevInstance would connect the to instance DevInstance on the server SRVSQLHost; not the database DevInstance on the instance MSSQLSERVER on the host SRVSQLHost.

You pass the name of the database using the -d switch. If you run simply sqlcmd (in Bash, not Powershell, where you need to use sqlcmd -?) you'll see the input switches:

:~$ 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]

Note that some switches available on Windows Powershell are not available in Bash and Powershell for Linux; such as parameters.

So, for what you have you want:

sqlcmd -S MY_SERVER_IP -d ESB_WSO2_USER_DB -U YOUR_LOGIN_NAME

(I assume you need to pass the -U switch, unless you have configured kerboros on your Ubuntu instance, but you didn't mention it, so I assumed not. Note that the LOGIN for -U must be a SQL Authentication login, not an AD login.)



来源:https://stackoverflow.com/questions/59175505/error-trying-to-connect-to-a-sql-server-using-sqlcmd-from-ubuntu-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!