0
votes

I tried to connect django to Azure database using django-pyodbc-azure and made sure that my settings in setting.py are correct. But still got this problem. I heard that this could be caused by some authentication problem but not sure how to solve this.

django.db.utils.Error: ('28000', "[28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'USERNAME'. (18456) (SQLDriverConnect)")

DATABASES = {
'default': {
    'ENGINE': 'sql_server.pyodbc',
    'NAME': 'SERVER_NAME',
    'USER': 'USERNAME',
    'PASSWORD': 'PASSWORD',
    'HOST': 'SERVER_NAME.database.windows.net',
    'PORT': '',
}

}

1
What format did you use for username? Did you include the server name in the username? - David Makogon
@DavidMakogon I was using [email protected]@SERVER_NAME. And it tells me that login failed for user [email protected] - Hansong Li
That doesn't look like a valid username (having an email address as a SQL Database username) - David Makogon
@DavidMakogon just to be sure, the username here should be the one I used to log into azure portal right? - Hansong Li
No. Username is the one you created for your database. - David Makogon

1 Answers

0
votes

@HansongLi, The connection string of Azure SQL Database is in this format Server=<ServerName>,<ServerPort>;Database=<DatabaseName>;UiD=<UserName>;Password={your_password_here};Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;.

Correspondingly, the database configuration is defined as below, see the article here.

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': '<DatabaseName>',
        'USER': '<UserName>',
        'PASSWORD': '{your_password_here}',
        'HOST': '<ServerName>',
        'PORT': '<ServerPort>',
        'OPTIONS': {
            'driver': 'SQL Server Native Client 11.0',
            'MARS_Connection': 'True',
        }
    }
}

Meanwhile, the <UserName> should be in the format <name>@<sql-azure-name> when the host <ServerName> is like this tcp:<sql-azure-name>.database.windows.net.

You can find the connection string on Azure portal.

enter image description here

Or at the tab DASHBOARD of Azure classic portal.

enter image description here