Search code examples
pythonmysqlazureipwhitelist

Accessing an Azure Database for MySQL Single Server from outside Azure


Moving this question from DevOps Stack Exchange where it got only 5 views in 2 days:


I would like to query an Azure Database for MySQL Single Server.

I normally interact with this database using a universal database tool (dBeaver) installed onto an Azure VM. Now I would like to interact with this database using Python from outside Azure. Ultimately I would like to write an API (FastAPI) allowing multiple users to connect to the database.

I ran a simple test from a Jupyter notebook, using SQLAlchemy as my ORM and specifying the pem certificate as a connection argument:

import pandas as pd
from sqlalchemy import create_engine

cnx = create_engine('mysql://XXX', connect_args={"ssl": {"ssl_ca": "mycertificate.pem"}})

I then tried reading data from a specific table (e.g. mytable):

df = pd.read_sql('SELECT * FROM mytable', cnx)

Alas I ran into the following error:

'Client with IP address 'XX.XX.XXX.XXX' is not allowed to connect to this MySQL server'.

According to my colleagues, a way to fix this issue would be to whitelist my IP address. While this may be an option for a couple of users with static IP addresses I am not sure whether it is a valid solution in the long run.

Is there a better way to access an Azure Database for MySQL Single Server from outside Azure?


Solution

  • As mentioned in comments, you need to whitelist the IP address ranges(s) in the Azure portal for your MySQL database resource. This is a well accepted and secure approach.