SQL Server Add ApplicationIntent = ReadOnly on Laravel Application

mw3dktmi  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(210)

I'm using a Laravel 7.3, and one of my requirement is to connect from an Azure Replica Database that includes adding ApplicationIntent = ReadOnly on connection string. But I cant find a way to a customize the connection string generated by laravel to create a PDO Object.

I also tried adding ApplicationIntent = ReadOnly on the database configuration options, but still no luck

'sql_server_readonly' => [
            'driver' => 'sqlsrv',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '1433'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
            'options'=> [
                'ApplicationIntent' => 'ReadOnly'
            ]
        ],
c90pui9n

c90pui9n1#

On Laravel 8.0, I needed to set 'readonly' => true for the ApplicationIntent to be set. See framework code here: https://github.com/illuminate/database/blob/ae346d0a75aa87b83783d44b926d3fcc2d18f430/Connectors/SqlServerConnector.php#L135

full config:

'sqlsrv_readonly' => [
            'driver' => 'sqlsrv',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '1433'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
            'readonly' => true
        ],
wmomyfyw

wmomyfyw2#

Just add it on the database.php connection config

'sql_server_readonly' => [
            'driver' => 'sqlsrv',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '1433'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
            'ApplicationIntent' => 'ReadOnly'
        ],

相关问题