can't connect to azure sql server from ubuntu - sql-server

I am getting the following error when trying to access my azure sql server from terminal using the command
tsql -H 'myserver.database.windows.net' -U 'myuser' -P 'mypass'
Error:
locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
Error 20017 (severity 9):
Unexpected EOF from the server
OS error 115, "Operation now in progress"
Error 20002 (severity 9):
Adaptive Server connection failed
There was a problem connecting to the server
also tried using settings in freetds.conf but same error

You need to make sure you're using TDS version 7.4. Make sure tsql is up to date (you will very likely have to download, build and install from sources) and then force the version from env:
TDSVER=7.4 tsql -H 'myserver.database.windows.net' -U 'myuser' -P 'mypass'

You can use sqlcmd as well.
sqlcmd -S 'myserver.database.windows.net' -U 'myuser' -P 'mypass'
for configuring sqlcmd use url:
https://learn.microsoft.com/en-us/sql/linux/quickstart-install-connect-ubuntu?view=sql-server-2017

Related

sqlcmd works in CMD and not in bash

I am having quite a few problems making MSSQL drivers work in Ubuntu. I have followed the following tutorial to make sqlcmd work in Ubuntu 16.04.
# In CMD:
sqlcmd -S my_server_name -U my_username -P my_password -d my_database
1> select name from sys.databases
2> go
After installing the same tool in Ubuntu, it seems to work, but it timeouts when attempting to connect to the same database:
# In Ubuntu bash
sqlcmd -S my_server_name -U my_username -P my_password -d my_database
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..
I have tried to change configurations of the database, such that the port is static at 1433, but still no luck.
Do you have any suggestions?
I have fixed the problem!
1. Install MSSQL drivers on Ubuntu
Follow this tutorial.
2. Ensure your port on the database is static
Follow this tutorial to set up a static port.
3. Identify the IP adress of your database
I had to call the following code on the database to get the ip-adress: local_net_address.
SELECT
+ CONNECTIONPROPERTY('net_transport') AS net_transport,
+ CONNECTIONPROPERTY('protocol_type') AS protocol_type,
+ CONNECTIONPROPERTY('auth_scheme') AS auth_scheme,
+ CONNECTIONPROPERTY('local_net_address') AS local_net_address,
+ CONNECTIONPROPERTY('local_tcp_port') AS local_tcp_port,
+ CONNECTIONPROPERTY('client_net_address') AS client_net_address
4. Connect to database (in Ubuntu bash)
Here are two examples with netcat and sqlcmd.
# Using MSSQL tool
sqlcmd -S my_server_ip_adress//my_server_name,my_port -U my_username -P my_password -d my_database
# Using netcat
nc -z -v -w5 my_server_ip_adress my_port

freetds connection issue macOS

I am trying to connect to a remote MSSQL DB from a MacOS.
I am able to connect to the remote DB using freetds
tsql -H servername.database.windows.net -U username -P password -D database_name -p 1433
But as I make an entry in the /usr/local/etc/freetds.conf file with following settings
[dbservername]
host = servername.database.windows.net
port = 1433
database = database_name
tds version = 7.3
And try to connect using
tsql -S dbservername -U username -P password
I get the following error
locale is "C/UTF-8/C/C/C/C"
locale charset is "UTF-8"
using default charset "UTF-8"
2Msg 40532 (severity 20, state 1) from dbservername:
"Cannot open server "dbservername" requested by the login. The login failed."
Error 20002 (severity 9):
Adaptive Server connection failed
There was a problem connecting to the server
tsql -C output
Compile-time settings (established with the "configure" script)
Version: freetds v1.00.48
freetds.conf directory: /usr/local/Cellar/freetds/1.00.48/etc
MS db-lib source compatibility: no
Sybase binary compatibility: no
Thread safety: yes
iconv library: yes
TDS version: 7.3
iODBC: no
unixodbc: no
SSPI "trusted" logins: no
Kerberos: no
OpenSSL: yes
GnuTLS: no
MARS: no
Any idea how to fix that?
The problem is that FreeTDS is looking for your configuration file in a different directory. tsql -C shows that it is expecting the file to be in /usr/local/Cellar/freetds/1.00.48/etc.
Move freetds.conf from /usr/local/etc/ to /usr/local/Cellar/freetds/1.00.48/etc and give it a try again. Good luck!
A little late to answer, but it's required to write the username as:
username#servername
when connected to Azure Database (sqlserver in Azure)

