Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thaMink's avatar

Microsoft Azure SQL Server

Can't figure out how to get connected to a microsoft sql server with laravel.

Have the configuration set up with correct credentials, hostname and port - and can connect from SQL management server but can't connect with Laravel. I get ....

Adaptive Server connection failed (myservername.database.windows.net:1433)

I'm pretty sure it's an Azure SQL server, but I've never connected to one with Laravel. When I check my php -m on Homestead, I see that pdo_dblib is there, so I'm not sure what the issue could be. Anyone have any ideas or next steps for troubleshooting?

0 likes
7 replies
Tippin's avatar

@thamink If you are connecting to an Azure MySQL, do you have SSL enabled? If you do, you have to export the .pem file from azure into your project and update the database.php config to point to the ssl, as well as your .env

database.php

        'mysql' => [
            'driver'    => 'mysql',
            'url'       => env('DATABASE_URL'),
            'host'      => env('DB_HOST', 'yourdatabase.com'),
            'port'      => env('DB_PORT', '3306'),
            'database'  => env('DB_DATABASE', 'DBname'),
            'username'  => env('DB_USERNAME','username'),
            'password'  => env('DB_PASSWORD', 'password'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
            'engine'    => null,
            'modes'       => [
                'ONLY_FULL_GROUP_BY',
                'STRICT_TRANS_TABLES',
                'NO_ZERO_IN_DATE',
                'NO_ZERO_DATE',
                'ERROR_FOR_DIVISION_BY_ZERO',
                'NO_ENGINE_SUBSTITUTION',
            ],
            'sslmode' => env('DB_SSLMODE', 'prefer'),
            'options'   => (env('MYSQL_SSL')) ? [
                PDO::MYSQL_ATTR_SSL_KEY => base_path('config/ssl/YourApp.crt.pem'),
            ] : []
        ],

.env

DB_CONNECTION=mysql
DB_HOST=yourapp.mysql.database.azure.com
DB_PORT=3306
DB_DATABASE=yoursite
DB_USERNAME=user@yoursite
DB_PASSWORD=password
MYSQL_SSL=true
thaMink's avatar

No, I don't have SSL enabled. That I'm aware of anyways.

thaMink's avatar

Will do. But to be clear, this isn't MySQL this is SQL Server.

thaMink's avatar

What is odd is that the following totally works. But I can't get the regular connection in laravel.

try {    
    $hostname = 'your_host_name.database.windows.net';
    $dbname = 'database_name';
    $username = 'database_user';
    $pwd = 'database_password';
 
    $pdo = new PDO ("dblib:version=8.0;charset=UTF-8;host={$hostname};dbname={$dbname}", $username, $pwd);
    $query = "SELECT * FROM location";
    $statement = $pdo->prepare($query);
    $statement->execute();
    
    $results = $statement->fetchAll(PDO::FETCH_ASSOC);
    var_dump($results);
 
} catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
}
thaMink's avatar
thaMink
OP
Best Answer
Level 2

For the poor sucker who finds this thread later on in life. My solution was to use the URL method. I kept all the DB config with username, host, password, dbname etc, but added the following item to my .env which apparently overwrites the default connection string.

DB_URL="sqlsrv;charset=UTF-8;host=the.server.url.windows.net;dbname=thedatabasename"

That did the trick. Not sure why it didn't build that connection string by itself, but there you have it.

FYI, for SEO purposes, my error prior to getting the fix was "Adaptive Server connection failed"

2 likes

Please or to participate in this conversation.