1

I want to connect to MSSQL server2012 from Python using pyodbc on a Debian8 host. The current code already works for Azure SQL server but not for local SQL server. The connection string looks like this:

import pyodbc

server = 'IP' 
database = 'DB_Name'
username = 'user'
password = 'pass'
driver = '{ODBC Driver 17 for SQL Server}'

cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password)

I had to use IP address because I could not reach SQL server via name. I can connect to the server with SSMS on a windows machine. I have tried the following combinations as well:

server='sqlservername\instancename'
server='IP, port'
server='IP\instancename'
driver='SQL Server'
Integrated_Security=false;
Trusted_Connection=no;

but none of them worked. I got either Login timeout or error in registering user. I run the same python code on windows machine without any problem.

sherry
  • 11
  • 2
  • So you are running Python+pyodbc on a Debian8 machine and you can connect to an Azure SQL instance out in the cloud but you cannot connect to an instance of SQL Server running on another (Windows) machine in your local network (LAN). Does that accurately describe the problem? If so, have you verified that the LAN instance of SQL Server has TCP/IP connections enabled? – Gord Thompson Apr 30 '19 at 11:54
  • Hi @GordThompson yes thats the problem description, actually I can connect to that MSSQL instance from a Linux(Debian9) machine in a DotNet application(to connect to another DB on the same instance) and it works. – sherry May 02 '19 at 07:12

1 Answers1

0

Finally i found the correct combination. Besides thanks to @GordThompson the TCP/IP was enabled but on another port.

import pyodbc

server = 'IP\InstanceName, port' 
database = 'DB_Name'
username = 'user'
password = 'pass'
driver = '{ODBC Driver 17 for SQL Server}'

cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
sherry
  • 11
  • 2