Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot connect to SQL server from python using Active Directory Authentication

I am using pymssql library to connect python to Sql Server. I can connect using windows/sql server authentication. I want to connect using Active Directory Authentication.

Tried the below connection string. But it fails with error :

unexpected keyword authentication


conn = pymssql.connect(server='adventureworks.database.windows.net', authentication = 'Active Directory Password',user='[email protected]',password='Enterpasswordhere', database='dbo')
like image 897
HadoopAddict Avatar asked Oct 18 '16 21:10

HadoopAddict


People also ask

How do I connect SQL Server client to server with Windows authentication mode?

Open SQL Server Management Studio. In Connect to Server, select Database Engine, enter your SQL Server name, and enter administrator credentials to connect to the server. Select Connect. In Object Explorer, expand the SQL Server, expand Security, right-click Logins, and then select New Login.

How do I connect to SQL Server with SQL authentication?

Right-click the server you wish to modify and then click Properties. Select the Security Page. Under the Server authentication heading choose either the desired authentication: Windows Authentication or SQL Server and Windows Authentication mode. Click OK.


1 Answers

Update 2019-11-01: The "sign-in assistant" appears to be part of Windows Essentials, which has been discontinued and is no longer available for download.


You can do this using ODBC Driver 13.1 for SQL Server and the Microsoft Sign-In assistant if you are on a Windows machine.

  1. install ODBC Driver 13.1 for SQL Server
    https://www.microsoft.com/download/details.aspx?id=53339

  2. install sign-in assistant (link broken)
    http://go.microsoft.com/fwlink/?LinkId=234947

  3. test connection

    import pyodbc
    server = servername ending in windows.net
    database = database
    username = username
    password = password
    tablename = tablename

    driver= '{ODBC Driver 13 for SQL Server}’
    cnxn = pyodbc.connect('DRIVER=‘ + driver +
    ';PORT=1433;SERVER=‘ + server +
    ';PORT=1443;DATABASE=‘+database+
    ';UID='+username+
    ';PWD='+ password +
    ';Authentication=ActiveDirectoryPassword’)

    cursor = cnxn.cursor()
    cursor.execute("SELECT TOP 20 * from ” + tablename)
    row = cursor.fetchone()

    while row:

    print str(row[0]) + " " + str(row[1])
    row = cursor.fetchone()

like image 130
Marissa Saunders Avatar answered Oct 15 '22 20:10

Marissa Saunders