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

China☆狼群 提交于 2020-08-02 07:23:17

问题


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.



来源:https://stackoverflow.com/questions/24854745/cant-connect-to-remote-sql-servers-outside-of-network-with-pyodbc

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