最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Connecting Laravel to SQL Server certificate problem - Stack Overflow

programmeradmin5浏览0评论

I have installed latest Laravel 11.30 and it works good with MySQL. Now I need to connect to another server that is using SQL Server and read some data from a table. I have installed sqlsrv and pdo driver and odbc 18 but I am not sure should I config something else for using them or not? Both servers are windows, firewall on both are disabled. I am using xamp on windows to run php and mydql where Laravel istalled. Add SQL Server connection data to confug->database and add connection data to .ENV file write a little code to test it. Code follows:

Config->database

'sqlsrv_attendance' => [
            'driver' => 'sqlsrv',
            'host' => '192.168.10.13',
            'database' => 'dbname',
            'username' => 'user',
            'password' => 'pass',
            'charset' => 'utf8',
            'prefix' => '',
        ],

env

#####SQL Server Added
DB_SQLSRV_HOST=192.168.10.13
DB_SQLSRV_PORT=1433
DB_SQLSRV_DATABASE=dbname
DB_SQLSRV_USERNAME=user
DB_SQLSRV_PASSWORD=pass
DB_OPTIONS="TrustServerCertificate=true"
DB_ODBC=0
#####################

Route

Route::middleware(['auth'])->group(function () {


    Route::get('/test-query', function () {
        try {
            $data = DB::connection('sqlsrv_attendance')->select('SELECT TOP 1 * FROM DataFile');
            dd($data);
        } catch (\Exception $e) {
            return  $e->getMessage();
        }
    });

but I got this error.

SQLSTATE[08001]: [Microsoft][ODBC Driver 18 for SQL Server]SQL Server Network Interfaces: Connection string is not valid [87]. (Connection: sqlsrv_attendance, SQL: SELECT TOP 1 * FROM DataFile)

Googling the problem shows solving it with adding DB_OPTIONS="TrustServerCertificate=true" in nodejs. What can I do? Should I config ODBC or DSN? I appreciate any help.

I have installed latest Laravel 11.30 and it works good with MySQL. Now I need to connect to another server that is using SQL Server and read some data from a table. I have installed sqlsrv and pdo driver and odbc 18 but I am not sure should I config something else for using them or not? Both servers are windows, firewall on both are disabled. I am using xamp on windows to run php and mydql where Laravel istalled. Add SQL Server connection data to confug->database and add connection data to .ENV file write a little code to test it. Code follows:

Config->database

'sqlsrv_attendance' => [
            'driver' => 'sqlsrv',
            'host' => '192.168.10.13',
            'database' => 'dbname',
            'username' => 'user',
            'password' => 'pass',
            'charset' => 'utf8',
            'prefix' => '',
        ],

env

#####SQL Server Added
DB_SQLSRV_HOST=192.168.10.13
DB_SQLSRV_PORT=1433
DB_SQLSRV_DATABASE=dbname
DB_SQLSRV_USERNAME=user
DB_SQLSRV_PASSWORD=pass
DB_OPTIONS="TrustServerCertificate=true"
DB_ODBC=0
#####################

Route

Route::middleware(['auth'])->group(function () {


    Route::get('/test-query', function () {
        try {
            $data = DB::connection('sqlsrv_attendance')->select('SELECT TOP 1 * FROM DataFile');
            dd($data);
        } catch (\Exception $e) {
            return  $e->getMessage();
        }
    });

but I got this error.

SQLSTATE[08001]: [Microsoft][ODBC Driver 18 for SQL Server]SQL Server Network Interfaces: Connection string is not valid [87]. (Connection: sqlsrv_attendance, SQL: SELECT TOP 1 * FROM DataFile)

Googling the problem shows solving it with adding DB_OPTIONS="TrustServerCertificate=true" in nodejs. What can I do? Should I config ODBC or DSN? I appreciate any help.

Share Improve this question edited Feb 16 at 23:12 Dale K 27.3k15 gold badges57 silver badges83 bronze badges asked Feb 16 at 23:09 hamidhamid 318 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Your configuration from config/database.php file is not affected by the contents on the .env file because you are not overriding it.

Your code on config/database.php should look as follows.

'sqlsrv_attendance' => [
    'driver' => 'sqlsrv',
    'host' => env('DB_SQLSRV_HOST', '127.0.0.1'),
    'port' => env('DB_SQLSRV_PORT', 1433),
    'database' => env('DB_SQLSRV_DATABASE', 'dbname'),
    'username' => env('DB_SQLSRV_USERNAME', 'user'),
    'password' => env('DB_SQLSRV_PASSWORD', 'pass'),
    'charset' => 'utf8',
    'prefix' => '',
    'encrypt' => 'yes',
    'trust_server_certificate' => 'true',
],
发布评论

评论列表(0)

  1. 暂无评论