Share via

Using service connector variables

lalin 0 Reputation points
2023-10-12T16:06:27.64+00:00

I have a container app that I connected to a MySQL database using service connector. I tried to check if they were connected using this code from the documentation:

<?php
    $host = "<db-host>";
    $username = "<user>";
    $password = "<password>";
    $database = "<db-name>";
    $conn = mysqli_init();

    if (!mysqli_real_connect($conn, $host, $username, $password, $database, 3306, NULL)) {
        die('Failed to connect to MySQL: ' . mysqli_connect_error());
    }

    echo 'Connected successfully to MySQL database!';
    mysqli_close($conn);
?>

I have also tried using an env file, and they both work. However, when I tried using the variables listed on service connector instead, which are basically the same variables, I got the error undefined array key. Is it possible to use the service connector variables instead of directly putting them or using an env file?

Azure Database for MySQL
Azure Container Apps
Azure Container Apps

An Azure service that provides a general-purpose, serverless container platform.

0 comments No comments

2 answers

Sort by: Most helpful
  1. GeethaThatipatri-MSFT 29,597 Reputation points Microsoft Employee Moderator
    2023-10-31T13:58:15.0166667+00:00

    @lalin You could use below ways to list configurations added by ServiceConnector, then use the right key names in your php test code.

    Use Azure Portal, your ACA -> ServiceConnector node at left panel -> the expanding arrow at the beginning of your connector

    User's image

    • Use Azure CLI

    az container app connection list-configuration -g <your_resource_group> -n <your_aca_name> --connection <your_serviceconnection_name>

    Regards

    Geetha

    Was this answer helpful?

    0 comments No comments

  2. GeethaThatipatri-MSFT 29,597 Reputation points Microsoft Employee Moderator
    2023-10-13T15:34:25.0566667+00:00

    @lalin Welcome to Microsoft Q&A thanks for posting your question

    Yes, it is possible to use the service connector variables, you need to call it using getenv() function . Service connector creates env variables for databases with a specific name so you would need to use those names exactly.

    I recommend referring to this article on how to use service connector with MySQL Flexible Server https://learn.microsoft.com/en-us/azure/service-connector/how-to-integrate-mysql?tabs=php

    <?php
    $host = getenv('AZURE_MYSQL_HOST');
    $username = getenv('AZURE_MYSQL_USER');
    $password = getenv('AZURE_MYSQL_PASSWORD');
    $database = getenv('Azure_MYSQL_DBNAME');
    $port = int(getenv('AZURE_MYSQL_PORT'));
    # $flag = getenv('AZURE_MYSQL_FLAG');
    
    $conn = mysqli_init();
    # mysqli_ssl_set($conn,NULL,NULL,NULL,NULL,NULL);
    mysqli_real_connect($conn, $host, $username, $password, $database, $port, NULL);
    
    if (mysqli_connect_errno($conn)) {
        die('Failed to connect to MySQL: ' . mysqli_connect_error());
    }
    
    echo 'Connected successfully to MySQL database!';
    mysqli_close($conn);
    ?>
    
    

    Regards

    Geetha

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.