Can't connect to remote SQL Servers outside of

2020-07-08 08:11发布

问题:

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.

回答1:

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.