I am trying to connect to Azure SQL server (authentication via active directory password) on linux VM using Python, sqlalchemy
error enter image description here
ODBC driver installed on VM enter image description here
Connection string used:
driver= "ODBC Driver 18 for SQL Server", endpoint="####-####.database.windows.net", dbname="sql#####", user="[email protected]", password="abc123"
connection_string = "DRIVER={};SERVER={},port=1433;DATABASE={};UID={};PWD={};Authentication=ActiveDirectoryPassword;".format(driver, endpoint, dbname, user, password)
connection_url = URL.create("mssql pyodbc", query={"odbc_connect": connection_string})
engine = sqlalchemy.create_engine(connection_url, fast_executemany=True, pool_size=100) conn = engine.connect()
Note:
- SQL Server has only active directory password authentication
- I have tried the endpoint with IP as well, no luck
CodePudding user response:
I created linux virtual machine and sql database with Active directory authentication only. in Azure portal. I installed ODBC Driver 17 for SQL Server in the virtual machine, and I tried to connect Azure sql database with active directory authentication using below code
import sqlalchemy as sa
username = "server"
password = "password"
host = "dbservere.database.windows.net"
database = "db"
authentication = "ActiveDirectoryPassword"
conn_string = sa.engine.url.URL.create(
"mssql pyodbc",
username=username,
password=password,
host=host,
port=1433,
database=database,
query={"driver": "ODBC Driver 17 for SQL Server","authentication": authentication},
)
engine = sa.create_engine(conn_string, pool_timeout=30)
connection = engine.connect()
I got the same error.
Image for reference:
I uninstall the ODBC Driver 17 for SQL Server in the virtual machine, and install ODBC Driver 13 for SQL Server in the virtual machine again I tried with the above changing odbc driver version as below
import sqlalchemy as sa
username = "username"
password = "password"
host = "server.database.windows.net"
database = "db"
authentication = "ActiveDirectoryPassword"
conn_string = sa.engine.url.URL.create(
"mssql pyodbc",
username=username,
password=password,
host=host,
port=1433,
database=database,
query={"driver": "ODBC Driver 13 for SQL Server","authentication": authentication},
)
engine = sa.create_engine(conn_string, pool_timeout=30)
connection = engine.connect()
it connected successfully to the Azure sql database. Image for reference:
It worked for me kindly check from your side.