1

I got a ubuntu docker container which runs php 5.5.9, laraverl 5.2 which can connect successfully to SQL Server and get results back.

The docker image I am using is https://hub.docker.com/r/h2labs/laravel-mssql/

The problem I got is that the server uses encryption and I cant find how to pass the following parameters to the laravel connection string for mssql

ENCRYPT=yes;trustServerCertificate=true

My SQL Server connection string at present looks like this

DB_CONNECTION=sqlsrv
DB_HOST=sql.mydomain.com
DB_PORT=1433
DB_DATABASE=mydbname
DB_USERNAME=mysusername
DB_PASSWORD=mypass

My laravel database config looks like this

        'sqlsrv' => [
        'driver' => 'sqlsrv',
        'host' => env('DB_HOST', 'localhost'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8',
        'prefix' => '',
    ],

The SQL Server error log entry is

Encryption is required to connect to this server but the client library does not support encryption; the connection has been closed. Please upgrade your client library. [CLIENT: 103.31.114.56]

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mav Peri
  • 11
  • 1
  • 4

1 Answers1

9

Support for either option was not introduced until Laravel 5.4; Specifically, v5.4.11

So you would first need to upgrade to laravel/framework:>=5.4.11,<5.5

Then, to configure your application, you will need to modify your config/database.php file as follows:

// ...
'sqlsrv' => [
    'driver' => 'sqlsrv',
    'host' => env('DB_HOST', 'localhost'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8',
    'prefix' => '',
    'encrypt' => 'yes', // alternatively, defer to an env variable
    'trust_server_certificate' => 'true', // alternatively, defer to an env variable
],
// ...

DatabaseServiceProvider, via ConnectionFactory and SqlServerConnector will use this to build the underlying PDO connection with those options set in the DSN.

Matt Ward
  • 91
  • 2
  • Not sure this is right though. Laravel 5.4 will need php version > 5.5.9 which will mean certain packages will not be avail on the image I work on. Can you provide a link to an image with a working setup> – Mav Peri Apr 12 '17 at 13:15
  • Which packages are problematic? PHP 5.5 reached end of life on [21 July 2016](https://en.wikipedia.org/wiki/PHP#Release_history), so it's probably easier to work around those packages. – Matt Ward Apr 12 '17 at 13:20