laravel5經過freetds鏈接sqlserver

相關版本

系統ubuntu 16.04, 使用的PHP版本是7.0.30, sqlserver 2012, freetds爲0.92 Laravel5.5和5.4都測試過了

安裝php驅動

sudo apt-get install php7.0-odbc 
sudo apt install php7.0-sybase

安裝freetds

sudo apt-get install freetds-bin freetds-common tdsodbc odbcinst unixodbc unixodbc-dev  
sudo mv /etc/odbcinst.ini /etc/odbcinst.ini.bak  
sudo cp /usr/share/tdsodbc/odbcinst.ini /etc/

配置freetds

sudo vim /etc/freetds/freetds.conf

修改配置php

[global]
    tds version = 8.0 # TDS version, ref <a href="http://www.freetds.org/userguide/choosingtdsprotocol.htm" target="_blank">this</a>.
    client charset = UTF-8
    text size = 20971520
[Server2012] #自定義名稱,後面須要使用
    host = {yourdomain}.database.windows.net // ip地址或域名
    port = 1433
    tds version = 8.0 #8.0爲2012其餘自行測試

測試SQLSERVER

TDSVER=8.0 tsql -H my_server_host -p 1433 -U my_user -P my_password -D my_database

配置Laravel5

打開config/database.phpconnections中添加配置,驅動使用sqlsrvmysql

'mssql' => [
        'driver' => 'sqlsrv',
        'host' => 'Server2012', // 這個對應freetds.conf的配置名稱
        'port' => '1433',
        'database' => env('DB_DATABASE', '數據庫'),
        'username' => env('DB_USERNAME', '用戶'),
        'password' => env('DB_PASSWORD', '密碼'),
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'strict' => false,
        'engine' => null,
    ],

多數據庫

若是你用了mysql又想用sqlserver的部份信息,我的項目緣由,但通常的作法是sqlserver的系統寫API讓Mysql系統的調用,但此次偷偷懶,就兩個一塊兒用了
在Model中 加入protected $connection = 'mssql';並使用protected $table = 'EMPLOYEE'; 指明數據表,就能夠不用每次在Controller寫鏈接了.sql

相關文章
相關標籤/搜索