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:
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '<servername>.database.windows.net',
'PORT': '3306',
'NAME': '<database_name>',
'USER': '<admin>@<servername>',
'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 servers.
Django's runserver
shows MySQLdb._exceptions.OperationalError: (2002, "Can't connect to server on '<servername>.database.windows.net' (115)")
with this configuration even if I have that server and database within it running.
One example php
query string in Azure portal has:
$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
but only 3306
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")
if I try port 1433
. I'm at the limit of my knowledge regarding this.
Correction-1: 3306
doesn't seem to work with Azure DataStudio. But using 1433
in Django settings won't even initialize connection.
Apparently I was using connection string and backend that didn't support the "MySql database" I was using. I'm still vague on how it worked but here it goes.
I created a new Azure Database for MySql servers
and created a new Database there. Then used connection strings as provided by @UtkarshPal-MT in my original Django's DATABASE={}
entry. Didn't have to do anything else. It just connected.
Note: You do compulsorily require to pass that certificate.
Edit1: If you're using this, as I did.
Then you need to use this mssql-django external backend with proper drivers. You can find details on using this backend in that PyPi page.