Connect to external MySql database

Copper Contributor

Hello all,

I finally convinced to write here after an hour of pointless searching.

I have an App written in .net framework, that uses MysqlConnector to connect to external database, hosted on Aiven.

The app does not work in Azure App service, since it complains that it cannot connect to the database.

The documentation tells about configuring Vnet I'm order to open the necessary tcp ports for the app to connect to the external database. Is this really necessary? App network configuration reports there's no limitations.

 

Thanks

1 Reply

Hey @johnnyontheweb,

You've run into a common issue when trying to connect an Azure App Service to an external MySQL database. Here's the main thing: Azure App Services operate in a sandbox environment with limited networking capabilities by default.

 

To connect to an external MySQL database, especially one hosted on Aiven, you typically need to ensure that your app can reach the database server over the network.

 

Here’s a step-by-step approach to solve your issue:

  1. VNet Integration:

    • Azure App Service needs to be integrated into a Virtual Network (VNet) to allow outbound traffic on specific ports. This is often necessary when connecting to external services securely.
    • Follow the documentation to integrate your App Service with a VNet. This allows you to control network access rules, including opening necessary TCP ports.
  2. Network Security Rules:

    • Ensure that your VNet has the appropriate Network Security Group (NSG) rules to allow outbound traffic to your MySQL database’s IP address on the required port (usually 3306).
  3. Database Configuration:

    • Verify that your Aiven MySQL instance allows connections from the IP address range used by your Azure VNet.
    • Check the firewall settings on the MySQL instance to make sure they permit connections from the Azure App Service.
  4. Connection String:

    • Double-check your connection string in the .NET app. Ensure it includes the correct server address, port, database name, user ID, and password.
  5. App Service Settings:

    • Go to your App Service settings in the Azure portal, navigate to "Networking," and ensure that VNet integration is correctly configured.
    • Also, check the “Application settings” to make sure there are no conflicting settings that might block the outbound connection.
  6. Logging and Diagnostics:

    • Enable diagnostic logging for your App Service to get more insights into what might be going wrong during the connection attempt. You can review the logs to see if there are any specific errors or warnings related to the network or database connection.

By integrating your Azure App Service with a VNet and ensuring the correct network configurations, you should be able to resolve the connectivity issue with your Aiven-hosted MySQL database. If there are still problems, detailed error logs will help pinpoint the exact issue.

 

If you need more help, feel free to ask!

Cheers!