Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't connect to remote SQL Servers outside of network with PYODBC

I for some reason can't seem to connect to any SQL Server that is outside of our network, yet have no problem if I'm within the network via VPN. My code is as such for local connection:

sql = pyodbc.connect(
    'DRIVER={FreeTDS};'
    'SERVER=192.168.1.xx\ROA;'
    'DATABASE=RentalDB;'
    'UID=xxxxxxx;'
    'PWD=xxxxxxx'
)

and the following is what I'm trying for remote:

sql = pyodbc.connect(
    'DRIVER={FreeTDS};'
    'SERVER=69.178.xx.xx/ROA;'
    'DATABASE=RentalDB;'
    'UID=xxxxxxxx;'
    'PWD=xxxxxxxx'
)

When trying to connect, I am thrown the following error after about 15 seconds or so:

pyodbc.Error: ('08001', '[08001] [unixODBC][FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')

I'm able to successfully connect via SQL Server Management Studio, and we have a C# developer that has no problem with establishing a connection, but for some reason, I can't. I am running Python 2.7, Debian 7, FreeTDS 0.91, and PYODBC 3.0.7. The SQL Server version is 2012, I think. Any ideas?

EDIT: On the remote connection, i have tried the following for the 'SERVER=' string:

69.178.xx.xx/ROA
69.178.xx.xx\ROA
mssql://69.178.xx.xx/ROA
mssql://69.178.xx.xx\ROA
69.178.xx.xx:1433/ROA
69.178.xx.xx:1433\ROA
mssql://69.178.xx.xx:1433/ROA
mssql://69.178.xx.xx:1433\ROA

and all of them return the same error.

EDIT 2: Fixed the port number on the things above.

like image 322
crookedleaf Avatar asked Jul 20 '14 20:07

crookedleaf


Video Answer


1 Answers

I have finally figured out what the answer is (by accident while trying to also troubleshoot connecting to a SQL Server hosted on Azure). The PYODBC connection string, if connecting outside of the network the SQL Server is on, requires a port number, rather than assuming the default like other SQL packages use. Simply adding 'PORT=1433;' to the connection string got the connection to work. PYODBC seems to be picky about when it does and doesn't want to use the default port for connections.

like image 120
crookedleaf Avatar answered Nov 15 '22 04:11

crookedleaf