Freetds could not connect to SQL Azure by server name (-S), but connect by host (-H) works

I have installed freetds and it looks working fine, when I try to connect by host:
tsql -H myhost -U myuser -P mypass -p 1433
locale is "C/UTF-8/C/C/C/C"
locale charset is "UTF-8"
using default charset "UTF-8"
1>
I have configured freetds.conf:
[azure]
host = myhost
port = 1433
tds version = 7.0
But when I'm trying to connect by server name (azure) it fails:
tsql -S azure -U myuser -P mypass
locale is "C/UTF-8/C/C/C/C"
locale charset is "UTF-8"
using default charset "UTF-8"
Msg 40532 (severity 20, state 1) from azure:
"Cannot open server "azure" requested by the login. The login failed."
Error 20002 (severity 9):
Adaptive Server connection failed
Tried on macos and debian.
The answer was simple, but strange. When you connect by config file (-S) you should use username#host as login, not just username. It's strange, but when you connect by host (-H), you could use only username (but could use username#host too).
Glad it worked out for you. Check out this doc - http://www.freetds.org/userguide/odbcconnattr.htm
As general practice, we recommend that you use -S
Let me know if you have further questions.

Remote connection to SQL Server with FreeTDS and IPv6

I am trying to connect to a remote SQL Server 2008 from a bash file in a Ubuntu 14.04. I am using FreeTDS and the tsql command to do so.
When I am in the same network as the SQL Server, I can access it using its local ip 192.168.0.2, using the following command:
tsql -S 192.168.0.2 -U <user> -P <password>
The problem is when I try to connect using an IPv6. tsql doesn't seem to recognize the IPv6 I got. Here is what I get when trying to connect (the ip is anonymized):
tsql -S fe80::c74a:ada5:be2e:255f%11
Here is the result:
Error 20012 (severity 2):
Server name not found in configuration files.
locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
Error 20013 (severity 2):
Unknown host machine name.
There was a problem connecting to the server
What am I doing wrong here? Any help is appreciated
The problem was that I was using freetds v0.91 (which was the latest version on apt-get), which does not have support for IPv6. I upgraded to the latest version, which can be found here. Any version above v0.95 should be fine.

How to pass instance in freetds - Ubuntu

I am trying to connect to mssql server (2008 R2) from my ubuntu. I have installed freetds driver.
Below is my configuration in freetds.conf:
[titan]
host = SQL_IP_ADDRESS
tds version = 7.0
instance = CENTERFORALLIED\MSSQLSERVER_2K8
Where CENTERFORALLIED\MSSQLSERVER_2K8 is the instance name.
When I try to connect using below command:
tsql -S titan\MSSQLSERVER_2K8 -U user -P pass
OR
tsql -S titan -U user -P pass
OR
tsql -S SQL_IP_ADDRESS -U user -P pass
its not working. I am getting below error:
locale is "en_IN"
locale charset is "UTF-8"
using default charset "UTF-8"
Msg 18456 (severity 14, state 1) from CENTERFORALLIED\SQLEXPRESS Line 1:
"Login failed for user 'ibuser'."
Error 20002 (severity 9):
Adaptive Server connection failed
There was a problem connecting to the server
I am not sure why this is trying to connect to SQLEXPRESS instance. My instance is MSSQLSERVER_2K8 and I am explicitly mentioned it in config file and even tried in arguments.
instance is MSSQLSERVER_2K8 (w/o the hostname part). You can alternatively conf directly the port.
Note that resolving the instance to a port relies on the SQL Server Browser Service being enabled and started. If is not started then you must use the port explicitly, and the port must be statically configured (not dynamic).

Resources