Cannot connect to MySql Database from local Django project

Anish Chapagai 46 Reputation points
2022-05-22T07:35:53.043+00:00

I'm having issues connecting my Django app in my local machine to MySql Database in Azure? I added my IP in the Rules and am connecting with this:

python
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'HOST': '<servername>.database.windows.net',
        'PORT': '1433',
        'NAME': '<database_name>',
        'USER': '<admin>',
        'PASSWORD': '<cecret>',
        'OPTIONS': {'ssl': {'pem': 'tls.pem'} }
    },

I can connect using AzureDataStudio, but not with this configuration in django. I Nmaped my host, found a bunch of open ports but 3306 and 1433 are bound to Sql something.

Django's runserver shows MySQLdb._exceptions.OperationalError: (2002, "Can't connect to server on '<servername>.database.windows.net' (115)") with this configuration in port 3306 even if I have that server and database within it running.

One example php query string in Azure portal has:

php
$conn = new PDO("sqlsrv:server = tcp:<server_name>.database.windows.net,1433; Database = <database_name>", "<admin>", "{your_password_here}");

So, I'm assuming I should connect to 1433 and it works from DataStudio. From python manage.py runserver it shows django.db.utils.OperationalError: (2013, "Lost connection to server at 'handshake: reading initial communication packet', system error: 104"). I'm at the limit of my knowledge regarding this.

Azure Database for MySQL
Azure Database for MySQL
An Azure managed MySQL database service for app development and deployment.
711 questions
{count} votes

Accepted answer
  1. Cristian SPIRIDON 4,471 Reputation points
    2022-05-22T20:33:30.093+00:00

    Hi,

    Did you tried with the mssql module provided by msft?

    https://learn.microsoft.com/en-us/samples/azure-samples/azure-sql-db-django/azure-sql-db-django/

    Hope this helps!


0 additional answers

Sort by: Most helpful