Connection Strings in Laravel

In Laravel, I set my SQL Server connection string like this in the .env file: -

DB_CONNECTION=mssql
DB_HOST=sql-server123.database.windows.net
DB_PORT=1433
DB_DATABASE=dev-db-123
DB_USERNAME=user123
DB_PASSWORD=pass123

But now I'd like to format my connection string to connect to SQL Azure using the system-assigned managed identity provided by Azure Cloud.

As a test, I used Microsoft's example to connect using a System Managed Identity: -

https://docs.microsoft.com/en-us/sql/connect/php/azure-active-directory?view=sql-server-ver15

<?php

$azureServer = 'myazureserver.database.windows.net';
$azureDatabase = 'myazuredatabase';
$connectionInfo = array('Database'=>$azureDatabase,
                        'Authentication'=>'ActiveDirectoryMsi');
$conn = sqlsrv_connect($azureServer, $connectionInfo);

if ($conn === false) {
    echo "Could not connect with Authentication=ActiveDirectoryMsi (system-assigned).\n";
    print_r(sqlsrv_errors());
} else {
    echo "Connected successfully with Authentication=ActiveDirectoryMsi (system-assigned).\n";
    
    $tsql = "SELECT @@Version AS SQL_VERSION";
    $stmt = sqlsrv_query($conn, $tsql);
    if ($stmt === false) {
        echo "Failed to run the simple query (system-assigned).\n";
        print_r(sqlsrv_errors());
    } else {
        while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
            echo $row['SQL_VERSION'] . PHP_EOL;
        }

        sqlsrv_free_stmt($stmt);
    }
    
    sqlsrv_close($conn);
}
?>

Where in Laravel can I set the connection string to use: -

'Authentication'=>'ActiveDirectoryMsi'

1 answer

  • answered 2021-04-21 20:11 Francesco Mantovani

    Try this and let me know:

    DB_CONNECTION=mssql
    DB_HOST=sql-server123.database.windows.net
    DB_PORT=1433
    DB_DATABASE=dev-db-123
    DB_USERNAME=user123
    DB_PASSWORD=pass123
    AUTHENTICATION=ActiveDirectoryMsi
    

    or in alternative:

    DB_AUTHENTICATION=ActiveDirectoryMsi