mkleehammer / pyodbc

Python ODBC bridge
https://github.com/mkleehammer/pyodbc/wiki
MIT No Attribution
2.92k stars 561 forks source link

Login Timeout Error when connecting to Synapse database using pyodbc python library in Azure synapse notebook via Managed Identity #1341

Open hellorohitsharma opened 5 months ago

hellorohitsharma commented 5 months ago

I am running my Python code in Synapse Jupiter notebooks on Azure Synapse. I am trying to connect to Synapse DB with pyodbc library using Managed identity. I am getting Login Timeout Error.

To diagnose, we usually need to know the following, including version numbers. On Windows, be sure to specify 32-bit Python or 64-bit: I

Code snippet: driver='{ODBC Driver 17 for SQL Server}'

try:

conn = pyodbc.connect('DRIVER='+driver+';SERVER=tcp:'+server+';PORT=#####;DATABASE='+database+';Encrypt=yes;Integrated Security=false;TrustServerCertificate=no;Connection Timeout=30;Authentication=ActiveDirectoryMsi;')

conn.autocommit=True
cursor=conn.cursor()
print(f"connection established with the SQL DB")

except Exception as E: print(f"Error occurred while establishing connections with Synapse DB", E)

Error: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')

Please help me on this.

v-chojas commented 5 months ago

Make sure the managed identity is enabled on the VM, and that the server is reachable from it. The "timeout expired" suggests it couldn't connect to the server.

And for the record, "PORT" is NOT a valid connection string option, nor is "Connection Timeout". Where are you getting that from? Here is the list that the driver supports: https://learn.microsoft.com/en-us/sql/connect/odbc/dsn-connection-string-attribute?view=sql-server-ver16